Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-14
20:21 • Ticket [e8efa378a9] Duplicate std* channels in different Threads status stil... artifact: e9207a7218 user: serhiy.storchaka
20:05 • Ticket [e8efa378a9]: 3 changes artifact: c65c24cddc user: JKU
19:53 • Ticket [e8efa378a9]: 3 changes artifact: cd8a690d99 user: serhiy.storchaka
19:40 • Open ticket [e8efa378a9]. artifact: c119671225 user: JKU
18:14 • Closed ticket [e8efa378a9]. artifact: 0006c77edc user: serhiy.storchaka
2013-08-30
15:07 • Ticket [e8efa378a9]: 3 changes artifact: 3d829265d5 user: dgp
2013-08-29
13:35 • Ticket [e8efa378a9]: 3 changes artifact: ef365875ea user: JKU
12:56 • Ticket [e8efa378a9]: 4 changes artifact: b347794474 user: JKU
12:47 • Ticket [e8efa378a9]: 3 changes artifact: 9e671dd620 user: JKU
12:44 • New ticket [e8efa378a9]. artifact: 56c6207321 user: JKU

Duplicate std* channels in different Threads

Opene8efa378a96bca5e69e00e5feff1315c932ed47bCreated 13 years agoUpdated 49 days ago
Descriptionwiki2013-08-29 12:44:17
When I create/open a channel in an other Thread (created with thread::create) the names of the first 3 opened channels are stdin, stdout and stderr, as described on the Tcl_StandardChannels manual page.
% 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.

JKU Ticket [e8efa378a9]: 3 changes [History diff]2013-08-29 12:47:38
JKUwiki2013-08-29 12:56:03

The transfer of such channels will fail with the error channel is shared.

With Tclsh, the new Thread has all 3 std* channels.

JKUwiki2013-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

dgp Ticket [e8efa378a9]: 3 changes [History diff]2013-08-30 15:07:19
serhiy.storchakawiki2026-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.

JKUplain2026-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.storchakawiki2026-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.

JKUmarkdown2026-10-14 20:05:01

And that is my problem here:

  • New channels in other threads may get std* names.
  • Those channels can not be transferred to other threads.
  • Related, but maybe a different bug: closing std* channels closes the underlying fd/handle, but leaves the channel hanging.

This results in:

  • Extra code may be needed to "occupy" the channel names if a channel should be created with the intention to transfer it later.
  • Complicated mental model around standard channels.

I did hope that there would be a discussion about this, with may result in (your pick):

  • All std* refer to the same underlying fd/handle, regardless if executed on wish/tclsh.
  • Closing std* channels makes them disappear in all threads.
  • Creating new channels does not name them std* if an other thread already has a channel with that name.

or

  • Channels created with a name like std* are shared across all threads.

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.storchakawiki2026-10-14 20:21:04

Thank you, I understand now. This would change documented behavior, so it should be discussed on tcl-core first.

Log in (anonymous is fine) to add a comment.