| Ticket UUID: | e8efa378a96bca5e69e00e5feff1315c932ed47b | ||
| Title: | Duplicate std* channels in different Threads | ||
| Type: | Bug | Created on: | 2013-08-29 12:44:17 |
| Submitter: | JKU | Assigned to: | aku |
| Subsystem: | 25. Channel System | Severity: | Severe |
| Priority: | 5 Medium | Last modified: | 2026-10-14 20:21:04 |
| Status: | Open | Closed by: | nobody |
| Resolution: | None | Closed on: | |
| Version: | 8.6.0 | ||
| Description: | ||||
|
When I create/open a channel in an other Thread (created with
% file channels
stdout stderr stdin
% package require Thread
2.6.7
% set tid [thread::create]
tid000006C0
% thread::send $tid {package require tcl::chan::string}
1.0.1
% thread::send $tid {::tcl::chan::string "Hello World\n"}
stdin
% thread::send $tid {open test.txt w}
stdout
% thread::send $tid {file channels}
stdin stdout
This will probably cause problems when transfering the channels. This behavior was not reproducable with tclsh, only wish seems to be affected. | ||||
| User Comments: | ||||
JKU added on 2013-08-29 12:56:03:
The transfer of such channels will fail with the error With Tclsh, the new Thread has all 3 std* channels. JKU added on 2013-08-29 13:35:53:
Tclsh, not the intended behavior:
% interp create
interp0
% package r Thread
2.6.7
% set tid [thread::create]
tid000019C8
% thread::send $tid {close stdout}
puts hello
error writing "stdout": bad file number
serhiy.storchaka added on 2026-10-14 18:14:04:
The first part is the documented behavior. In wish on Windows a new thread has no standard channels, and as described in Tcl_StandardChannels(3), "the first three Tcl channels then opened by the application are designated as the Tcl standard channels"; a standard channel cannot be transferred. On Linux, and in tclsh on Windows (as you wrote), the new thread gets its own stdin, stdout and stderr. The second part is expected too: the standard channels of all threads refer to the same standard handles of the process, so closing stdout in one thread closes the process's standard output, just as closing stdout does in a single-threaded program. JKU added on 2026-10-14 19:40:19:
"the standard channels of all threads refer to the same standard handles of the process" As shown in the reproducer, this is not true at all - new channels in other threads may refer to different channels, even if they have the same name. Closing those does NOT close the process standard input. serhiy.storchaka added on 2026-10-14 19:53:36:
You are right, that sentence was too general. In wish on Windows the standard channel names in a new thread are just given to the first channels opened there (test.txt etc. in your example), so closing them closes only those channels. The sentence applies to tclsh (your second example): there stdout of every thread refers to the same file descriptor 1, so closing stdout in one thread breaks stdout of the main thread. That ordinary channels become standard channels in a thread without platform standard channels is still the behavior documented in Tcl_StandardChannels(3). Not doing that in threads other than the main thread would be a change of that behavior, which needs a decision. JKU added on 2026-10-14 20:05:01:
And that is my problem here:
This results in:
I did hope that there would be a discussion about this, with may result in (your pick):
or
I think that there might be some improvement in the way Tcl handles replacing std* channels and threads. The current behavior is documented, but... I am not really happy with it as it stands. serhiy.storchaka added on 2026-10-14 20:21:04:
Thank you, I understand now. This would change documented behavior, so it should be discussed on tcl-core first. | ||||
