Tk Source Code

View Ticket
Login
Ticket UUID: aeb2ed51abc45f7b544c7695875bee6d013a580a
Title: Aqua: Unretained NSWindow reference passed to showRootWindow() idle task
Type: Bug Version: 9.0.5, 9.1.0
Submitter: chrstphrchvz Created on: 2025-11-29 01:37:46
Subsystem: 66. Aqua Window Operations Assigned To: chrstphrchvz
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-11-10 22:10:03
Resolution: Fixed Closed By: nflyernz
    Closed on: 2026-11-10 22:10:03
Description:

Just a potential issue I notice from code review, not an observed crash. In TkpInit(), there is this call:

Tcl_DoWhenIdle(showRootWindow, window);

It seems possible that the NSWindow window could get deallocated before showRootWindow() is finally called. I would think window should be retained first and then released during showRootWindow(). Or maybe Tcl_CancelIdleCall(showRootWindow, …) should be called from somewhere instead, although showRootWindow() is currently static.

User Comments: nflyernz added on 2026-11-10 22:10:03:
 ### Independent macOS ARM64 verification of the `showRootWindow()` fix

I encountered a native Tkinter crash while investigating Python 3.15.2 GUI tests in ArduPilot MethodicConfigurator.

The problem was reduced to this standalone reproducer, requiring no application code or third-party modules:

```python
import tkinter as tk

first = tk.Tk()
first.destroy()

second = tk.Tk()
second.update_idletasks()
second.destroy()
```

On macOS 26.7.2 ARM64, this consistently produced a native segmentation fault in the diagnostic runs.

The failure reproduced with:
- python.org/setup-python CPython 3.15.2, Tk 9.0.5
- uv-managed CPython 3.15.2, Tk 9.0.4
- python.org/setup-python CPython 3.14.10

The uv-managed build identified `showRootWindow+0x38` in the native crash stack.

I then performed a controlled comparison using two isolated Tk builds from the same pre-fix revision, applying only the fix for ticket `aeb2ed51ab` to the second build.

**Results:**
- W1 — unpatched: segmentation fault, exit 139
- W2 — patched: completed successfully, exit 0

Both trials used identical Python 3.15.2, `_tkinter`, Tcl 9.0.4 and macOS environment. Dynamic-library loading was recorded and verified to confirm that each process used the intended Tk binary.

The patch changes were limited to `macosx/tkMacOSXInit.c`.

Evidence and native traces:
https://github.com/nflyernz/MethodicConfigurator/actions/runs/42908025736

Previous reduction and distribution comparisons:
https://github.com/nflyernz/MethodicConfigurator/actions/runs/42901204212

https://github.com/nflyernz/MethodicConfigurator/actions/runs/42902938334

This provides independent confirmation that the existing upstream fix prevents this particular `showRootWindow()` crash on macOS ARM64.

It does not establish that other Tkinter lifecycle failures, geometry stalls or menu-validation crashes are resolved.

Thank you for the fix.

marc_culler (claiming to be Marc Culler) added on 2026-10-29 20:02:01:
The macos-root-destroyed-early branch stops this crash.  I have merged
that branch into trunk and will close this ticket.

serhiy.storchaka added on 2026-10-24 10:39:54:

Fixed on branch macos-root-destroyed-early [996b73f3a3], as suggested: window is retained before Tcl_DoWhenIdle() and released in showRootWindow(). The same commit fixes the crash in [8e32cb7ece], which happens once this one is avoided. New test window-2.13 runs the script from there.

Tested on Linux and Windows. I have no Mac, so could someone test it on macOS?


chrstphrchvz added on 2025-11-29 03:55:37:

The crash predicted by this ticket can be triggered by this script (adapted from test window-2.12):

package require Tk
after 1000 {set forever 1}
after 0 {destroy .}
puts "waiting"
vwait forever
puts "done waiting"

Output with NSZombieEnabled=YES:

waiting
2025-11-28 21:54:06.698 tclsh9.0[21191:236550] *** -[TKWindow respondsToSelector:]: message sent to deallocated instance 0x615000041a80
zsh: illegal hardware instruction