Skip to content

[Bug]: Qt, opencv, matplotlib interaction -> matplotlib figure, ax trigger error, causes SIGABRT without error trace. #29139

Closed as not planned
@BMaxV

Description

@BMaxV

Bug summary

Calling these functions after [bad setup]

fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)

causes a SIGABRT.

In short, the error / crash is fine and the steps to fix it for the user are clear / documented.

The problem is the non-graceful fail.

The issue isn't caused by matplotlib, but I'm reporting the issue here anyway, because I think matplotlib expects a certain setup, and triggers an error when that assumption isn't true. From my perspective, writing those simple lines should work or produce a pythonic error (assumptions not met, conflict with other package, whatever).

The fix for the underlying issue is to uninstall opencv-python and to install opencv-python-headless.

Code for reproduction

# not that simple.

Actual outcome

QObject::moveToThread: Current thread (0x25b0b60) is not the object's thread (0x3c70250).
Cannot move to target thread (0x25b0b60)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/max/.local/lib/python3.12/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx.

Expected outcome

a pythonic error trace that at least shows me which function caused the issue.

Additional information

There are multiple closed and a few open issues to essentially the topic here, on opencv and in various other packages that encounter the problem.

e.g. this one.

opencv/opencv-python#736

the search phrase

Could not load the Qt platform plugin "xcb" matplotlib

should turn up more examples.


I found this answer for how to handle report, but that doesn't really seem to work nicely. The error handler works in the sense it does detect the SIGABRT, but it still kills the process. Maybe, you can put this in...

https://discuss.python.org/t/how-can-i-handle-sigabrt-from-third-party-c-code-std-abort-call/22078/4

Their "solution" is to use multiprocessing, which is probably not something you want to build into your basic fig construction.


As a way to detect the problem I used what's being suggested in the python.org thread and sys.modules.

import sys
import ctypes
from signal import SIGABRT

c_globals = ctypes.CDLL(None) # POSIX

@ctypes.CFUNCTYPE(None, ctypes.c_int)
def sigabrt_handler(sig):
    print('SIGABRT handled!',sig)
    for x in sys.modules:
        if "cv2" in x:
            print("cv2 detected, this can cause a known issue [link]")
            break
        
    raise NotImplementedError("some interaction causes a C SIGABRT")

c_globals.signal(SIGABRT, sigabrt_handler)

which changes the error to look like this:

QObject::moveToThread: Current thread (0x331d4a0) is not the object's thread (0x4ab3630).
Cannot move to target thread (0x331d4a0)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/max/.local/lib/python3.12/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx.

SIGABRT handled! 6
modules
cv2 detected, this can cause a known issue [link]
Exception ignored on calling ctypes callback function: <function sigabrt_handler at 0x76fb050d8cc0>
Traceback (most recent call last):
  File "/home/max/Documents/Python/orometry-terrains/TerrainFromDivtree.py", line 40, in sigabrt_handler
    raise NotImplementedError("some interaction causes a C SIGABRT")
NotImplementedError: some interaction causes a C SIGABRT
fish: Job 2, 'python3 TerrainFromDivtree.py' terminated by signal SIGABRT (Abort)

still not good, but maybe if you plug the link in there, people can find better info on the problem than having to piece it together.

Anyway, thanks for your work on matplotlib!

If you're another user who encountered this bug, I hope this helps :)

Operating system

Ubuntu

Matplotlib Version

3.9.2

Matplotlib Backend

No response

Python version

3.12

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions