Windows 8.6: under ephemeral port exhaustion, sockets get disabled for the whole process and Tcl sometimes crashes
Reproduced on Windows 11 with the 8.6 tip (8.6.18, Thread 2.8.13). Each thread opens a server socket on a free port of 127.0.0.1 and a client connection to it, then exits with both open:
package require Thread
set script {
set srv [socket -server {apply {{c a p} {set ::c $c}}} -myaddr 127.0.0.1 0]
set cli [socket 127.0.0.1 [lindex [fconfigure $srv -sockname] 2]]
update
}
for {set i 0} {$i < 1000} {incr i} {
thread::join [thread::create -joinable $script]
}
Running it repeatedly fills the dynamic port range with TIME_WAIT connections (about 16300 of 16384). Then the first error is couldn't open socket: address already in use, as expected. But 8.6 then fails with sockets are not available on this system or winsock is not initialized, in every thread including the main one, for the rest of the process. In 5 of 40 runs of a stress script the process failed this way, 2 of them with a crash (SIGSEGV). Without port exhaustion it runs clean.
9.0 and 9.1 in the same state just report address already in use (40 of 40 runs without failure each).
Possible cause: InitSockets() in win/tclWinSock.c of 8.6 sets the global initialized to -1 when the per-thread setup fails in one thread, which disables sockets in all threads. 8.7 and 9.0 reworked this initialization. Backporting [a36f76eb6b] (for [40b1814b93]) to 8.6 did not help (4 of 40 runs still failed, 2 crashes). The crash may be related to [06f19cc401], fixed only on main.