Skip to content

[Bug]: macosx backend, plots not closing, segfault #32019

Description

@adamjstewart

Bug summary

I am trying to create a plot in a for-loop. I can move each plot around, but even if I close it (click the ❌, use plt.close()), all prior plots are still displayed behind the newest plot. If I try to close plots (click the ❌) out of order, it results in a segfault.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

for _ in range(100):
    fig, ax = plt.subplots()
    x = np.random.rand(10)
    y = np.random.rand(10)
    ax.plot(x, y)
    plt.show()
    plt.close()

Actual outcome

> python3 -X faulthandler test.py
Fatal Python error: Segmentation fault

Current thread 0x00000001f5cc1e80 (most recent call first):
  File "/Users/Adam/torchgeo/.venv/lib/python3.14/site-packages/matplotlib/backends/backend_macosx.py", line 180 in start_main_loop
  File "/Users/Adam/torchgeo/.venv/lib/python3.14/site-packages/matplotlib/backend_bases.py", line 3702 in show
  File "/Users/Adam/torchgeo/.venv/lib/python3.14/site-packages/matplotlib/pyplot.py", line 646 in show
  File "/Users/Adam/Downloads/test.py", line 9 in <module>

Current thread's C stack trace (most recent call first):
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at _Py_DumpStack+0x44 [0x101cf10a4]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at faulthandler_dump_c_stack+0x54 [0x101d68f48]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at faulthandler_fatal_error+0x16c [0x101d68df8]
  Binary file "/usr/lib/system/libsystem_platform.dylib", at _sigtramp+0x38 [0x189b4b744]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at _Py_Dealloc+0x38 [0x1013b0238]
  Binary file "/Users/Adam/torchgeo/.venv/lib/python3.14/site-packages/matplotlib/backends/_macosx.cpython-314-darwin.so", at -[Window close]+0x7c [0x106c59f70]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSWindow __close]+0x178 [0x18e2e2ad8]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSApplication(NSResponder) sendAction:to:from:]+0x230 [0x18e1c66c0]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSControl sendAction:to:]+0x48 [0x18e1c6460]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at __26-[NSCell _sendActionFrom:]_block_invoke+0x64 [0x18e1c63a4]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSCell _sendActionFrom:]+0xcc [0x18e1c62cc]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSButtonCell _sendActionFrom:]+0x60 [0x18e1c61c8]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at NSControlTrackMouse+0x5d4 [0x18e1c37b0]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSCell trackMouse:inRect:ofView:untilMouseUp:]+0x90 [0x18e1c31b0]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:]+0xe4 [0x18e1c307c]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSControl(_NSTracking) _mouseDown:]+0x260 [0x18e712a74]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSControl(_NSTracking) mouseDown:]+0x68 [0x18e7127e8]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:]+0xe70 [0x18e1c1c04]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:]+0x1ec [0x18e1501f4]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSWindow(NSEventRouting) sendEvent:]+0x17c [0x18e14fe64]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSApplication(NSEventRouting) sendEvent:]+0x4fc [0x18ebc040c]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSApplication _handleEvent:]+0x3c [0x18e80d664]
  Binary file "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit", at -[NSApplication run]+0x198 [0x18e01f164]
  Binary file "/Users/Adam/torchgeo/.venv/lib/python3.14/site-packages/matplotlib/backends/_macosx.cpython-314-darwin.so", at show+0x74 [0x106c5e970]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at _TAIL_CALL_CALL_NON_PY_GENERAL.llvm.10127954890374434623+0x104 [0x101705924]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at _PyFunction_Vectorcall+0x338 [0x10167429c]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at method_vectorcall.llvm.1628862021063799139+0x94 [0x1016b7330]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at _TAIL_CALL_CALL_FUNCTION_EX.llvm.10127954890374434623+0x10c [0x101469578]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at _PyEval_Vector+0x30c [0x1014f5d00]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at PyEval_EvalCode+0xa0 [0x10150588c]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at run_mod.llvm.9148091775861049191+0x124 [0x101517a7c]
  Binary file "/Users/Adam/.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/libpython3.14.dylib", at pyrun_file+0xa4 [0x101645880]
  <truncated rest of calls>

Extension modules: numpy._core._multiarray_umath, numpy.linalg._umath_linalg, PIL._imaging, kiwisolver._cext, matplotlib.backends._macosx, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._pcg64, numpy.random._generator, numpy.random._mt19937, numpy.random._philox, numpy.random._sfc64, numpy.random.mtrand (total: 14)
fish: Job 1, 'python3 -X faulthandler test.py' terminated by signal SIGSEGV (Address boundary error)

Expected outcome

I would expect no phantom plots and no segfaults.

Additional information

Tried Python 3.13 and 3.14 in fresh uv environments, no luck so far. Happy to try other debugging suggestions.

Operating system

macOS Tahoe 26.5.1

Matplotlib Version

3.11.0

Matplotlib Backend

macosx

Python version

3.14.4

Jupyter version

N/A

Installation

uv

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions