-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-139145: fix spinning event loop in tkinter #139180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mdehoon
wants to merge
10
commits into
python:main
Choose a base branch
from
mdehoon:issue139145_bugfix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−4
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d0489a
bpo-139145: fix spinning event loop in tkinter
5dcb99f
Adding test exposing the bug. The test mimics a user running Python i…
49258d1
📜🤖 Added by blurb_it.
blurb-it[bot] 4c4e4b5
fixing lint
dafea18
Merge branch 'issue139145_bugfix' of github.com:mdehoon/cpython into …
99086de
fix blurb
add49bd
simplify test
35758f1
be sure to delete the file handler also in case of errorInCmd
a3d07d4
only relevant on Windows
e8fc833
trivial change to trigger another round of testing
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# test_tkinter_vwait_mainloop_stdin.py | ||
import unittest | ||
import subprocess | ||
import sys | ||
import time | ||
from test import support | ||
from test.support import import_helper | ||
|
||
tkinter = import_helper.import_module("tkinter") | ||
|
||
|
||
@unittest.skipUnless(support.has_subprocess_support, "test requires subprocess") | ||
@unittest.skipIf(sys.platform == "win32", "test is not supported on Windows") | ||
class TkVwaitMainloopStdinTest(unittest.TestCase): | ||
|
||
def run_child(self): | ||
# Full Python script to execute in the child | ||
code = r""" | ||
import tkinter as tk | ||
import time | ||
|
||
interp = tk.Tcl() | ||
|
||
def do_vwait(): | ||
start_wall = time.time() | ||
start_cpu = time.process_time() | ||
interp.eval("vwait myvar") | ||
end_cpu = time.process_time() | ||
end_wall = time.time() | ||
cpu_frac = (end_cpu - start_cpu) / (end_wall - start_wall) | ||
print(cpu_frac) | ||
|
||
# Schedule vwait and release | ||
interp.after(100, do_vwait) | ||
interp.after(500, lambda: interp.setvar("myvar", "done")) | ||
""" | ||
|
||
# Start child in interactive mode, but use -c to execute code immediately | ||
proc = subprocess.Popen( | ||
[sys.executable, "-i", "-c", code], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
return proc | ||
|
||
def test_vwait_stdin_busy_loop(self): | ||
proc = self.run_child() | ||
|
||
# Wait until vwait likely started | ||
time.sleep(0.15) | ||
|
||
# Send input to stdin to trigger the bug | ||
proc.stdin.write(b"\n") | ||
|
||
# Ensure child exits cleanly | ||
proc.stdin.write(b"exit()\n") | ||
|
||
stdout, stderr = proc.communicate() | ||
out = stdout.decode("utf-8", errors="replace") | ||
err = stderr.decode("utf-8", errors="replace") | ||
|
||
if proc.returncode != 0: | ||
self.fail(f"Child exited with {proc.returncode}\nSTDOUT:\n{out}\nSTDERR:\n{err}") | ||
|
||
# Extract CPU fraction printed by child | ||
cpu_frac = float(out) | ||
|
||
# Fail if CPU fraction is too high (indicative of busy-loop) | ||
self.assertLess(cpu_frac, 0.5, | ||
f"CPU usage too high during vwait with stdin input (fraction={cpu_frac:.2f})") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2025-09-20-15-52-12.gh-issue-139145.1bMDI-.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Prevents tkinter ending up in a busy loop when ``vwait`` (and other Tcl commands | ||
that run their own tight event loop) are used with Python in interactive mode, | ||
and the user types any input on stdin while ``vwait`` is still running. Patch | ||
by Michiel de Hoon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3356,7 +3356,9 @@ static int stdin_ready = 0; | |||||
static void | ||||||
MyFileProc(void *clientData, int mask) | ||||||
{ | ||||||
int *tfile = clientData; | ||||||
stdin_ready = 1; | ||||||
Tcl_DeleteFileHandler(*tfile); | ||||||
} | ||||||
#endif | ||||||
|
||||||
|
@@ -3373,7 +3375,7 @@ EventHook(void) | |||||
errorInCmd = 0; | ||||||
#ifndef MS_WINDOWS | ||||||
tfile = fileno(stdin); | ||||||
Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL); | ||||||
Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, &tfile); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#endif | ||||||
while (!errorInCmd && !stdin_ready) { | ||||||
int result; | ||||||
|
@@ -3398,14 +3400,14 @@ EventHook(void) | |||||
if (result < 0) | ||||||
break; | ||||||
} | ||||||
#ifndef MS_WINDOWS | ||||||
Tcl_DeleteFileHandler(tfile); | ||||||
#endif | ||||||
if (errorInCmd) { | ||||||
errorInCmd = 0; | ||||||
PyErr_SetRaisedException(excInCmd); | ||||||
excInCmd = NULL; | ||||||
PyErr_Print(); | ||||||
#ifndef MS_WINDOWS | ||||||
if (!stdin_ready) Tcl_DeleteFileHandler(tfile); | ||||||
#endif | ||||||
} | ||||||
PyEval_SaveThread(); | ||||||
return 0; | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this. This is an important point.
The
while
-loop inEventHook
can only be exited if eitherstdin_ready
is 1 orerrorInCmd
is 1.stdin_ready
is set to 1 only inMyFileProc
, which also callsTcl_DeleteFileHandler
. As the file handler is deleted, there won't be a dangling pointer.But I was missing a call to
Tcl_DeleteFileHandler
for the case in whicherrorInCmd
is 1, andMyFileProc
was never called. Then then file handler will stay alive and will keep a dangling pointer totfile
afterEventHook
exits.I fixed this by adding a call to
Tcl_DeleteFileHandler
to theif (errorInCmd) {...
to ensure that the file handler is deleted beforeEventHook
exits, and no pointer totfile
remains,