open channel "nul" within non-main thread on windows (not-console application)
As I understand, first calls of `open "nul" w`, on Windows within new thread, wrap the open channel to the std-channels.
So second to fourth call (first time executed) get corresponding stdin, stdout and stderr.
This occurs only within not console application, that does not share standard channels (currently reproducible for tk, not for tclsh).
So as simplification it looks like:
set t [thread::create];
puts [join [thread::send $t {
set chs {}
time { lappend chs [open nul w] } 5;
foreach ch $chs { close $ch }
set chs
}] \n]
thread::release $t
will give one the output:file11dd288
stdin
stdout
stderr
file11d03f0
---
Original message from Timothy Tomkinson:
```
In wish 8.6.8 on Windows, the 2nd, 3rd, and 4th files that are opened within a thread return the channel names stdin, stdout, and stderr. This does not happen in the main thread, in tclsh 8.6.8, in wish 8.5.*, or on Linux.
For example, the following script writes the first 5 opened channel names to a log file:
The file c:/temp/log.txt will contain something like:
package require Thread
set tid [::thread::create]
::thread::send $tid {
set chan1 [open c:/temp/log.txt w]
set chan2 [open nul w]
set chan3 [open nul w]
set chan4 [open nul w]
set chan5 [open nul w]
puts $chan1 "chan1 = $chan1"
puts $chan1 "chan2 = $chan2"
puts $chan1 "chan3 = $chan3"
puts $chan1 "chan4 = $chan4"
puts $chan1 "chan5 = $chan5"
close $chan1
close $chan2
close $chan3
close $chan4
close $chan5
}
exit
```
chan1 = file303b0c0
chan2 = stdin
chan3 = stdout
chan4 = stderr
chan5 = file3033160
This happens when opening any file (including sockets), not just "nul".
I got it now, this belongs to "wrong" recognition of first empty standart channel slots here.
This construction assumes that if (stdinInitialized = 1, etc), first 3 channels should get mapped to initial empty std-channels among the thread-storage. Which is rather actually wrong for the case of non-console application, or some initialization process does not set correctly stdinInitialized, stdoutInitialized etc in such case.
The same in 8.5 (also mapped to stdin, stdout and stderr), excepting the channels get the names.
This is because of the fully-correct commit [c0b94338ed879e40]:
+ strcpy(tmp, "stdin");
+ strcpy(tmp, "stdout");
+ strcpy(tmp, "stderr");
But the real cause is entirely different - this is TclpGetDefaultStdChannel with not allowed duplication for std-handle, returned with GetStdHandle, so returns NULL from the Tcl_MakeFileChannel...
I'll fix it for all versions >= 8.5.
fixed for both versions now (8.5 [041bc74867] and >= 8.6 [6b38bf6d66]).