Tcl UDP

Ticket Change Details
Login

Ticket Change Details

Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2018 Conference, Houston/TX, US, Oct 15-19
Send your abstracts to tclconference@googlegroups.com or submit via the online form
by Aug 20.
Overview

Artifact ID: b4df16c944bee23a8ee55df6897a32e0b2a559b886b041eb16507f3c6197ad9b
Ticket: b3905a822661d7ccb060926e81b832198ed415b5
Code Not Thread-Safe
User & Date: anonymous 2021-04-12 19:42:27
Changes

  1. foundin changed to: "1.0.11"
  2. icomment:
    The extension crashes when used in multiple threads. The problem is in UDP_CheckProc when it is determining whether it should read the socket. If there are multiple threads, the order in which the threads call this function is not deterministic, so it is possible that the wrong thread will read the socket.<br>
    <br>
    The solution is to verify the correct thread by changing:
    <pre>    if (statePtr->packetNum > 0) {</pre>
    to:<br>
    <pre>    if (statePtr->packetNum > 0 && statePtr->threadId == Tcl_GetCurrentThread()) {</pre>
    
    Here is a script that demonstrates the problem. The main thread reads a socket and a child thread writes a socket:<br>
    <pre>
        package require udp
        package require Thread
        
        proc handleRead {chan} {
            set data [read $chan]
            puts "received [string length $data] byte(s)"
        }
        
        set chan [udp_open]
        set port [fconfigure $chan -myport]
        fileevent $chan readable [list handleRead $chan]
        
        set tid [::thread::create]
        ::thread::send $tid "set port $port"
        ::thread::send $tid {
            package require udp
            set chan [udp_open]
            fconfigure $chan -remote [list localhost $port] -buffering none
        }
        
        while 1 {
            ::thread::send $tid {puts -nonewline $chan A}
            update
        }
    </pre>
    
  3. login: "anonymous"
  4. mimetype: "text/html"
  5. private_contact changed to: "1c57e67924247fd7926e89348f266c081ec57db2"
  6. severity changed to: "Critical"
  7. status changed to: "Open"
  8. title changed to: "Code Not Thread-Safe"
  9. type changed to: "Code_Defect"