Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-22
17:19 • Ticket [3611974fff] InitSubsystems multiple thread issue status still Closed... artifact: 7990758263 user: serhiy.storchaka
2026-10-15
11:39 • Closed ticket [3611974fff]. artifact: f960665630 user: serhiy.storchaka
2013-06-19
16:54 • Ticket [3611974fff]: 3 changes artifact: d3f09111a1 user: dgp
08:56 • Ticket [3611974fff]: 3 changes artifact: cd6ec2f1d8 user: jan.nijtmans
2013-06-18
15:26 • Ticket [3611974fff]: 3 changes artifact: 687729eb5f user: dgp
14:12 • Ticket [3611974fff]: 3 changes artifact: 193220186b user: jan.nijtmans
13:53 • Ticket [3611974fff]: 3 changes artifact: 986d50b0ec user: dgp
10:43 • Ticket [3611974fff]: 3 changes artifact: e8ffde2f96 user: jan.nijtmans
10:40 • Pending ticket [3611974fff]. artifact: f3ebee58c9 user: jan.nijtmans
10:36
Fix [3611974]: InitSubsystems multiple thread issue check-in: 33d17a1685 user: jan.nijtmans tags: trunk
10:33
Fix [3611974]: InitSubsystems multiple thread issue check-in: d155ff70cf user: jan.nijtmans tags: core-8-5-branch
2013-04-29
20:24 • Ticket [3611974fff] InitSubsystems multiple thread issue status still Open w... artifact: 22ba9e9859 user: dgp
04:11 • Ticket [3611974fff]: 4 changes artifact: 2286f54354 user: nijtmans
2013-04-27
02:02 • Ticket [3611974fff]: 4 changes artifact: 02e35028ca user: dgp
2013-04-26
18:55 • New ticket [3611974fff]. artifact: 4c1a81275e user: dgp

InitSubsystems multiple thread issue

Closed / Fixed3611974fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 41 days ago
Descriptionplain2013-04-26 18:55:49
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.
dgpplain2013-04-27 02:02:35
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.
nijtmansplain2013-04-29 04:11:10
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.
dgpplain2013-04-29 20:24:56
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.
jan.nijtmansplain2013-06-18 10:40:19
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?
jan.nijtmans Ticket [3611974fff]: 3 changes [History diff]2013-06-18 10:43:44
dgpplain2013-06-18 13:53:46
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.
jan.nijtmansplain2013-06-18 14:12:36
>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.
dgpplain2013-06-18 15:26:31
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.
jan.nijtmansplain2013-06-19 08:56:55
>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
dgpplain2013-06-19 16:54:41
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.
serhiy.storchakawiki2026-10-15 11:39:51

Fixed by [33d17a1685] (trunk) and [d155ff70cf] (8.5), released in 8.6.1 and 8.5.15.

serhiy.storchaka Ticket [3611974fff]: 3 changes [History diff]2026-10-22 17:19:27
Log in (anonymous is fine) to add a comment.