Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 43254c9383dca19e8e8b7955340abc7882976763411705ccd568586c0309e047
Ticket: f583715154c6f33d621418e8c9f6dfae5cc5ebc4
Fileevent runs in the wrong thread
User & Date: sebres on 2020-03-31 14:21:31
Changes

  1. icomment:
    Although suggested fix seems to work, but I am unsure we really need to reassign the async-callback handler (Tcl_DeleteFileHandler/Tcl_CreateFileHandler) always, I'm trying to figure better solution out.
    
    Let alone 8.5 seems to handle that correctly (and I don't see such handler reaction there).
    
    As for suggested test, it is too sporadic on my side (I guess the helper thread mostly accomplished the context switch of the channel on my machine), so here is a bit better reproducible test script (that also repeats connection up to 100 times):
    <code><pre style="padding-left:10pt">
    set ::helper [thread::create]
    set ::master [thread::id]
    puts "Main thread:\t\t $::master"
    puts "Helper thread:\t\t $::helper"
    
    set ::count 0
    thread::send -async $::helper [list set ::master $::master]
    thread::send -async $::helper {
      set ::helper [thread::id]
      proc iteration {} {
        set fd [socket -async 127.0.0.1 0]
        thread::detach $fd
        thread::send -async $::master [list test $fd]
      }
      iteration
    }
    proc test {fd} {
      thread::attach $fd
      fileevent $fd writable [list apply {{fd} {
        puts "Fileevent thread:\t [thread::id]"
        if {[thread::id] ne $::master} {
          thread::send -async $::master {puts "ERROR: invalid thread,\t $::master is expecting"; set ::count 100}
          exit; # return
        }
        close $fd
        incr ::count
        thread::send -async $::helper iteration
      }} $fd]
    }
    puts **wait**
    while {$::count < 100} { vwait ::count }
    thread::release -wait $::helper
    </pre></code>
    
  2. login: "sebres"
  3. mimetype: "text/x-fossil-wiki"