Repository navigation
gh-161353: Fix handle leak in test_tcl - #161368
Conversation
Fix handle leaks in test_tcl when the test is run with -R 3:3. Call explicitly Tcl_FinalizeThread() in test_returning_deeply_nested() to clean up explicitly per-thread state. The test is run in a thread because of the @run_with_limited_c_stack() decorator. On Windows, Tcl_CreateInterp() creates an event in the Tcl notifier implementation. The event is stored in per-thread state which is only cleaned up at exit. Add _tkinter.finalize_thread() function.
|
See #161353 (comment) for the longer rationale. @serhiy-storchaka @terryjreedy @gpshead: Would you mind to review my fix? |
|
This fix should be backported to 3.15 once #161071 will be merged. |
|
An alternative would be to register an atexit callback with I mean, I don't understand well if the handle leak (one Windows event) only impacts tests, or call impact real Python code. |
|
!buildbot AMD64.Windows.Server.2025.Refleaks. |
|
🤖 New build scheduled with the buildbot fleet by @encukou for commit bc80f6a 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F161368%2Fmerge The command will test the builders whose names match following regular expression: The builders matched are:
|
|
|
|
Well, the simplest fix is to skip the test in the leak-hunting mode. The leak is still here, but it is small, and once per thread. |
In my PR, _tkinter.finalize_thread() is called when the thread exits, so I'm not sure of what you mean by "later"? Do you mean executing more Tcl/Tk code after calling _tkinter.finalize_thread()? Would you prefer to rename the function to |
|
Adding an underscore and changing the docstring will help. But skipping the test is simpler. |
Oh wait, I misunderstood
I added an underscore. How do you want to change the docstring? The function is documented at https://www.tcl-lang.org/man/tcl8.2.3/TclLib/Exit.htm as:
I wasn't sure about the "invoke the thread exit handlers" part so I omitted it. |
I prefer to not skip the test. It's useful to run it on Refleak buildbots. For example, it catched this leak :-) The buildbot is broken for 6 days. That's way longer than what's expected on a Tier-1 platform: https://peps.python.org/pep-0011/#tier-1. So I enabled auto-merge to fix the buildbot regression. IMO this change only hides a real bug. When a Python thread calls |
Fix handle leaks in test_tcl when the test is run with -R 3:3. Call explicitly Tcl_FinalizeThread() in test_returning_deeply_nested() to clean up explicitly per-thread state. The test is run in a thread because of the
@run_with_limited_c_stack()decorator.On Windows, Tcl_CreateInterp() creates an event in the Tcl notifier implementation. The event is stored in per-thread state which is only cleaned up at exit.
Add _tkinter.finalize_thread() function.