Skip to content

[Bug]: GLib io error if sigint is handled while some icon is loaded #32556

Open

Description

@bonsaiiV

Bug summary

When handling a sigint while plotting a figure, sometimes a glib error is raised. Sometimes this is handled and execution continues.

Code for reproduction

import networkx as nx
import matplotlib.pyplot as plt
import signal
import io

should_exit = False

def handle_sig_int(sig_num, _):
    global should_exit
    should_exit = True

def draw(G, pos, tree, x, y, d):
    G.add_node(tree["key"])
    pos[tree["key"]] = [x, y]
    if tree["left"] is not None:
        draw(G, pos, tree["left"], x - d, y - 1, d / 2)
        G.add_edge(tree["key"], tree["left"]["key"])
    if tree["right"] is not None:
        draw(G, pos, tree["right"], x + d, y - 1, d / 2)
        G.add_edge(tree["key"], tree["right"]["key"])

def draw_tree(buf, tree):
    G = nx.DiGraph()

    pos = {}
    draw(G, pos, tree, 0, 0, 1)
    plt.figure(figsize=(3, 3), dpi=300)
    nx.draw(G, pos, 
            with_labels=True, 
            node_size=500, 
            node_color="white", 
            edge_color="black", 
            font_size=10)
    plt.savefig(buf)
    plt.close()

signal.signal(signal.SIGINT, handle_sig_int)
tree = { "key": 50,
            "left": {
                "key": 40,
                "left": {
                    "key": 31,
                    "left": {
                        "key": 5,
                        "left": None,
                        "right": None
                    },
                    "right": None
                },
                "right": None
            },
            "right": {
                "key": 95,
                "left": {
                    "key": 92,
                    "left": {
                        "key": 63,
                        "left": None,
                        "right": None
                    },
                    "right": {
                        "key": 93,
                        "left": None,
                        "right": None
                    }
                },
                "right": None
            }
        }

buf = io.BytesIO()
print("setup finished")
for j in range(10):
    n = 100
    for i in range(n):
        draw_tree(buf, tree)
        print(f"{i}/{n}", end='\r')
    print(f"loop {j} finished")

Actual outcome

^CTraceback (most recent call last):
  File "<frozen runpy>", line 203, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "<path shortened>/glib-issue-reproduction.py", line 76, in <module>
    draw_tree(buf, tree)
    ~~~~~~~~~^^^^^^^^^^^
  File "<path shortened>/glib-issue-reproduction.py", line 27, in draw_tree
    plt.figure(figsize=(3, 3), dpi=300)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/matplotlib/pyplot.py", line 1096, in figure
    manager = new_figure_manager(
        num, figsize=figsize, dpi=dpi,
        facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
        FigureClass=FigureClass, **kwargs)
  File "/usr/lib/python3.14/site-packages/matplotlib/pyplot.py", line 576, in new_figure_manager
    return _get_backend_mod().new_figure_manager(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/matplotlib/backend_bases.py", line 3664, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/matplotlib/backend_bases.py", line 3669, in new_figure_manager_given_figure
    return cls.FigureCanvas.new_manager(figure, num)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/matplotlib/backend_bases.py", line 1836, in new_manager
    return cls.manager_class.create_with_canvas(cls, figure, num)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/matplotlib/backend_bases.py", line 2818, in create_with_canvas
    return cls(canvas_class(figure), num)
  File "/usr/lib/python3.14/site-packages/matplotlib/backends/_backend_gtk.py", line 147, in __init__
    self.window.set_icon_from_file(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        str(cbook._get_data_path(f"images/matplotlib.{icon_ext}")))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
gi.repository.GLib.GError: gdk-pixbuf-error-quark: Failed to load image “/usr/lib/python3.14/site-packages/matplotlib/mpl-data/images/matplotlib.svg”: Remote error: org.freedesktop.zbus.Error: i/o error (0)

Expected outcome

No error, execution continues after the signal is handled.

Additional information

To reproduce: Start spamming SIGINT (Ctrl+C) once "setup finished" is printed to stdout. Might take some attempts. Some non-fatal error messages appears sometimes.

Operating system

Arch

Matplotlib Version

3.11.2

Matplotlib Backend

gtk3agg

Python version

3.14.8

Jupyter version

no jupyter in use

Installation

Linux package manager

[@tacaswell edited to use triple backticks on traceback]

Activity

  1. tacaswell commented on Oct 29, 2026

    @tacaswell
    Member

    The first question is if you actually need the gui window or not. In this example you are saving the figure to a BytesIO object which I could beleive is either the actual use case or a simplication to report the bug. If it is the actual use case, then using the Agg backend (or directly create Figure/Axes objects and pass them into the networkx API and not importing pyplot at all!) will avoid the problem. It will also have the benefit of being faster and using less memory! If this is part of a bigger program that is hard to edit but you do not need the GUI anywhere in it, launch with MPLBACKEND=agg ENV set which will force the backend to Agg. You can also clear the X11 or wayland ENVS before you launch which will make our auto-backend selection logic select Agg because it will conclude no windowing system is available.

    If you do infact need the gtk3 windows then we have to actually sort this problem out. My guess is that glib/gtk3/something in that callchain is replaceing your handler with its own (in the polite way that makes sure to forward to what ever was there before so yours still looks like it works) to do something like "use sent sigint, cancel trying to do this disk I/O". If that is the case, then we may be out of luck as we can not control what future calls do with the signal handlers get installed so you have to wrap your functions in try...except and then decide if the ctrl-c meant "finish after this one" so you need to re-draw the last tree (being careful if you get re-interputed to not just keep looping!) or "stop now" in which case abandon the last tree. In either case it I'm guessing (from the shape of the code) that you could set should_exit in the except block and then let your normal exit/cleanup code do its thing.

    Trying a diffrenet GUI framework might also work, but I'm not sure if you are pinned to gtk3 by some other force.

  2. Akash-Dhanwate commented on Oct 30, 2026

    @Akash-Dhanwate

    Hi @tacaswell,

    I'd like to investigate this issue as a first contribution.

    From the traceback, the failure occurs while GTK3 is loading the Matplotlib SVG window icon via set_icon_from_file(). I'm interested in checking whether the problem is specific to SVG loading or whether SIGINT can interrupt GTK icon loading more generally.

    I plan to compare the original SVG path with PNG icon loading and temporarily disabling icon loading, while keeping the same SIGINT reproduction.

    If the results point to a Matplotlib-side mitigation, I can work on a minimal patch and regression test.

    Would this be a useful direction, or would you prefer investigating the GLib/GdkPixbuf signal interaction first?

  3. bonsaiiV commented on Nov 4, 2026

    @bonsaiiV
    Author

    @tacaswell thanks for the hint. I am using Figures directly now. It took some time to change all occurrences, but the speedup is well worth it.

    I am leaving this issue open since the bug still exists.

  4. Jayant-kernel commented on Nov 27, 2026

    @Jayant-kernel

    @tacaswell @bonsaiiV

    I can check whether this still happens on main. GTK3 loads the window icons through GdkPixbuf directly now, so I’ll compare it with 3.11.2 on the same setup and share what I find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions