Tcl Source Code

View Ticket
Login
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 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.

User Comments:
JKU added on 2013-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.


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:

  • 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.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.