@@ -460,6 +460,50 @@ static Tcl_ThreadDataKey excInCmd_key;
460460#define excInCmd \
461461 (*(PyObject **)Tcl_GetThreadData(&excInCmd_key, sizeof(PyObject *)))
462462
463+ /* The number of live Tcl interpreters created in the current thread. */
464+ static Tcl_ThreadDataKey interp_count_key ;
465+ #define interp_count \
466+ (*(int *)Tcl_GetThreadData(&interp_count_key, sizeof(int)))
467+
468+ /* Tcl allocates per-thread resources (for example an event handle on
469+ Windows for every thread which waits for a call to a Tcl interpreter in
470+ other thread), which are only freed by Tcl_FinalizeThread(). Call it
471+ when the Python thread state is cleared at the end of the thread, unless
472+ the thread still has a Tcl interpreter. */
473+
474+ #define THREAD_FINALIZER_NAME "_tkinter.thread_finalizer"
475+
476+ static void
477+ thread_finalizer (PyObject * capsule )
478+ {
479+ void * thread_id = PyCapsule_GetPointer (capsule , THREAD_FINALIZER_NAME );
480+ if (thread_id == (void * )Tcl_GetCurrentThread () && interp_count == 0 ) {
481+ Tcl_FinalizeThread ();
482+ }
483+ }
484+
485+ static int
486+ register_thread_finalizer (void )
487+ {
488+ PyObject * dict = PyThreadState_GetDict ();
489+ if (dict == NULL ) {
490+ return 0 ;
491+ }
492+ int r = PyDict_ContainsString (dict , THREAD_FINALIZER_NAME );
493+ if (r != 0 ) {
494+ return r < 0 ? -1 : 0 ;
495+ }
496+ PyObject * capsule = PyCapsule_New ((void * )Tcl_GetCurrentThread (),
497+ THREAD_FINALIZER_NAME ,
498+ thread_finalizer );
499+ if (capsule == NULL ) {
500+ return -1 ;
501+ }
502+ r = PyDict_SetItemString (dict , THREAD_FINALIZER_NAME , capsule );
503+ Py_DECREF (capsule );
504+ return r ;
505+ }
506+
463507
464508static PyObject * Tkapp_UnicodeResult (TkappObject * );
465509
@@ -709,6 +753,7 @@ Tkapp_New(const char *screenName, const char *className,
709753 static PyMutex tcl_create_mutex ;
710754 PyMutex_Lock (& tcl_create_mutex );
711755 v -> interp = Tcl_CreateInterp ();
756+ interp_count ++ ;
712757 v -> wantobjects = wantobjects ;
713758#if TCL_MAJOR_VERSION >= 9
714759 v -> threaded = 1 ;
@@ -735,6 +780,10 @@ Tkapp_New(const char *screenName, const char *className,
735780 tcl_lock = NULL ;
736781 }
737782 PyMutex_Unlock (& tcl_create_mutex );
783+ if (register_thread_finalizer () < 0 ) {
784+ Py_DECREF (v );
785+ return NULL ;
786+ }
738787
739788 v -> OldBooleanType = Tcl_GetObjType ("boolean" );
740789 {
@@ -1641,6 +1690,9 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
16411690 PyObject * exc = NULL ; // init to make static analyzers happy
16421691 if (!WaitForMainloop (self ))
16431692 return NULL ;
1693+ if (register_thread_finalizer () < 0 ) {
1694+ return NULL ;
1695+ }
16441696 ev = (Tkapp_CallEvent * )attemptckalloc (sizeof (Tkapp_CallEvent ));
16451697 if (ev == NULL ) {
16461698 PyErr_NoMemory ();
@@ -1922,6 +1974,9 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
19221974 if (!WaitForMainloop (self ))
19231975 return NULL ;
19241976
1977+ if (register_thread_finalizer () < 0 ) {
1978+ return NULL ;
1979+ }
19251980 ev = (VarEvent * )attemptckalloc (sizeof (VarEvent ));
19261981 if (ev == NULL ) {
19271982 PyErr_NoMemory ();
@@ -2603,7 +2658,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
26032658 }
26042659
26052660 if (self -> threaded && self -> thread_id != Tcl_GetCurrentThread () &&
2606- !WaitForMainloop (self ))
2661+ ( !WaitForMainloop (self ) || register_thread_finalizer () < 0 ))
26072662 return NULL ;
26082663
26092664 TRACE (self , ("((ss()O))" , "proc" , name , func ));
@@ -2679,6 +2734,9 @@ _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name)
26792734
26802735 Tcl_Condition cond = NULL ;
26812736 CommandEvent * ev ;
2737+ if (register_thread_finalizer () < 0 ) {
2738+ return NULL ;
2739+ }
26822740 ev = (CommandEvent * )attemptckalloc (sizeof (CommandEvent ));
26832741 if (ev == NULL ) {
26842742 PyErr_NoMemory ();
@@ -3249,6 +3307,7 @@ Tkapp_Dealloc(PyObject *op)
32493307 ENTER_TCL
32503308 Tcl_DeleteInterp (Tkapp_Interp (self ));
32513309 LEAVE_TCL
3310+ interp_count -- ;
32523311 }
32533312 DisableEventHook (self );
32543313 Py_XDECREF (self -> trace );
0 commit comments