Skip to content
Tcl Source Code History Of Ticket 3026061fffffffff
Login

History Of Ticket 3026061fffffffff

Artifacts Associated With Ticket 3026061fffffffff

  1. Ticket change [aaee8e4eb2] (rid 101009) by a_kovalenko on 2010-07-06 22:42:00:

    1. assignee initialized to: "vasiljevic"
    2. cmimetype initialized to: "text/plain"
    3. comment initialized to:
      This bug is a member of "rare segfaults on exit" family. However I've found a way to reproduce it reliably:
      
      #!/usr/bin/wish8.6
      # The _wish_ interpreter is mandatory for reproducing the bug using this script: 
      # the example below depends on Tk_MainEx() being called.
      package req Thread
      package req Tk
      # Create thread pool
      set tph [tpool::create]
      # Post a long-running job
      tpool::post -nowait -detached $tph "after 5000; 
        [list thread::send -async [thread::id] {puts "foo bar, yep?"}];"
      # Make Tk/wish exit earlier than the job finishes
      after 3000 {destroy .}
      # And then it segfaults.
      
      What's going on?
      1. On initial `package require Thread', threadCmd.c:Init() is called for the main interpreter. Interps for other threads are created with threadCmd.c:NewThread(), only the main one is not _created_ but _registered_ with Init(). NewThread() takes care to Tcl_Preserve/Tcl_Release the interp when appropriate, but Init() doesn't. Pointer to the main interp is now stored in tsdPtr, but there is no Tcl_Preserve call to prevent its destruction, nor Tcl_CallWhenDeleted handler to wipe the pointer when the interp is destroyed. BAD.
      
      2. Thread pool is created and the job is posted, so a worker thread is created and NewThread is started in it. No problem here.
      
      3. Before the job finishes, main window is destroyed, and Tk_MainEx is going to exit.
      
      4. Tk_MainEx marks the main interp for deletion, then calls Tcl_Release on it, causing its actual deletion. From looking at Tk_MainEx I decided that it has the right to do what it does - that's why this bug is submitted against Thread package, not Tk. 
      
      4.1. At this point, tsdPtr of thread package has a dangling reference to a corpse of main interp. BAD.
      
      5. Then Tk_MainEx calls Tcl_Exit(0), which in turn invokes application exit handlers.
      
      6. ThreadPoolCmd.c:AppExitHandler() tries to destroy the thread pool gracefully. It causes a worker thread to exit and then waits for it, calling Tcl_DoOneEvent() repeatedly until there is no workers.
      
      7. But, _before_ the worker thread exits, it posts an event to the main thread, asking it to eval "puts..." within its main interp (that doesn't exist).
      
      8. threadCmd.c:ThreadEventProc() retrieves a dangling interp pointer from main thread's tsd.
      
      9. When it comes to Tcl_ResetResult(dead interp), wish segfaults.
      
      A possible solution, not nessesary the best one, is to add a couple of lines into threadCmd.c:Init():
       Tcl_Preserve(interp);
       Tcl_CreateThreadExitHandler(Tcl_Release, interp);
      
      Another possible solution is to assign NULL to tsdPtr->interp when interp is deleted (Tcl_CallWhenDeleted).
      
      
    4. foundin initialized to: "None"
    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: "a_kovalenko"
    10. subsystem initialized to: "80. Thread Package"
    11. title initialized to:
      threadCmd.c:Init() references an interp but not preserves it
      
    12. type initialized to: "Bug"
  2. Ticket change [a585b38634] (rid 110390) by vasiljevic on 2010-07-07 14:59:04:

    1. icomment:
      Hey, good analysis! Thank you for going to that extent.
      I will look into this.
      Cheers
      Zoran
      
      
    2. login: "vasiljevic"
    3. mimetype: "text/plain"
    4. username: "vasiljevic"
  3. Ticket change [11edc3bbe5] (rid 95226) by vasiljevic on 2010-09-29 02:16:22:

    1. closedate changed to: "2010-09-28T19:16:22"
    2. closer changed to: "vasiljevic"
    3. icomment:
      I have cleared the tsdPtr->interp on thread exit so I hope this should fix the problem. I used the sample script above and it did not crash my interp. The fix will appear in 2.6.7 patch release. Please update your sandbox and run the example again and tell me if this solves the problem for you as well. I will keep the ticket open until I hear from you (at least couple of days).
      
      
      
    4. login: "vasiljevic"
    5. mimetype: "text/plain"
    6. resolution changed to: "Accepted"
    7. status changed to: "Pending"
    8. username: "vasiljevic"
  4. Ticket change [355a717d57] (rid 106910) by sf-robot on 2010-10-13 02:20:04:

    1. closedate changed to: "2010-10-12T19:20:04"
    2. closer changed to: "sf-robot"
    3. icomment:
      This Tracker item was closed automatically by the system. It was
      previously set to a Pending status, and the original submitter
      did not respond within 14 days (the time period specified by
      the administrator of this Tracker).
      
      
    4. login: "sf-robot"
    5. mimetype: "text/plain"
    6. status changed to: "Closed"
    7. username: "sf-robot"