@@ -416,6 +416,50 @@ static Tcl_ThreadDataKey excInCmd_key;
416416#define excInCmd \
417417 (*(PyObject **)Tcl_GetThreadData(&excInCmd_key, sizeof(PyObject *)))
418418
419+ /* The number of live Tcl interpreters created in the current thread. */
420+ static Tcl_ThreadDataKey interp_count_key ;
421+ #define interp_count \
422+ (*(int *)Tcl_GetThreadData(&interp_count_key, sizeof(int)))
423+
424+ /* Tcl allocates per-thread resources (for example an event handle on
425+ Windows for every thread which waits for a call to a Tcl interpreter in
426+ other thread), which are only freed by Tcl_FinalizeThread(). Call it
427+ when the Python thread state is cleared at the end of the thread, unless
428+ the thread still has a Tcl interpreter. */
429+
430+ #define THREAD_FINALIZER_NAME "_tkinter.thread_finalizer"
431+
432+ static void
433+ thread_finalizer (PyObject * capsule )
434+ {
435+ void * thread_id = PyCapsule_GetPointer (capsule , THREAD_FINALIZER_NAME );
436+ if (thread_id == (void * )Tcl_GetCurrentThread () && interp_count == 0 ) {
437+ Tcl_FinalizeThread ();
438+ }
439+ }
440+
441+ static int
442+ register_thread_finalizer (void )
443+ {
444+ PyObject * dict = PyThreadState_GetDict ();
445+ if (dict == NULL ) {
446+ return 0 ;
447+ }
448+ int r = PyDict_ContainsString (dict , THREAD_FINALIZER_NAME );
449+ if (r != 0 ) {
450+ return r < 0 ? -1 : 0 ;
451+ }
452+ PyObject * capsule = PyCapsule_New ((void * )Tcl_GetCurrentThread (),
453+ THREAD_FINALIZER_NAME ,
454+ thread_finalizer );
455+ if (capsule == NULL ) {
456+ return -1 ;
457+ }
458+ r = PyDict_SetItemString (dict , THREAD_FINALIZER_NAME , capsule );
459+ Py_DECREF (capsule );
460+ return r ;
461+ }
462+
419463
420464static PyObject * Tkapp_UnicodeResult (TkappObject * );
421465
@@ -670,6 +714,11 @@ Tkapp_New(const char *screenName, const char *className,
670714 v -> interp = Tcl_CreateInterp ();
671715 PyMutex_Unlock (& tcl_create_mutex );
672716 v -> thread_id = Tcl_GetCurrentThread ();
717+ interp_count ++ ;
718+ if (register_thread_finalizer () < 0 ) {
719+ Py_DECREF (v );
720+ return NULL ;
721+ }
673722#if TCL_MAJOR_VERSION < 9 /* Tcl 9.x is always threaded */
674723 threaded = Tcl_GetVar2Ex (v -> interp ,
675724 "tcl_platform" ,
@@ -1745,6 +1794,9 @@ _tkinter_tkapp_call_impl(TkappObject *self, PyObject *args)
17451794 PyObject * exc = NULL ; // init to make static analyzers happy
17461795 if (!WaitForMainloop (self ))
17471796 return NULL ;
1797+ if (register_thread_finalizer () < 0 ) {
1798+ return NULL ;
1799+ }
17481800 ev = (Tkapp_CallEvent * )attemptckalloc (sizeof (Tkapp_CallEvent ));
17491801 if (ev == NULL ) {
17501802 PyErr_NoMemory ();
@@ -2026,6 +2078,9 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
20262078 if (!WaitForMainloop (self ))
20272079 return NULL ;
20282080
2081+ if (register_thread_finalizer () < 0 ) {
2082+ return NULL ;
2083+ }
20292084 ev = (VarEvent * )attemptckalloc (sizeof (VarEvent ));
20302085 if (ev == NULL ) {
20312086 PyErr_NoMemory ();
@@ -2705,7 +2760,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
27052760 }
27062761
27072762 if (self -> thread_id != Tcl_GetCurrentThread () &&
2708- !WaitForMainloop (self ))
2763+ ( !WaitForMainloop (self ) || register_thread_finalizer () < 0 ))
27092764 return NULL ;
27102765
27112766 TRACE (self , ("((ss()O))" , "proc" , name , func ));
@@ -2781,6 +2836,9 @@ _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name)
27812836
27822837 Tcl_Condition cond = NULL ;
27832838 CommandEvent * ev ;
2839+ if (register_thread_finalizer () < 0 ) {
2840+ return NULL ;
2841+ }
27842842 ev = (CommandEvent * )attemptckalloc (sizeof (CommandEvent ));
27852843 if (ev == NULL ) {
27862844 PyErr_NoMemory ();
@@ -3385,6 +3443,7 @@ Tkapp_Dealloc(PyObject *op)
33853443 ENTER_TCL
33863444 Tcl_DeleteInterp (Tkapp_Interp (self ));
33873445 LEAVE_TCL
3446+ interp_count -- ;
33883447 }
33893448 DisableEventHook (self );
33903449 (void )Tkapp_Clear (op );
0 commit comments