Skip to content
Tcl Source Code History Of Ticket 3608957fff
Login

History Of Ticket 3608957fff

Artifacts Associated With Ticket 3608957fff

  1. Ticket change [18eed8d12c] (rid 122740) by sbron on 2013-03-24 16:15:27:

    1. assignee initialized to: "kennykb"
    2. cmimetype initialized to: "text/plain"
    3. comment initialized to:
      My C library posts an event via Tcl_QueueEvent when the app receives a signal (like SIGHUP). When using the library with a non-threaded tclsh, the event is picked up by the event loop and processed almost immediately. However, with a threaded tclsh nothing happens until some other event occurs. At that time the queued events and the other event are all processed. Is this the intended behavior?
      
      Issue found on OpenSUSE 12.3 (linux 3.7.10) 64-bit.
      
      I have attached the stripped down sources for a library that reproduces the problem. Run the script and send a SIGHUP to the process. Observe that with a non-threaded tclsh the script reports "signal" immediately. With a threaded tclsh the "signal" report comes out just before the "Boo!".
      
      
    4. foundin initialized to: "current: 8.6.0"
    5. is_private initialized to: "0"
    6. priority initialized to: "5 Medium"
    7. resolution initialized to: "None"
    8. status initialized to: "Open"
    9. submitter initialized to: "sbron"
    10. subsystem initialized to: "01. Notifier"
    11. title initialized to: "Events posted by Tcl_QueueEvent linger in queue"
    12. type initialized to: "Bug"
  2. Add attachment "sighup.zip" [8487d21558] (rid 117567) by sbron on 2013-03-24 23:15:28.

  3. Ticket change [89c40be4d4] (rid 127282) by sbron on 2013-03-24 23:15:28:

    1. icomment:
      
      
      File Added - 461908: sighup.zip
      
      
      
    2. login: "sbron"
    3. mimetype: "text/plain"
    4. username: "sbron"
  4. Ticket change [e7c665d8ed] (rid 104526) by jenglish on 2013-03-24 23:39:52:

    1. icomment:
      The signal handler in the attached code calls ckalloc() and Tcl_QueueEvent(), neither of which are async-signal safe.
      
      Probable cause of problem in threaded build is that nothing calls Tcl_AlertNotifier(), so the event loop never wakes up.  Note that Tcl_AlertNotifier() isn't async-signal safe either.
      
      Recommended approach is to use Tcl_CreateFileHandler and the write-to-pipe technique.
      
      
      
    2. login: "jenglish"
    3. mimetype: "text/plain"
    4. username: "jenglish"
  5. Ticket change [d4fcae096f] (rid 123539) by sbron on 2013-03-25 04:23:38:

    1. icomment:
      Thanks for your suggestions Joe, although I don't know what "the write-to-pipe technique" is in this context.
      
      I switched to Tcl_AsyncMark for my library, because apparently that command was specifically designed to handle cases like this and it seems to work just fine.
      
      If the consensus is that the difference in behavior of Tcl_QueueEvent in threaded and non-threaded builds is not a bug then I don't have objections to closing this tracker item.
      
      
      
    2. login: "sbron"
    3. mimetype: "text/plain"
    4. username: "sbron"
  6. Ticket change [1d26088482] (rid 90747) by jenglish on 2013-03-25 08:00:07:

    1. icomment:
      The idea behind write-to-pipe is: use pipe(2) to get a pair of connected file descriptors; in the event loop listen on the read end (with select, poll, kqueue, whatever), and in the signal handler write a single byte (generally the signal number) to the write end.  That will wake up the event loop, which can read the pipe to determine which signal was sent.
      
      write(2) is async-signal safe, whereas almost none of the pthread_* routines are.  That's why this approach is preferred.
      
      > I switched to Tcl_AsyncMark for my library, because apparently
      >  that command was specifically designed to handle cases like this 
      
      That may have been the original intent, but since the first thing Tcl_AsyncMark() does is lock a mutex, it can't be called from a signal handler either.
      
      > and it seems to work just fine.
      
      "seems" being the operative word here :-)  If a signal ever gets delivered while Tcl is in the middle of an AsyncInvoke or other critical section, the process will deadlock.  
      
      Also beware: async handlers are called at very precarious times; the interp can be in almost any state, you have to be very careful.  (Scheduling a callback with Tcl_QueueEvent is about the only thing you should do in an async handler).
      
      
    2. login: "jenglish"
    3. mimetype: "text/plain"
    4. username: "jenglish"
  7. Ticket change [e532833037] (rid 88088) by sbron on 2013-03-25 14:57:30:

    1. icomment:
      Then the Tcl_AsyncMark manual page is misleading. It says: "These procedures provide a safe mechanism for dealing with asynchronous events such as signals". It would be good to note there that the mechanism is not entirely safe.
      
      
    2. login: "sbron"
    3. mimetype: "text/plain"
    4. username: "sbron"
  8. Ticket change [53fdb39fa9] (rid 93858) by dkf on 2013-03-25 19:18:40:

    1. icomment:
      Part of the problem was that the mechanism was originally (in the distant past) designed specifically for signal handling. It was also abused quite a bit for passing in messages from other threads (especially in non-threaded-Tcl in threaded-app situations) which is a very different use.
      
      What I want to see is some way to improve signal delivery. Some signals probably should just go on the event queue (SIGWINCH anyone?) but others are more urgent (SIGINT) and some just lethal (SIGSEGV+SIGBUS are unlikely to ever be recoverable from by script). But we may have to throw some of the babies under the bus to make this happen; it's very messy and I've never 100% understood it (or certainly not enough to grok that I *do* understand it).
      
      
    2. login: "dkf"
    3. mimetype: "text/plain"
    4. username: "dkf"
  9. Ticket change [978587a5a9] (rid 106508) by anonymous on 2013-04-03 17:58:52:

    1. icomment:
      @Joe,
      can you provide an example of how to use the write-to-pipe technique.
      Thanx,
      Nicolas
      
      
    2. login: ""
    3. mimetype: "text/plain"
    4. username: ""
  10. Ticket change [ac9d5469b0] (rid 101331) by davygrvy on 2013-04-05 16:42:24:

    1. icomment:
      Tcl_AsyncMark might be helpful here.  Not a complete solution, but part of a method
      
      
    2. login: "davygrvy"
    3. mimetype: "text/plain"
    4. username: "davygrvy"
  11. Ticket change [4df60aa318] (rid 259762) by serhiy.storchaka on 2026-10-21 10:09:08:

    1. icomment:
      <tt>Tcl_QueueEvent()</tt> does not wake the notifier and must not be called from a signal handler. Since 9.0 the right way is <tt>Tcl_AsyncMarkFromSignal()</tt> (TIP 511, [1a6b389968]) with the event queued from the async handler, and with the epoll/kqueue notifier the event is handled at once. Closing.
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
    4. resolution changed to: "Out of Date"
    5. status changed to: "Closed"
  12. Ticket change [42bb9dfe21] (rid 260049) by serhiy.storchaka on 2026-10-22 17:19:32:

    1. closedate changed to: "2461334.923014248"
    2. closer changed to: "serhiy.storchaka"
    3. login: "serhiy.storchaka"