Title: tkinter wait_variable can cause the event loop to spin · Issue #139145 · python/cpython · GitHub
Open Graph Title: tkinter wait_variable can cause the event loop to spin · Issue #139145 · python/cpython
X Title: tkinter wait_variable can cause the event loop to spin · Issue #139145 · python/cpython
Description: Bug report Bug description: The following script causes the event loop to spin when Python is in interactive mode. To reproduce this bug, run this script as python -i script.py. Then press the top button to call the root.wait_variable co...
Open Graph Description: Bug report Bug description: The following script causes the event loop to spin when Python is in interactive mode. To reproduce this bug, run this script as python -i script.py. Then press the top ...
X Description: Bug report Bug description: The following script causes the event loop to spin when Python is in interactive mode. To reproduce this bug, run this script as python -i script.py. Then press the top ...
Opengraph URL: https://github.com/python/cpython/issues/139145
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"tkinter wait_variable can cause the event loop to spin","articleBody":"# Bug report\n\n### Bug description:\n\nThe following script causes the event loop to spin when Python is in interactive mode.\n\nTo reproduce this bug, run this script as `python -i script.py`. Then press the top button to call the `root.wait_variable` command. Once waiting, press \"enter\" in the command window. Monitoring `top` will show you that CPU usage goes up to 100%.\n\nThe reason is that `EventHook` (in `_tkinter.c`) calls `Tcl_CreateFileHandler` to ask Tcl to monitor stdin.\n`root.wait_variable` calls `Tk_TkwaitObjCmd`, which will sit in the following tight event loop (in `generic/tkCmds.c` in the Tk source):\n```C\n done = 0;\n while (!done) {\n if (Tcl_Canceled(interp, TCL_LEAVE_ERR_MSG) == TCL_ERROR) {\n code = TCL_ERROR;\n break;\n }\n Tcl_DoOneEvent(0);\n }\n```\nBut because this event loop starts before `EventHook` exits, Tcl is still monitoring stdin.\nTherefore, `Tcl_DoOneEvent` detects activity on stdin, resulting in a call to `MyFileProc` (in `_tkinter.c`), which duly sets `stdin_ready` to 1 without reading the input on stdin. But the tight event loop above doesn't know about `stdin_ready`, and therefore keeps running, calling `Tcl_DoOneEvent` again, which finds the same input still available on stdin. Thus this tight event loop keeps spinning, causing the CPU usage to go up to 100%.\n\nNote that calling `trigger_wait_variable` from the Python command prompt (instead of pressing the button) will not result in a spinning event loop, as in that case `EventHook` exits first, calling `Tcl_DeleteFileHandler` on stdin before `trigger_wait_variable` is executed. Therefore in that case Tcl is no longer monitoring stdin when entering the tight event loop above.\n\nThis was tested on Linux.\n\n\n\n```python\nimport tkinter\n\nroot = tkinter.Tk()\nroot.title(\"wait_variable Demo\")\n\npy_var = tkinter.StringVar()\n\nlabel = tkinter.Label(root, text=\"click the top button to start waiting\", padx=20, pady=10)\nlabel.pack()\n\ndef trigger_wait_variable():\n label.config(text=\"click the bottom button to stop waiting\")\n print(\"click the bottom button to stop waiting\")\n root.wait_variable(py_var) # blocks until py_var is set\n print(\"finished waiting\")\n label.config(text=f\"finished waiting\")\n\ntkinter.Button(root, text=\"call wait_variable to start waiting\", command=trigger_wait_variable).pack()\n\ndef set_py_var():\n py_var.set(\"done from wait_variable button\")\n\ntkinter.Button(root, text=\"stop waiting\", command=set_py_var).pack()\n```\n\n\n### CPython versions tested on:\n\n3.12\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-139180\n* gh-152257\n* gh-152291\n* gh-152292\n* gh-152293\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/mdehoon","@type":"Person","name":"mdehoon"},"datePublished":"2025-09-19T04:46:00Z","url":"https://github.com/python/cpython/issues/139145"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ac4c14f9-2991-beee-c59b-1e29aaf28ad8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A7E2:19E4BB:446F858:571D581:6B023A98 |
| html-safe-nonce | b520331e66e7520448d47b985f47535aca6c48afe8c24e851c2e6676fe028bd4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBN0UyOjE5RTRCQjo0NDZGODU4OjU3MUQ1ODE6NkIwMjNBOTgiLCJ2aXNpdG9yX2lkIjoiNTEyMjE5NzE4MDMyODg1MjEyMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3434dcd6c004eb05a118cef55ea8f2050f3560733e0c97e2b83ffd0eaf074352 |
| hovercard-subject-tag | issue:3432755134 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/139145/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0a8de7b0a34afba1295022ba384434dddd5fe617e05113580345c08e230e390d/python/cpython/issues/139145 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0a8de7b0a34afba1295022ba384434dddd5fe617e05113580345c08e230e390d/python/cpython/issues/139145 |
| og:image:alt | Bug report Bug description: The following script causes the event loop to spin when Python is in interactive mode. To reproduce this bug, run this script as python -i script.py. Then press the top ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mdehoon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width