Artifacts Associated With Ticket 3611974
Ticket change [4c1a81275e] (rid 126820) by dgp on 2013-04-26 18:55:49:
- assignee initialized to: "nijtmans"
- cmimetype initialized to: "text/plain"
- comment initialized to:
The mutex arrangement in TclInitSubsystems() seems to suffer from a significant flaw. Consider two threads, A and B, calling the routine concurrently. Thread A gets the lock first, sets subsystemsInitialized to 1 and starts calling the set of subsystem initializing routines. Meanwhile thread B makes the initial test (subsystemsInitialized == 0) and since thread A has already set it to 1, thread B jumps straight to the bottom of TIS() and calls TclInitNotifier(). The trouble is when B calls TclInitNotifier(), we have no idea how much of Tcl library initialization is actually done. We just know it got started.
- foundin initialized to: "current: 8.5.14"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "dgp"
- subsystem initialized to: "02. Event Loops"
- title initialized to: "InitSubsystems multiple thread issue"
- type initialized to: "Bug"
Ticket change [02e35028ca] (rid 119728) by dgp on 2013-04-27 02:02:35:
- icomment:
Branch bug-3611974 committed: Revisions to the multi-thread arrangments of TclInitSubsystems(). With these changes, the caller of TclInitSubsystems() can be sure that at some point after the TIS() call is begun and before it returned, the Tcl library became fully initialized. Without these revisions, that cannot be guaranteed, TIS() can return with Tcl library initialization having never been finished. Even with these revisions, the caller cannot be sure the library remains initialized. Any thread might call Tcl_Finalize() at any time. That's a deeper problem which this patch does not attempt to address.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [2286f54354] (rid 130762) by nijtmans on 2013-04-29 04:11:10:
- icomment:
Two remarks: - Tcl_Finalize could be handled as well by using 4 states in stead of 3: CHANGING -> STARTING | FINISHING - If Tcl_CreateInterp wouldn't call TclInitSubsystems(), but in stead the application was required to call TclInitSubsystems() first, this problem wouldn't exist at all. But that's food for Tcl 9. Further on, I agree with this change. It doesn't really solve the problem, but a panic is better than leaving the system in an undefined state. I'll have a further look. - login: "nijtmans"
- mimetype: "text/plain"
- username: "nijtmans"
- icomment:
Ticket change [22ba9e9859] (rid 130805) by dgp on 2013-04-29 20:24:56:
- icomment:
Of course, once we make Tcl_InitSubsystems() a public routine that any extension might call at any time (buggy as that might be), the problem will come right back. The 4 state makes no functional difference that I can see, but I agree it feels a bit more polished that way. I'll commit that change to the branch. Thanks for looking it over. Working through this got me started thinking about ways to do this better, but as you say, that's a novem project. Will keep that in other branches and tickets.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [f3ebee58c9] (rid 132299) by jan.nijtmans on 2013-06-18 10:40:19:
- assignee changed to: "aku"
- closer changed to: "nobody"
- comment changed to:
The mutex arrangement in TclInitSubsystems() seems to suffer from a significant flaw. Consider two threads, A and B, calling the routine concurrently. Thread A gets the lock first, sets subsystemsInitialized to 1 and starts calling the set of subsystem initializing routines. Meanwhile thread B makes the initial test (subsystemsInitialized == 0) and since thread A has already set it to 1, thread B jumps straight to the bottom of TIS() and calls TclInitNotifier(). The trouble is when B calls TclInitNotifier(), we have no idea how much of Tcl library initialization is actually done. We just know it got started.
- icomment:
I think there is a much simpler solution, committed now to core-8-5-branch and trunk. The original code contains the comment: "Have to set this bit here to avoid deadlock with the routines below us that call into TclInitSubsystems.". That may have been true in the past, but currently none of the "routime below"'s call TclInitSubsystems(). This means that subsystemsInitialized can be set to 1 as final step, which fixes the whole thread issue. This means that branch "bug-3611974" is no longer necessary. Agreed?
- login: "jan.nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- severity changed to: "Minor"
- status changed to: "Pending"
Ticket change [e8ffde2f96] (rid 132300) by jan.nijtmans on 2013-06-18 10:43:44:
- assignee changed to: "dgp"
- login: "jan.nijtmans"
- mimetype: "text/plain"
Ticket change [986d50b0ec] (rid 132316) by dgp on 2013-06-18 13:53:46:
- icomment:
Thanks for pulling this back from the sidelines. The change now on 8.5 and trunk *might* be ok? It's hard to be certain. I find it easier to reason about a three- or four-state system than the two-state one you now have in place. You can check the history to be sure, but I think you've just restored what was in place many years ago, and I fear if it had to be changed before, it will have to be changed again. Also, why not have a system that's more robust against the routines called by TIS() and Tcl_Finalize() starting to misbehave? One particular concern I have with what's currently on the 8.5 and trunk branches is the timing of when Tcl_Finalize() sets subsystemsInitialized back to 0. With two threads concurrently attempting to finalize, the second will leap to the end of finalization while the first is still doing the teardown work. The three- or four-state scheme would avoid that.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [193220186b] (rid 132321) by jan.nijtmans on 2013-06-18 14:12:36:
- icomment:
>With two threads concurrently attempting to finalize, >the second will leap to the end of finalization while >the first is still doing the teardown work. I think that's what should happen: finalization should only happen once, it's doesn't really matter which thread actually does it. So, the thread that does the second Tcl_Finaliza() should skip over the section, and just assume that the other thread will finish it. The thing that still goes wrong is if one thread does an initialize while another thread starts a finalize when the other initialize is still running. A 4-state system with a panic would be better, but for the 8.x line I'm fine with just letting it crash. I'll check the history.
- login: "jan.nijtmans"
- mimetype: "text/plain"
- icomment:
Ticket change [687729eb5f] (rid 132322) by dgp on 2013-06-18 15:26:31:
- icomment:
It's fine to have only one thread carry out the finalization (I think, modulo the difficulty that multi-threaded finalization is still suffering from many flaws, AIUI). However the second thread that's leaping past finalization then lands on making a call to TclFinalizeLock(). That seems at least confusing and dangerous, if not outright wrong. Hmmmm... I see that the TclpInitLock() in Tcl_Finalize() lacks a balancing TclpInitUnlock() as well. It may be of little practical consequence, but I can follow the code on the bug-3611974 branch with far greater ease and confidence than what's currently in place. Unless I've really botched it without realizing it, I'd rather move to that.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [cd6ec2f1d8] (rid 132326) by jan.nijtmans on 2013-06-19 08:56:55:
- icomment:
>Hmmmm... I see that the TclpInitLock() in Tcl_Finalize() >lacks a balancing TclpInitUnlock() as well. No that's not a bug, because TclFinalizeLock() does an implicit unlock before destroying the lock. I agree that the code in the "bug-3611974" is better readable than what's in core-8-5-branch/trunk now, but I can see a bug which is not present in core-8-5-branch: In line 1234 a TclpInitUnlock() unlocks the lock, but the following TclFinalizeLock() should do that. Fixed now. It's difficult to get it right, "bug-3611974" is better readable but too risky to put in in 8.5 IMHO
- login: "jan.nijtmans"
- mimetype: "text/plain"
- icomment:
Ticket change [d3f09111a1] (rid 132333) by dgp on 2013-06-19 16:54:41:
- icomment:
That's fair enough, and your commit to 8.5 and trunk appears to me to at least be an improvement on the issue I raised originally. The bug-3611974 branch might still find some use on novem, or an 8.7 branch if any comes to pass. Just curious about the TclpInitUnlock() detail. Is it actually broken to have the sequence TclpInitUnlock() TclFinalizeLock() or is it just suboptimal? If it's actually broken, then I'm curious if there's any remaining brokenness in the possibility that multiple threads might call TclFinalizeLock(). Thanks. - login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [f960665630] (rid 258154) by serhiy.storchaka on 2026-10-15 11:39:51:
- icomment:
Fixed by [33d17a1685] (trunk) and [d155ff70cf] (8.5), released in 8.6.1 and 8.5.15.
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- icomment:
Ticket change [7990758263] (rid 259946) by serhiy.storchaka on 2026-10-22 17:19:27:
- closedate changed to: "2461328.986016435"
- closer changed to: "serhiy.storchaka"
- login: "serhiy.storchaka"