Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-02-24
23:20 • Ticket [b78d94c049] Thread pool panics if stdin closed and IO performed in initc... artifact: 1664d33697 user: sebres
22:38
experimental, attempt to fix [b78d94c049af5090]: the state of the closed standard channel must be "h... leaf check-in: 93d9a20635 user: sebres tags: bug-b78d94c049af5090-closed-std-chan
2025-02-22
15:18 • Closed ticket [b78d94c049]: Thread pool panics if stdin closed and IO performed ... artifact: a836a35ad5 user: chw
14:54 • Pending ticket [b78d94c049]. artifact: a3b8b75769 user: chw
14:54 • Ticket [b78d94c049]: 5 changes artifact: 1bef83fa77 user: chw
13:17 • Closed ticket [b78d94c049]. artifact: 09c76b1953 user: sebres
04:11 • Ticket [b78d94c049]: 4 changes artifact: 5d2a245d14 user: aleteoryx
03:03 • Ticket [b78d94c049]: 3 changes artifact: 3ceb85c1ce user: aleteoryx
03:01 • New ticket [b78d94c049]. artifact: 2e20980c3a user: aleteoryx

Thread pool panics if stdin closed and IO performed in initcmd

Closed / Invalidb78d94c049af5090a9ef1c4aaa8059aa45b98b59Created 2 years agoUpdated 2 years ago
Descriptionmarkdown2025-02-22 03:01:37

The following code panics with "Tcl_RegisterChannel: duplicate channel names". The IO call in the initcmd does not matter; I discovered the issue with a package require call.

package require Thread

close stdin

set pool [tpool::create -initcmd {
  open tmpfile w
}]
tpool::post $pool {}
aleteoryx Ticket [b78d94c049]: 3 changes [History diff]2025-02-22 03:03:54
aleteoryx Ticket [b78d94c049]: 4 changes [History diff]2025-02-22 04:11:54
sebreswiki2025-02-22 13:17:02

It's not a bug, it's a documented feature... If the standard channels get closed, they will be reopen by next channel open command (chan/file/pipe/socket/whatever). Just in your case the unexpected thing - you do that not from main thread (and the channel is opened not for read)...

See https://stackoverflow.com/a/68869664 for more detailed answer to similar question.

No idea what are you trying to do by closing of stdin, but if it is something like signal to caller EOF, you can simply reopen new (dummy) stdin implicitly after `close stdin`, for instance with `chan pipe` command.
For instance, trying something like this in tcl-shell, you'd see that read side of pipe channels becomes (new) stdin:

    % close stdin; puts [chan pipe]
    stdin file3
Hereafter everything shall work as expected with the threads (and registering channels doesn't get confused).

By the way, this is Tcl- not thread-repository, so basically every thread-related stuff belongs to thread-repository.

chwplain2025-02-22 14:54:15
I believe this is a real legit bug, since the Thread package
still shouldn't produce a crash. The problem is, that the
thread functions of both the tpool::create and thread::create
command do not proper setup the TSD of the I/O subsystem
before calling Tcl_CreateInterp() etc. This might be
remedied by calling the Tcl_GetStdChannel() function for
all standard channels before Tcl_CreateInterp().
chw Pending ticket [b78d94c049]. [History diff]2025-02-22 14:54:39
chwplain2025-02-22 15:18:18
There's now an open ticket

https://core.tcl-lang.org/thread/info/8c7d212ebece48e3

in the thread package for further discussion.
sebreswiki2025-02-24 23:20:20

Well, it looks like this is indeed an issue of tcl (not the thread-module).

Here is the experimental commit that solves the issue - [93d9a20635]. Although I'm not quite convinced with this solution.

In general the usage of standard channels across threads is not well thought out in tcl-IO:

  • the standard channels (by their names) are "shared" across the threads, however not the instances of them (which are quasi duplicates each thread);
  • so every thread initializes its own instance, so that if a thread uses some of standard channel first time, it gets its own instance (for the same thing)
  • the close of standard channel in one thread will do that not for the other thread, if both already touched it, but new thread may miss it
  • the whole thing is additionally platform-specific (and may deviate e. g. by windows if it is closes in main thread, the new thread cannot obtain a new standard handle in new thread, on linux it'd be able to do that.
  • to handle it properly, one has to know how many threads using some standard channel (e. g. N threads in use, but M thread ever touched some channel), inclusive possible timing issues on some race condition.

Basically the proper and consistent solution can be:

  1. either real sharing of standard channels across the threads (and common global array of them), what would be not so simple since tcl doesn't know about "sharing" of channels at all;
  2. or fully independent standard channels across the threads (may be again platform-specific thing or at least backwards incompatible);
  3. and/or complete avoidance of implicit reassignment of standard channels by open after close (need then some command to do that explicitly if wanted).
In any case it needs a clear definition what shall happen multi-threaded by closing of standard channels (in "shared" and "unshared" state), because at the moment it is rather an UB, and "duplicate channel names" error is rather an after-effect.

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