Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: macOS backend run by non-logged-in user crashes in Ventura #26292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tjni opened this issue Jul 12, 2023 · 7 comments · May be fixed by #27761
Open

[Bug]: macOS backend run by non-logged-in user crashes in Ventura #26292

tjni opened this issue Jul 12, 2023 · 7 comments · May be fixed by #27761
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! GUI: MacOSX

Comments

@tjni
Copy link

tjni commented Jul 12, 2023

Bug summary

When the macOS backend is used to draw a figure in Ventura (I am on 13.4.1), it crashes with an XPC misuse error when it is run from a non-root and non-logged-in user (from what I can tell).

...

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BREAKPOINT (SIGTRAP)
Exception Codes:       0x0000000000000001, 0x0000000181f5db40

Termination Reason:    Namespace SIGNAL, Code 5 Trace/BPT trap: 5
Terminating Process:   exc handler [35989]

Application Specific Signatures:
API Misuse

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libxpc.dylib                  	       0x181f5db40 _xpc_api_misuse + 92
1   libxpc.dylib                  	       0x181f3fb2c xpc_connection_set_target_uid + 216
2   WindowManagement              	       0x208de9cf0 -[WMClientWindowManager _createXPCConnection] + 968
3   WindowManagement              	       0x208dea630 -[WMClientWindowManager _xpcConnection] + 172
4   WindowManagement              	       0x208de83f0 -[WMClientWindowManager stages] + 84
5   AppKit                        	       0x185e5e568 __54-[NSWMWindowCoordinator initializeStageFramesIfNeeded]_block_invoke + 32
6   libdispatch.dylib             	       0x18204c400 _dispatch_client_callout + 20
7   libdispatch.dylib             	       0x18204dc40 _dispatch_once_callout + 32
8   AppKit                        	       0x185e5e514 -[NSWMWindowCoordinator initializeStageFramesIfNeeded] + 304
9   AppKit                        	       0x18550b278 -[NSWindow _commonInitFrame:styleMask:backing:defer:] + 660
10  AppKit                        	       0x18550ac50 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 684
11  AppKit                        	       0x18550a998 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48
12  ???                           	       0x118de0980 -[Window initWithContentRect:styleMask:backing:defer:withManager:] + 68
13  ???                           	       0x118de3f74 FigureManager_init + 240
14  ???                           	       0x101106744 wrap_init + 20
15  ???                           	       0x1010a735c wrapperdescr_call + 392
16  ???                           	       0x10109e400 _PyObject_MakeTpCall + 360
...

Code for reproduction

N/A

Actual outcome

N/A

Expected outcome

N/A

Additional information

The most common place I have seen this is when a module consuming matplotlib draws figures in its tests without specifying a backend. When that module is built by the nix package manager, the build is performed by a non-root, non-logged-in user, triggering this crash.

I do not think there is anything actionable here but wanted to open an issue to help anyone else who runs into this. Perhaps detection is possible during backend selection, but it's not entirely obvious to me how to do it robustly. If the team agrees, we can close this issue.

Operating system

macOS 13.4.1

Matplotlib Version

3.7.1

Matplotlib Backend

macosx

Python version

3.10.11

Jupyter version

No response

Installation

None

@jklymak
Copy link
Member

jklymak commented Jul 12, 2023

I'm not sure I completely understand but I can imagine it doesn't make sense to use any gui backend if the user is not logged in and hence has no display.

@tacaswell tacaswell added this to the v3.9.0 milestone Jul 12, 2023
@tacaswell
Copy link
Member

The quick fix is for nix to set the MPLBACKEND=agg env for those packages when running tests.

For linux we have a helper to detect if we have a usable display and if not, the auto-backend selection falls back to a non-interactive backend. I suspect that something similar is possible for OSX to detect if we have a usable GUI display.

I tried to reproduce this by sshing into a mac without being logged in, but that failed in other ways (blocking show() blocked...forever and there was no window even after I logged in).

I think this is a good first issue because it will be adding a private helper function + one or two calls to it in the backend selection logic and/or the backend start up code.

Tasks:

  • sort out if there is a way (could be in objective-c) to detect if we can actually make a window
  • skip GUI backends on OSX if we do not have a functioning display
  • work out a way to test this (we do have OSX CI jobs and standard ways of running tests in sub-processes) or make a good case as to why we can not test this under CI / developer machines (e.g. we need elevated privileges / to make a new user / log the user out to drive the system to a broken state)

I would say this is medium difficulty as it is possible that there is a call in the OSX SDK that just answers this question, but might require research to find it. You will also have to understand the auto backend selection logic.

@tacaswell tacaswell added GUI: MacOSX Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! labels Jul 12, 2023
@github-actions
Copy link

Good first issue - notes for new contributors

This issue is suited to new contributors because it does not require understanding of the Matplotlib internals. To get started, please see our contributing guide.

We do not assign issues. Check the Development section in the sidebar for linked pull requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please collaborate on the work by reviewing it rather than duplicating it in a competing PR.

If something is unclear, please reach out on any of our communication channels.

@tacaswell
Copy link
Member

I labeled this as 3.9 to keep it out of our 3.8 wrap up workstream for now. If this ends up being simple no problem in backporting it to 3.8.x .

@tjni
Copy link
Author

tjni commented Jul 12, 2023

Thank you for responding! I cobbled together these reproduction steps on my macOS 13.4.1 M1 machine:

  1. Suppose we are currently logged in with username me.
  2. Set up test directory:
cd ~
mkdir matplotlib-test
cd matplotlib-test
python -m venv env
source env/bin/activate
pip install matplotlib
  1. Create a script test.py:
import matplotlib.pyplot as plt

figure = plt.figure()
  1. Verify that python test.py does not crash.
  2. Create a new user named someuser:
sudo dscl . -create /Users/someuser
sudo dscl . -create /Users/someuser UserShell /bin/bash
sudo dscl . -create /Users/someuser RealName "Some User"
sudo dscl . -create /Users/someuser UniqueID "1010"
sudo dscl . -create /Users/someuser PrimaryGroupID 80
sudo dscl . -create /Users/someuser NFSHomeDirectory /Users/someuser
sudo dscl . -passwd /Users/someuser password
sudo mkdir /Users/someuser
  1. Temporarily give access to me's home directory:
sudo chmod 777 /Users/me
  1. Become someuser (password is password):
su - someuser
cd /Users/me/matplotlib-test
source env/bin/activate
  1. Verify that python test.py crashes:
(env) -bash-3.2$ python test.py
Matplotlib created a temporary cache directory at /tmp/matplotlib-m4igy099 because the default path (/Users/someuser/.matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
2023-07-12 09:53:30.523 Python[87901:57870394] XType: Using static font registry.
Trace/BPT trap: 5
  1. Restore everything:
sudo chmod 750 /Users/me
sudo dscl . -delete /Users/someuser
sudo rm -rf /Users/someuser

@tacaswell
Copy link
Member

Thank you for the reproduction examples but that is not something we can put in the test suite!

@QuLogic
Copy link
Member

QuLogic commented Jan 20, 2024

I wonder if we look at the list of screens, it would be empty in this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! GUI: MacOSX
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants