Skip to content
Tcl Source Code History Of Ticket b78d94c049af5090
Login

History Of Ticket b78d94c049af5090

Artifacts Associated With Ticket b78d94c049af5090

  1. Ticket change [2e20980c3a] (rid 237644) by aleteoryx on 2025-02-22 03:01:37:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-markdown"
    4. comment initialized to:
      The following code panics with "Tcl_RegisterChannel: duplicate channel names".
      
      ```
      package require Thread
      
      close stdin
      
      set pool [tpool::create -initcmd {
        open tmpfile w
      }]
      tpool::post $pool {}
      ```
      
    5. foundin initialized to: "8.6"
    6. is_private initialized to: "0"
    7. login: "aleteoryx"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "f1b08526abe45bf5e2e4fd8f848cb4cffa73bca6"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "aleteoryx"
    14. subsystem initialized to: "49. Threading"
    15. title initialized to:
      Thread pool panics if stdin closed and IO performed in initcmd
      
    16. type initialized to: "Bug"
  2. Ticket change [3ceb85c1ce] (rid 237645) by aleteoryx on 2025-02-22 03:03:54:

    1. comment changed to:
      The following code panics with "Tcl_RegisterChannel: duplicate channel names". The IO call in the initcmd does not matter, and 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 {}
      ```
      
    2. login: "aleteoryx"
    3. mimetype: "text/plain"
  3. Ticket change [5d2a245d14] (rid 237646) by aleteoryx on 2025-02-22 04:11:54:

    1. comment changed to:
      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 {}
      ```
      
    2. foundin changed to: "8.6 - Linux"
    3. login: "aleteoryx"
    4. mimetype: "text/plain"
  4. Ticket change [09c76b1953] (rid 237647) by sebres on 2025-02-22 13:17:02:

    1. closedate changed to: "2460729.05350073"
    2. closer changed to: "sebres"
    3. icomment:
      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|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.<br/>
      For instance, trying something like this in tcl-shell, you'd see that read side of pipe channels becomes (new) stdin:
      <code><pre>
          % close stdin; puts [chan pipe]
          stdin file3
      </pre></code>
      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/|thread-repository].
      
    4. login: "sebres"
    5. mimetype: "text/x-fossil-wiki"
    6. resolution changed to: "Invalid"
    7. status changed to: "Closed"
  5. Ticket change [1bef83fa77] (rid 237648) by chw on 2025-02-22 14:54:15:

    1. closedate changed to: "2460729.12101016"
    2. closer changed to: "chw"
    3. icomment:
      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().
      
    4. login: "chw"
    5. mimetype: "text/plain"
  6. Ticket change [a3b8b75769] (rid 237649) by chw on 2025-02-22 14:54:39:

    1. closer changed to: "nobody"
    2. login: "chw"
    3. mimetype: "text/plain"
    4. status changed to: "Pending"
  7. Ticket change [a836a35ad5] (rid 237650) by chw on 2025-02-22 15:18:18:

    1. closedate changed to: "2460729.1377135"
    2. closer changed to: "chw"
    3. icomment:
      There's now an open ticket
      
      https://core.tcl-lang.org/thread/info/8c7d212ebece48e3
      
      in the thread package for further discussion.
      
    4. login: "chw"
    5. mimetype: "text/plain"
    6. status changed to: "Closed"
  8. Ticket change [1664d33697] (rid 237820) by sebres on 2025-02-24 23:20:20:

    1. closedate changed to: "2460731.47246301"
    2. closer changed to: "sebres"
    3. icomment:
      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:
      <ul>
      <li>the standard channels (by their names) are "shared" across the threads, however not the instances of them (which are quasi duplicates each thread);</li>
      <li>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)</li>
      <li>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</li>
      <li>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.</li>
      <li>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.</li>
      </ul>
      
      Basically the proper and consistent solution can be:
      <ol>
      <li>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;</li>
      <li>or fully independent standard channels across the threads (may be again platform-specific thing or at least backwards incompatible);</li>
      <li>and/or complete avoidance of implicit reassignment of standard channels by open after close (need then some command to do that explicitly if wanted).</li>
      </ol>
      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.
      
    4. login: "sebres"
    5. mimetype: "text/x-fossil-wiki"