Skip to content

gh-162654: Fix use-after-free in tkinter file handlers - #163180

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-162654-filehandler-uaf
Open

serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-162654-filehandler-uaf

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Dec 5, 2026 •

Copy link
Copy Markdown
Member
  • deletefilehandler() now releases the callback and the file object after calling Tcl_DeleteFileHandler().
  • Releasing them no longer crashes if a finalizer modifies the list of handlers.
  • The list of handlers is now per-thread, like in Tcl.
  • createfilehandler() now releases the replaced handler immediately.

🤖 Generated with Claude Code

* deletefilehandler() now releases the callback and the file object
  after calling Tcl_DeleteFileHandler().
* Releasing them no longer crashes if a finalizer modifies the list of
  handlers.
* The list of handlers is now per-thread, like in Tcl.
* createfilehandler() now releases the replaced handler immediately.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

sreevx commented Dec 5, 2026

Copy link
Copy Markdown

I think the new thread-local list needs an explicit teardown path.

Each entry in HeadFHCD is allocated with PyMem_NEW and owns strong references to func and file. However, thread_finalizer() currently calls Tcl_FinalizeThread() when interp_count == 0 without first walking and freeing HeadFHCD.

The thread-local storage for the list head being reclaimed by Tcl does not, by itself, release the separately allocated FileHandler_ClientData nodes or their Python references. Tcl exposes thread-exit handlers specifically for per-thread cleanup; could this list be drained as part of thread finalization (with care about the order relative to Tcl's own file-handler teardown)?

The current test_threads deletes the worker's file handler explicitly, so it does not exercise this path. Could we add a regression test that registers a file handler in a worker thread, leaves it registered, then lets the thread exit and verifies that the retained callback/file references are released?

I haven't reproduced the leak yet, so this is a lifecycle concern from the ownership paths rather than a confirmed runtime failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants