Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 58eda89ce00edec7308fbeed20418631b2804a7e
Ticket: f4f44174e4c88a9132b19bcc4b68346a1d229fe1
protect notifier against signals
User & Date: aspect on 2017-01-01 13:35:12
Changes

  1. comment changed to:
    The following script (using Tclx) eventually hangs, as the signal is
    delivered either to the notifier thread or to Tcl_AlertNotifier.
    
    Patch coming.
    
    ~~~
    package require Tclx
    package require Thread
    
    signal trap SIGHUP handler
    
    proc handler args {
        puts "SIGHUP [incr ::HUPS] in [thread::id] $args"
    }
    
    proc gotinput {chan} {
        gets $chan d
        puts "hupity $d"
    }
    
    set sub [thread::create thread::wait]
    
    if {![catch {
        package require Tcl 8.6
    }]} {
        lassign [chan pipe] r w
        chan configure $w -buffering none
        chan configure $r -blocking 0
        chan event $r readable [list gotinput $r]
    
        thread::transfer $sub $w
        thread::send $sub [list set wchan $w]
    } else {
        thread::send $sub [list set wchan stdout]
    }
    
    thread::send -async $sub {
        package require Tclx
        set pid [pid]
        while 1 {
            kill HUP $pid
            puts $wchan [incr ::HUP]
        }
    } result
    
    vwait result
    ~~~
    
  2. icomment:
    > Signals and threads are two-component explosive by committee design, now I wonder what Angus McGyver would do.
    
    Possibly, fashion something out of a pipe(2)!  It took me a couple of tries, but I have something on branch [aspect-async-pipe] which I think mostly does the job.
    
    Prior to back-porting to 8.5 I thought it might entirely do the job, but same-thread notifications still need a mutex (Tcl_ThreadAlert(), damn that waitCV!).  However I think [tip-458] makes waitCV and notifierMutex redundant, so this will be allowed!  Maybe?
    
    Note that tcltest cannot test this without a way to install signal handlers that use Tcl_AsyncMark().
    
    Back to my pre-backport commentary, which is still mostly valid:
    ~~~
    
    The problem is that Tcl_AsyncMark goes under a mutex to deliver async events to other threads.  That is verboten by POSIX, and it's important that the asyncReady check remain cheap so we need to safely update a value in the target threads's memory .. what to do?
    
    In a delicious inversion of what I previously had in mind for signals, I have solved this with another thread.  AsyncMark *is* permitted to write(2), so cross-thread notifications now go down a new pipe to AsyncThreadProc().  All threads can use the same pipe, since (small enough) write(2)s are atomic.
    
    The new thread alternates between blocking read from the pipe, and doing the mutex dance to notify whoever needs an async event.
    
    There's a change in data structures too:  the per-thread list of AsyncHandlers is now a member of sharedData->handlersList (actually a list of lists).  This is so the token sent to AsyncThreadProc can be validated before we go dereferencing it to set async flags.  As a result, calling Tcl_AsyncMark() with a token that was deleted in another thread - or whose thread has exited - is now safe.
    
    Same-thread notifications simply set asyncReady without a mutex.  I think this is safe, as the worst thing that can happen concurrently is AsyncThreadProc setting asyncReady (with a mutex) and the observable outcome should always be asyncReady=1.  Pre-empting itself should always be safe.
    
    The test suite passes, and the script that started this ticket runs for 10e8s of signals without deadlock.  Of course, notifierMutex is still a problem, but we seem to be on the right track.
    
  3. login: "aspect"
  4. mimetype: "text/plain"