Skip to content

Commit 3b30937

Browse files
authored
gh-154923: Fix a crash when creating Tcl interpreters concurrently (GH-156342)
In the free-threaded build, two threads creating Tcl interpreters at the same time could both free the Tcl lock. Interpreter creation is now serialized.
1 parent a2d812e commit 3b30937

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎Lib/test/test_tcl.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
import subprocess
33
import sys
44
import os
5+
import threading
56
from test import support
67
from test.support import import_helper
78
from test.support import os_helper
9+
from test.support import threading_helper
10+
from test.support.isolation import runInSubprocess
811

912
# Skip this test if the _tkinter module wasn't built.
1013
_tkinter = import_helper.import_module('_tkinter')
@@ -33,6 +36,29 @@ def testFlattenLen(self):
3336
self.assertRaises(TypeError, _tkinter._flatten, 'string')
3437
self.assertRaises(TypeError, _tkinter._flatten, {'set'})
3538

39+
@unittest.skipUnless(support.Py_GIL_DISABLED,
40+
'requires the free-threaded build')
41+
@threading_helper.requires_working_threading()
42+
@runInSubprocess()
43+
def test_create_concurrent(self):
44+
# gh-154923: Create Tcl interpreters in different threads at once.
45+
# Run in a fresh process, where no interpreter was created yet.
46+
nthreads = 16
47+
start = threading.Barrier(nthreads)
48+
errors = []
49+
def worker():
50+
try:
51+
start.wait()
52+
for _ in range(8):
53+
_tkinter.create(None, '', 'Tk', False, 1, False, False,
54+
None)
55+
except Exception as e:
56+
errors.append(e)
57+
threads = [threading.Thread(target=worker) for _ in range(nthreads)]
58+
with threading_helper.start_threads(threads):
59+
pass
60+
self.assertEqual(errors, [])
61+
3662

3763
class TclTest(unittest.TestCase):
3864

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when creating Tcl interpreters concurrently in different
2+
threads in the :term:`free-threaded build`.

‎Modules/_tkinter.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ Tkapp_New(const char *screenName, const char *className,
681681
if (v == NULL)
682682
return NULL;
683683

684+
/* Without the GIL, two threads could both free tcl_lock below, and
685+
the first-time initialization in Tcl_CreateInterp() is not
686+
thread-safe either. Serialize them. */
687+
static PyMutex tcl_create_mutex;
688+
PyMutex_Lock(&tcl_create_mutex);
684689
v->interp = Tcl_CreateInterp();
685690
v->wantobjects = wantobjects;
686691
#if TCL_MAJOR_VERSION >= 9
@@ -695,6 +700,7 @@ Tkapp_New(const char *screenName, const char *className,
695700

696701
#ifndef TCL_THREADS
697702
if (v->threaded) {
703+
PyMutex_Unlock(&tcl_create_mutex);
698704
PyErr_SetString(PyExc_RuntimeError,
699705
"Tcl is threaded but _tkinter is not");
700706
Py_DECREF(v);
@@ -706,6 +712,7 @@ Tkapp_New(const char *screenName, const char *className,
706712
PyThread_free_lock(tcl_lock);
707713
tcl_lock = NULL;
708714
}
715+
PyMutex_Unlock(&tcl_create_mutex);
709716

710717
v->OldBooleanType = Tcl_GetObjType("boolean");
711718
{

0 commit comments

Comments
 (0)