From 5fa855ad90ba0e335dabf9589884c63b0c3056c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbhuvi27=E2=80=9D?= Date: Tue, 25 Aug 2026 09:24:48 +0530 Subject: [PATCH 1/3] gh-154923: Serialize Tcl interpreter creation in _tkinter Concurrent _tkinter.create() raced Tcl's first-time library init and could double-free tcl_lock on the free-threaded build. --- Lib/test/test_tcl.py | 36 +++++++++++++++++++ ...-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst | 2 ++ Modules/_tkinter.c | 13 +++++++ 3 files changed, 51 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py index b0458e46c6beced..9a9e7d0f5d2526a 100644 --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -5,6 +5,8 @@ from test import support from test.support import import_helper from test.support import os_helper +from test.support import script_helper +from test.support import threading_helper # Skip this test if the _tkinter module wasn't built. _tkinter = import_helper.import_module('_tkinter') @@ -33,6 +35,40 @@ def testFlattenLen(self): self.assertRaises(TypeError, _tkinter._flatten, 'string') self.assertRaises(TypeError, _tkinter._flatten, {'set'}) + @unittest.skipUnless(support.Py_GIL_DISABLED, + "gh-154923 is a free-threading race") + @threading_helper.requires_working_threading() + def test_create_concurrent(self): + # Concurrent _tkinter.create() races Tcl's first-time library + # init and can double-free tcl_lock (gh-154923). Run in a + # subprocess so Tcl has not already been initialized. + script = """\ +import _tkinter +import threading +nthreads = 4 +rounds = 8 +start = threading.Barrier(nthreads) +errors = [] + +def worker(): + try: + start.wait() + for _ in range(rounds): + app = _tkinter.create(None, '', 'Tk', False, 1, False, False, None) + del app + except Exception as e: + errors.append(repr(e)) + +threads = [threading.Thread(target=worker) for _ in range(nthreads)] +for t in threads: + t.start() +for t in threads: + t.join() +if errors: + raise SystemExit('\\n'.join(errors)) +""" + script_helper.assert_python_ok('-c', script, __isolated=False) + class TclTest(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst b/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst new file mode 100644 index 000000000000000..bd871d2a4189526 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst @@ -0,0 +1,2 @@ +Fix a data race when creating Tcl interpreters concurrently via +``_tkinter.create()`` on the :term:`free-threaded build`. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index f5fb1841f0d5940..195c169f6130977 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -291,6 +291,12 @@ Tkinter_TkInit(Tcl_Interp *interp) static PyThread_type_lock tcl_lock = 0; +/* Serializes Tcl_CreateInterp() and the one-time tcl_lock free. + tcl_lock itself cannot be used for that: it is discarded once Tcl is + threaded, and Tcl's first CreateInterp lazily initializes process-wide + mutexes that are not safe to race (gh-154923). */ +static PyMutex tcl_create_mutex = {0}; + #ifdef TCL_THREADS static Tcl_ThreadDataKey state_key; #define tcl_tstate \ @@ -640,6 +646,11 @@ Tkapp_New(const char *screenName, const char *className, if (v == NULL) return NULL; + /* Tcl's first CreateInterp() runs process-wide lazy init (including + Tcl_MutexLock initializing a static mutex). Concurrent callers race + that init under free-threading; two threads can also both free + tcl_lock. Serialize both (gh-154923). */ + PyMutex_Lock(&tcl_create_mutex); v->interp = Tcl_CreateInterp(); v->wantobjects = wantobjects; #if TCL_MAJOR_VERSION >= 9 @@ -654,6 +665,7 @@ Tkapp_New(const char *screenName, const char *className, #ifndef TCL_THREADS if (v->threaded) { + PyMutex_Unlock(&tcl_create_mutex); PyErr_SetString(PyExc_RuntimeError, "Tcl is threaded but _tkinter is not"); Py_DECREF(v); @@ -665,6 +677,7 @@ Tkapp_New(const char *screenName, const char *className, PyThread_free_lock(tcl_lock); tcl_lock = NULL; } + PyMutex_Unlock(&tcl_create_mutex); v->OldBooleanType = Tcl_GetObjType("boolean"); { From bd0c6623213b1cef61ed1020b356499a22dd7e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbhuvi27=E2=80=9D?= Date: Tue, 25 Aug 2026 09:40:15 +0530 Subject: [PATCH 2/3] gh-154923: Make tcl_create_mutex a local static The c-globals checker rejects non-constant module-level globals, but allows local static mutexes used to wrap non-thread-safe C APIs. --- Modules/_tkinter.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 195c169f6130977..73769243084692a 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -291,12 +291,6 @@ Tkinter_TkInit(Tcl_Interp *interp) static PyThread_type_lock tcl_lock = 0; -/* Serializes Tcl_CreateInterp() and the one-time tcl_lock free. - tcl_lock itself cannot be used for that: it is discarded once Tcl is - threaded, and Tcl's first CreateInterp lazily initializes process-wide - mutexes that are not safe to race (gh-154923). */ -static PyMutex tcl_create_mutex = {0}; - #ifdef TCL_THREADS static Tcl_ThreadDataKey state_key; #define tcl_tstate \ @@ -650,6 +644,7 @@ Tkapp_New(const char *screenName, const char *className, Tcl_MutexLock initializing a static mutex). Concurrent callers race that init under free-threading; two threads can also both free tcl_lock. Serialize both (gh-154923). */ + static PyMutex tcl_create_mutex; PyMutex_Lock(&tcl_create_mutex); v->interp = Tcl_CreateInterp(); v->wantobjects = wantobjects; From 7331f2a30b00c982ca90dbc2c82f047444e12f7d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 25 Nov 2026 19:31:37 +0200 Subject: [PATCH 3/3] Clarify the comment and NEWS, and use runInSubprocess in the test Co-Authored-By: Claude Opus 5.5 --- Lib/test/test_tcl.py | 52 ++++++++----------- ...-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst | 4 +- Modules/_tkinter.c | 7 ++- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py index 09e6329174b3f69..46a18f727e40d27 100644 --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -2,11 +2,12 @@ import subprocess import sys import os +import threading from test import support from test.support import import_helper from test.support import os_helper -from test.support import script_helper from test.support import threading_helper +from test.support.isolation import runInSubprocess # Skip this test if the _tkinter module wasn't built. _tkinter = import_helper.import_module('_tkinter') @@ -36,38 +37,27 @@ def testFlattenLen(self): self.assertRaises(TypeError, _tkinter._flatten, {'set'}) @unittest.skipUnless(support.Py_GIL_DISABLED, - "gh-154923 is a free-threading race") + 'requires the free-threaded build') @threading_helper.requires_working_threading() + @runInSubprocess() def test_create_concurrent(self): - # Concurrent _tkinter.create() races Tcl's first-time library - # init and can double-free tcl_lock (gh-154923). Run in a - # subprocess so Tcl has not already been initialized. - script = """\ -import _tkinter -import threading -nthreads = 4 -rounds = 8 -start = threading.Barrier(nthreads) -errors = [] - -def worker(): - try: - start.wait() - for _ in range(rounds): - app = _tkinter.create(None, '', 'Tk', False, 1, False, False, None) - del app - except Exception as e: - errors.append(repr(e)) - -threads = [threading.Thread(target=worker) for _ in range(nthreads)] -for t in threads: - t.start() -for t in threads: - t.join() -if errors: - raise SystemExit('\\n'.join(errors)) -""" - script_helper.assert_python_ok('-c', script, __isolated=False) + # gh-154923: Create Tcl interpreters in different threads at once. + # Run in a fresh process, where no interpreter was created yet. + nthreads = 16 + start = threading.Barrier(nthreads) + errors = [] + def worker(): + try: + start.wait() + for _ in range(8): + _tkinter.create(None, '', 'Tk', False, 1, False, False, + None) + except Exception as e: + errors.append(e) + threads = [threading.Thread(target=worker) for _ in range(nthreads)] + with threading_helper.start_threads(threads): + pass + self.assertEqual(errors, []) class TclTest(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst b/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst index bd871d2a4189526..54c04dcc1ecc7b0 100644 --- a/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst +++ b/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst @@ -1,2 +1,2 @@ -Fix a data race when creating Tcl interpreters concurrently via -``_tkinter.create()`` on the :term:`free-threaded build`. +Fix a crash when creating Tcl interpreters concurrently in different +threads in the :term:`free-threaded build`. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index f66a5c1887c1fdc..57a932c35be2cda 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -681,10 +681,9 @@ Tkapp_New(const char *screenName, const char *className, if (v == NULL) return NULL; - /* Tcl's first CreateInterp() runs process-wide lazy init (including - Tcl_MutexLock initializing a static mutex). Concurrent callers race - that init under free-threading; two threads can also both free - tcl_lock. Serialize both (gh-154923). */ + /* Without the GIL, two threads could both free tcl_lock below, and + the first-time initialization in Tcl_CreateInterp() is not + thread-safe either. Serialize them. */ static PyMutex tcl_create_mutex; PyMutex_Lock(&tcl_create_mutex); v->interp = Tcl_CreateInterp();