Skip to content

tkinter: use-after-free in FileHandler() when deletefilehandler() runs a __del__ that services the event loop #162654

Backlog

Description

@xuziqiang98

Crash report

_tkinter.tkapp.deletefilehandler() calls DeleteFHCD() first and Tcl_DeleteFileHandler() afterwards. DeleteFHCD() unlinks the handler's client data and drops the references to the callback and to the file object right away:

        if (p->id == id) {
            *pp = p->next;
            Py_XDECREF(p->func);
            Py_XDECREF(p->file);
            PyMem_Free(p);
        }

Between the two calls Tcl still has the handler registered. Py_XDECREF(p->file) can run Python code: if the file object's __del__ (or the callback's) services the event loop (dooneevent(), update()), Tcl dispatches the still-registered handler, and FileHandler() calls data->func, which was freed one line earlier. The pointer is not cleared before the DECREF, and PyMem_Free(p) only happens after both references are gone.

Reproducer

Uses the documented tkinter API only; a Tcl interpreter is enough, no display is needed.

import _tkinter
import os
import tkinter

tcl = tkinter.Tcl()                      # Tcl only, no display needed
r, w = os.pipe()
os.write(w, b"x")                        # keep the read end readable
os.close(w)


class File:
    def fileno(self):
        return r

    def __del__(self):
        # Runs inside deletefilehandler(), after the callback has been freed
        # but before Tcl_DeleteFileHandler(): the handler is still registered.
        tcl.dooneevent(_tkinter.DONT_WAIT | _tkinter.FILE_EVENTS)


f = File()
tcl.createfilehandler(f, tkinter.READABLE, lambda *args: None)
del f                                   # only the handler keeps f alive
tcl.deletefilehandler(r)                # frees the callback, runs __del__(), which
                                        # lets Tcl dispatch the stale callback
os.close(r)
print("no crash")

Output

3.15.2, built from the official tarball with --with-address-sanitizer:

ERROR: AddressSanitizer: heap-use-after-free
READ of size 8 at 0x50f0000c74c8 thread T0
    #0 _Py_TYPE_impl Include/object.h:234:16
    #1 _PyVectorcall_FunctionInline Include/internal/pycore_call.h:95:24
    #2 _PyObject_VectorcallTstate Include/internal/pycore_call.h:139:12
    #3 _PyObject_CallFunctionVa Objects/call.c:552:18
    #4 PyObject_CallFunction Objects/call.c:574:14
    #5 FileHandler Modules/_tkinter.c:2709:11
    #6 FileHandlerEventProc (tclUnixNotfy.c:808)      <-- Tcl
    #8 Tcl_ServiceEvent (tclNotify.c:670)
    #9 Tcl_DoOneEvent (tclNotify.c:967)
    #10 _tkinter_tkapp_dooneevent_impl Modules/_tkinter.c:3012:10
    ...
    #21 slot_tp_finalize Objects/typeobject.c:11330:25      <-- File.__del__
    #24 subtype_dealloc Objects/typeobject.c:2838:13
    #28 DeleteFHCD Modules/_tkinter.c:2691:13
    #29 _tkinter_tkapp_deletefilehandler_impl Modules/_tkinter.c:2781:5

freed by thread T0 here:
    #1 _Py_Dealloc Objects/object.c:3324:5
    #2 Py_DECREF Include/refcount.h:427:9
    #3 Py_XDECREF Include/refcount.h:533:9
    #4 DeleteFHCD Modules/_tkinter.c:2690:13
    #5 _tkinter_tkapp_deletefilehandler_impl Modules/_tkinter.c:2781:5

ASan reports the same use-after-free on 3.11.18, 3.12.16, 3.13.18, 3.14.10, 3.15.2, 3.16.0a2 and main (4b66b87), 3 runs each. On main the call is at Modules/_tkinter.c:2897 and the DECREFs at lines 2878 and 2879.

Without a sanitizer the script segfaults: 5 of 5 runs on Ubuntu's python3.12 (3.12.3) with its own _tkinter.

Tested with Tcl/Tk 8.6.

CPython versions tested on:

3.16, 3.15, 3.14, 3.13, 3.12, 3.11, CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.15.2 (main, Nov 25 2026, 20:06:37) [Clang 18.1.3 (1ubuntu1)]

Activity

  1. added
    type-crash
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulestopic-tkintertype-crash

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions