Skip to content
Tcl Source Code History Of Ticket 17f747a4a4
Login

History Of Ticket 17f747a4a4

Artifacts Associated With Ticket 17f747a4a4

  1. Ticket change [f7ed631201] (rid 158804) by chw on 2017-11-21 05:48:03:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      There's a static cache of Tcl_Obj pointers (e.g. "zipfs_literal_fstype") to cache
      commonly used strings. This seems to be wrong since it potentially is used by
      more than a single thread and lacks synchronization.
      
      Either must that cache be left out or implemented using thread local storage as
      in the Tcl_SetStartupScript() function.
      
    5. foundin initialized to: "core_zip_vfs"
    6. is_private initialized to: "0"
    7. login: "chw"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "0b3aff16612e1e9f3b675963fa64e01bd1d89307"
    10. resolution initialized to: "None"
    11. severity initialized to: "Critical"
    12. status initialized to: "Open"
    13. submitter initialized to: "chw"
    14. subsystem initialized to: "49. Threading"
    15. title initialized to: "Thread safety in tclZipfs.c"
    16. type initialized to: "Bug"
  2. Ticket change [6555b849b4] (rid 158823) by hypnotoad on 2017-11-21 10:35:10:

    1. icomment:
      Considering these are constants that are only generated once in a process lifetime, nothing should be changing them once set. Tcl_SetStartupScript needs to be thread aware because the startup script for a thread could very well be different than for the main process.
      
      Can you demonstrate this actually causing a problem?
      
    2. login: "hypnotoad"
    3. mimetype: "text/plain"
  3. Ticket change [e8fa2ec9ae] (rid 158824) by chw on 2017-11-21 11:30:46:

    1. icomment:
      I can't demonstrate it, however, ask if Tcl_DecrRefCount() and Tcl_IncrRefCount()
      are thread safe. AFAIK, they are not. And this might be the real underlying problem.
      
    2. login: "chw"
    3. mimetype: "text/plain"
  4. Ticket change [15831dc015] (rid 158825) by hypnotoad on 2017-11-21 12:05:46:

    1. icomment:
      There was a lively discussion on stackoverflow on the subject:
      
      https://stackoverflow.com/questions/28462414/alloc-invalid-block-are-tcl-incrrefcount-and-tcl-decrrefcount-thread-safe-for
      
      The long and short answer is that Tcl_IncrRefCount is perfectly Thread safe. Passing values around between threads... no to much. Mostly to do with shimmering. 
      
      On one hand these are marked as shared objects, and anyone who was going to modify them should clone them and modify a copy. On the other... if someone does manage to modify them it will be hell to figure out what is going on.
      
      Let's meet halfway. Instead of storing Tcl_Objs, let's store constant strings. And all of the functions that return Tcl_Objs will produces a fresh copy of those strings.
      
    2. login: "hypnotoad"
    3. mimetype: "text/plain"
  5. Ticket change [854e252600] (rid 158826) by hypnotoad on 2017-11-21 12:07:31:

    1. icomment:
      It might also be worth a quick read of tclLiteral.c to see how things are done in there.
      
    2. login: "hypnotoad"
    3. mimetype: "text/plain"
  6. Ticket change [e000540d53] (rid 158827) by chw on 2017-11-21 13:45:47:

    1. icomment:
      Maybe I'm missing something here, but the decrement in 
      
       #define Tcl_DecrRefCount(objPtr) \
         do { \
           Tcl_Obj *_objPtr = (objPtr); \
           if ((_objPtr)->refCount-- <= 1) { \
             TclFreeObj(_objPtr); \
         } \
       } while(0)
      
      is not guaranteed to be atomic. No gcc/clang trickery,
      no Microsoften InterlockedDecrement()ery.
      
      Thus, yes, please Tcl_NewString() from constant strings.
      
    2. login: "chw"
    3. mimetype: "text/plain"
  7. Ticket change [5c50abc1eb] (rid 158828) by sebres on 2017-11-21 14:17:40:

    1. icomment:
      Sharing of Tcl_Obj between different threads is a very bad idea (and not allowed in standard tcl-core).
      And this is not necessarily a thing of atomic refCount incr/decr (resp. object modifications) - default tcl-core has neither a GIL, nor a multiple internal representation.
      
      This meant: one thread can make an integer from the object, another makes unicode string or list (with another internal representation).
      Meaning the object changes internally and this will produce a BOOM (segfaults and co.).
      
      Don't do such kind of sharing. Therefore:
      
      - either move the object completely to another thread (take over the ownership);
      - or make a duplicate (using Tcl_DuplicateObj) in order to supply it to different threads;
      - or create each time a new object by the command exec as suggested from @chw.
      
    2. login: "sebres"
    3. mimetype: "text/plain"
  8. Ticket change [949d54e17a] (rid 158834) by hypnotoad on 2017-11-21 14:24:34:

    1. icomment:
      I removed the pool of Tcl_Objs, and replaced with a single pointer to a constant string once the discovery process has run. Functions that return a Tcl_Obj run a fresh Tcl_NewStringObj every time against the constant string.
      
      Checkin: [9f0abfb1ac]
      
    2. login: "hypnotoad"
    3. mimetype: "text/plain"
    4. resolution changed to: "Fixed"
  9. Ticket change [a9a95c1b7a] (rid 257999) by serhiy.storchaka on 2026-10-14 13:37:35:

    1. icomment:
      Fixed by [9f0abfb1ac]: the static Tcl_Obj literals were replaced by C strings. The fix is in all 9.0 releases.
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
    4. resolution changed to: "Fixed"
    5. status changed to: "Closed"
  10. Ticket change [7b34846580] (rid 259909) by serhiy.storchaka on 2026-10-22 17:19:25:

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