Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 7ec2e0605d5625d53eb3a1c0c315063ebd639c97e7bc37c63b323b0ad51f294f
Ticket: f583715154c6f33d621418e8c9f6dfae5cc5ebc4
Fileevent runs in the wrong thread
User & Date: sbron on 2020-01-10 12:14:24
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    The script shown below sets up an async socket in a helper thread and then transfers the channel to the main thread. In the main thread, a writable file event handler is then configured on the channel. However, when this event handler is invoked, it runs in the helper thread rather than the main thread.
    
    <verbatim>
    package require Thread
    set tid [thread::create]
    
    puts "Main thread: [thread::id]"
    puts "Helper thread: $tid"
    
    set fd [thread::send $tid {
        set fd [socket -async 8.8.8.8 53]
        thread::detach $fd
        set fd
    }]
    
    thread::attach $fd
    fileevent $fd writable {
        puts "Fileevent thread: [thread::id]"
        exit
    }
    
    vwait forever
    </verbatim>
    
    The output of the script is something like:<verbatim>
    Main thread: tid0x7f868f4b5680
    Helper thread: tid0x7f868ddbb700
    Fileevent thread: tid0x7f868ddbb700
    </verbatim>
    
    System: openSUSE Leap 15.1 x86_64
    
    I initially opened this issue against the thread extension ([https://core.tcl-lang.org/thread/tktview/d481f15516|d481f15516]). However, it appears to be an issue in the Tcl core caused by the use of an internal event handler to deal with the async connect.
    
    I believe this is a different issue than [https://core.tcl-lang.org/tcl/info/815e246806|815e246806].
    
    I created a patch that reconfigures the internal event handler in the new thread when the channel is transferred. It fixes the problem in all scenarios I could come up with sofar.
    
  5. foundin changed to: "trunk (9.0a2)"
  6. is_private changed to: "0"
  7. login: "sbron"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "8475f421649231f1e116ac602bea4537d2b027b4"
  10. resolution changed to: "None"
  11. severity changed to: "Severe"
  12. status changed to: "Open"
  13. submitter changed to: "sbron"
  14. subsystem changed to: "49. Threading"
  15. title changed to: "Fileevent runs in the wrong thread"
  16. type changed to: "Bug"