[Bug]: macosx backend crashes when used on a worker thread · Issue #31968 · matplotlib/matplotlib · GitHub
Skip to content

[Bug]: macosx backend crashes when used on a worker thread #31968

Description

@iccir

Bug summary

While Interactive figures and asynchronous programming specifically states that "In general Matplotlib is not thread safe", the macosx backend crashes with SIGSEGV or SIGTRAP when various methods are called on a worker thread:

Method Result
FigureCanvas.flush_events CRASH
FigureManager.set_window_title Obj-C Exception
FigureManager.show CRASH
FigureManager.resize CRASH
FigureManager.full_screen_toggle CRASH

Some methods, like FigureCanvas.set_cursor, do work without crashing; however, NSCursor isn't documented to be thread-safe. It's also not on the historic list of thread-unsafe classes, so our usage falls into a gray area.

I think we should default to setting a Python error when any macosx backend method is called on a background thread, with specific exceptions for Timer or FigureCanvas.draw_idle(). Possibly others?

Code for reproduction

import threading
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use("macosx")
 
fig = plt.figure()
canvas = fig.canvas
manager = fig.canvas.manager

def worker():
    canvas.flush_events()       # EXC_BAD_ACCESS (SIGSEGV)
#    manager.show()
#    manager.resize(100, 200)
 
t = threading.Thread(target=worker)
t.start()
t.join()

Actual outcome

SIGSEGV, SIGTRAP, Obj-C Exception

Expected outcome

A Python error is thrown.

Additional information

No response

Operating system

No response

Matplotlib Version

3.11

Matplotlib Backend

macosx

Python version

3.14.5

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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