Skip to content

Conversation

@ngoldbaum
Copy link
Contributor

@ngoldbaum ngoldbaum commented Oct 28, 2025

Fixes #30666

Currently matplotlib will crash due to an uncaught NSException if a worker thread tries to create a GUI window. Only the main thread can create GUIs in Cocoa. This is documented in the Cocoa docs under the "Application Kit Framework Thread Safety" section.

I added a new check to the beginning of FigureManager_new that bails with an exception if the thread is not the main thread. It turns out [NSThread isMainThread] is the builtin way to check for that in objC. I'm open to other more fine-grained approaches as well. I tried simply catching the exception seen in #30666, but that fails because subsequent Cocoa cleanup code segfaults. The only way I could find to avoid these segfaults was to avoid setting up any Cocoa state and bailing as early as possible as in this PR.

Also added a test to make sure the correct exception gets raised. There's already a more generic UserWarning generated by the pyplot python implementation, so I added a pytest.warns for that as well.

@ngoldbaum
Copy link
Contributor Author

Huh. I need to come back to the to try to understand why the image comparison tests are failing on the Mac runners...

@ngoldbaum
Copy link
Contributor Author

Huh. I need to come back to the to try to understand why the image comparison tests are failing on the Mac runners...

actually on second look, I think these failures are happening on all PRs. I don't see the Windows failure here elsewhere but I don't see how it could be related.

@timhoffm
Copy link
Member

The windows failures are flaky timeouts, which are a known issue.

The macOS image comparison failures are quite new but happen consistently across PRs. We haven’t had time to inspect them but they are unrelated to this PR. I’m sorry our CI is currently in a desolate state.

@ksunden
Copy link
Member

ksunden commented Oct 29, 2025

Appears likely to be the Ghostscript version update as noted (re: fedora rawhide) here: #30624

At the very least the same two tests were affected

@tacaswell tacaswell added this to the v3.10.8 milestone Oct 29, 2025
@greglucas
Copy link
Contributor

I wonder if we should add #pragma nocoverage to the private subprocess helper classes so it doesn't look like codecoverage was less than it actually was in these. I can tell the subprocess test ran that function and tested it locally that it was getting hit.

@greglucas greglucas merged commit 94def4e into matplotlib:main Oct 31, 2025
48 of 50 checks passed
@lumberbot-app
Copy link

lumberbot-app bot commented Oct 31, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v3.10.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 94def4ee50ce92ce96461f269e0c79c188588610
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #30697: BUG: raise when creating a MacOS FigureManager outside the main thread'
  1. Push to a named branch:
git push YOURFORK v3.10.x:auto-backport-of-pr-30697-on-v3.10.x
  1. Create a PR against branch v3.10.x, I would have named this PR:

"Backport PR #30697 on branch v3.10.x (BUG: raise when creating a MacOS FigureManager outside the main thread)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

ngoldbaum pushed a commit to ngoldbaum/matplotlib that referenced this pull request Oct 31, 2025
…ing a MacOS FigureManager outside the main thread)
ngoldbaum pushed a commit to ngoldbaum/matplotlib that referenced this pull request Oct 31, 2025
…rker-threads

BUG: raise when creating a MacOS FigureManager outside the main thread
(cherry picked from commit 94def4e)
QuLogic added a commit that referenced this pull request Nov 1, 2025
Backport PR #30697 on branch v3.10.x (BUG: raise when creating a MacOS FigureManager outside the main thread)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: calling pyplot.gca() outside the main thread crashes the interpreter with the MacOS backend

6 participants