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>