Visitar URL original
Stuck figure window after plt.close() in ipython with Qt5 · Issue #17109 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20221228012852/https://github.com/matplotlib/matplotlib/issues/17109
Skip to content
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

Stuck figure window after plt.close() in ipython with Qt5 #17109

Open
felixchenier opened this issue Apr 12, 2020 · 16 comments
Open

Stuck figure window after plt.close() in ipython with Qt5 #17109

felixchenier opened this issue Apr 12, 2020 · 16 comments
Labels
Milestone

Comments

@felixchenier
Copy link

felixchenier commented Apr 12, 2020 •

Bug report

Bug summary

When more than one figure is shown, then clicking on the last figure's close button or running plt.close() or plt.close('all') leaves a zombie figure behind. At first, figure windows are properly closed, but when switching back to the matplotlib python application, the last figure resuscitates and is not clickable nor closable anymore.

Doing the samething another time leaves an additional zombie figure window, and so on. The only way to close those non-responding windows is to quit ipython.

Code for reproduction

Running in ipython:

%matplotlib Qt5
import matplotlib.pyplot as plt
plt.figure()
plt.figure()
plt.close('all')

Actual outcome

No output, and the figures close. But then if we press alt-tab, the last figure resuscitates and does not respond to user input.

Expected outcome

The last figure should not resuscitate.

Matplotlib version

  • Operating system: macOS 10.15.4
  • Matplotlib version: 3.1.3
  • Matplotlib backend (Qt5Agg):
  • Python version: 3.7.7
  • Other libraries:

Installation under conda, default conda channel:

conda create -n test python=3.7
conda activate test
conda install ipython matplotlib pyqt

[TAC edited to remove comments in example code to make copy-pasting easier]

@tacaswell tacaswell added this to the v3.2.2 milestone Apr 12, 2020
@tacaswell
Copy link
Member

tacaswell commented Apr 12, 2020

If you create another new figure do the existing ones become responsive again? Alternatively dose plt.show(block=True) make it responsive again?

My suspicion is that with all the windows closed, we are no longer spinning the QT event loop so while the window exists in the display manager side, nothing in handling the input.

@felixchenier
Copy link
Author

felixchenier commented Apr 12, 2020

@tacaswell
Thanks for your answer.

If you create another new figure do the existing ones become responsive again? Alternatively dose plt.show(block=True) make it responsive again?

Unfortunately no, the "closed" figure stays there and still does not respond.

My suspicion is that with all the windows closed, we are no longer spinning the QT event loop so while the window exists in the display manager side, nothing in handling the input.

I also had this suspicion, I tried many combinations but what I see is that anytime I created more than one figure, this problem happens some way or the other and it's impossible do close those stuck figures.

@felixchenier
Copy link
Author

felixchenier commented Apr 12, 2020 •

I realized that I was not running the latest stable version, which is 3.2.1. I installed it from conda-forge and I experience the same bug.

@QuLogic
Copy link
Member

QuLogic commented May 12, 2020

Throwing a plt.pause(0.1) after does not fix it, nor does opening a new figure or calling plt.close('all') again. It seems like this window is completely detached from Matplotlib at some point, and Qt is just running it because it wants to.

This is also a very old bug, which reproduced back to 2.2.5. Possibly it's something to do with IPython versions and its Qt integration.

@QuLogic QuLogic changed the title Stuck figure window after plt.close() in ipython on macOS with Qt5 Stuck figure window after plt.close() in ipython with Qt5 May 13, 2020
@QuLogic QuLogic added GUI: Qt and removed OS: Apple labels May 13, 2020
@QuLogic
Copy link
Member

QuLogic commented May 13, 2020

This is also not macOS-specific.

@tacaswell tacaswell modified the milestones: v3.2.2, v3.4.0 May 13, 2020
@tacaswell
Copy link
Member

tacaswell commented May 13, 2020

I suspect that there is some race going on between IPython and Matplotlib trying to start up the global QApplication instance (but that is more of a hunch than anything solid).

@felixchenier
Copy link
Author

felixchenier commented May 22, 2020

I think I found a workaround. I manually connected the qt window destroy function to the figures' close events. My first tests seem to work, either using the pyplot close function, or the window close button (both cases had problems). Maybe it could serve as a direction to solve this bug in the real code.

%matplotlib qt
import matplotlib.pyplot as plt
                                                      
fig1 = plt.figure() 
fig1.canvas.mpl_connect('close_event', lambda _: fig1.canvas.manager.window.destroy()) 

fig2 = plt.figure() 
fig2.canvas.mpl_connect('close_event', lambda _: fig2.canvas.manager.window.destroy()) 

plt.close('all')

@felixchenier
Copy link
Author

felixchenier commented May 22, 2020

Interestingly, the workaround doesn't work anymore as soon as there's a plt.pause while the figures are shown.

Works:

%matplotlib qt
import matplotlib.pyplot as plt
                                                      
fig1 = plt.figure() 
fig1.canvas.mpl_connect('close_event', lambda _: fig1.canvas.manager.window.destroy()) 

fig2 = plt.figure() 
fig2.canvas.mpl_connect('close_event', lambda _: fig2.canvas.manager.window.destroy()) 

plt.close('all')

Doesn't work:

%matplotlib qt
import matplotlib.pyplot as plt
                                                      
fig1 = plt.figure() 
fig1.canvas.mpl_connect('close_event', lambda _: fig1.canvas.manager.window.destroy()) 

fig2 = plt.figure() 
fig2.canvas.mpl_connect('close_event', lambda _: fig2.canvas.manager.window.destroy()) 

plt.pause(0.1)
plt.close('all')

There seems to effectively has something with the event loop (I messed a bit with MPL's code and it's the start_event_loop from plt.pause that seems to cause this difference).

@tfetherolf
Copy link

tfetherolf commented Oct 3, 2020

I want to add that I encounter what seems to be a related issue when creating many figures in a script that is run from the command line. While the script does not hang on figures when plt.close() is used, creating a large number of figures in a loop shows that they are not properly closed when the open file limit is reached.

OSError: [Errno 24] Too many open files: 'figurename255.png'

Increasing or removing the limit on the number of open files allowed is not an adequate solution for me since this would cause extreme CPU slowdowns from the large number of figures I am creating.

Versions

Operating system: macOS 10.15.7
Python version: 3.8.5
Matplotlib version: 3.3.1
Matplotlib backend: MacOSX
Spyder version: 4.1.5
Qt version: 5.9.7
PyQt5 version: 5.9.2
iPython version: 7.18.1

Code for Reproduction

Running as a script from command line.

import matplotlib.pyplot as plt

for i in range(1000):
    fig = plt.figure()
    plt.savefig('figurename'+str(i)+'.png')
    plt.close()

Adding fig.canvas.mpl_connect('close_event', lambda _: fig.canvas.manager.window.destroy()) after fig = plt.figure() does not prevent the script problem for me, but does work for closing windows when using the iPython console in Spyder.

Current Workaround

Using plt.clf() instead of opening/closing many figures is an adequate workaround in my case.

import matplotlib.pyplot as plt

fig = plt.figure()
for i in range(1000):
    plt.savefig('figurename.png')
    plt.clf()

@jklymak
Copy link
Member

jklymak commented Oct 3, 2020

@tfetherolf, this seems somewhat different since its from the command line and never uses an interactive backend, can you open a new issue for this?

@tacaswell
Copy link
Member

tacaswell commented Oct 16, 2020

@tfetherolf If you do matplotlib.use('agg') before creating your first figure does it fix the problem?

@tacaswell
Copy link
Member

tacaswell commented Oct 16, 2020

In both cases my new suspicion is that for various reasons we defer setting up the logic to tear down the GUI windows until after the GUI window is shown, however if you close the figure before it is shown we end up in a state where on one hand we have asked a GUI to be shown, then tear down the Python side logic, then the window shows and we get a zombie window (with no Python side way to get a hold of it!) and no logic of when to close it.

@tfetherolf
Copy link

tfetherolf commented Oct 19, 2020

@tacaswell Adding matplotlib.use('agg') to the top of my script (before the first figure) does prevent the crash from happening.

@tacaswell
Copy link
Member

tacaswell commented Oct 30, 2020

doing a gc.collect() will clear the window so there is something stuck in garbage collection purgetory that is delaying tearing down the Python object (even if we can not get to it any more) which is in turn delaying tearing down the c++ object which means it is alive enough for Qt to try to show it but not alive enough to be usable.

This is connected to #18609 (comment) in the sense that we are not sure if "close" means "hide" or "destroy".

@felixchenier
Copy link
Author

felixchenier commented Oct 30, 2020

Hello @tacaswell
On my side the garbage collector doesn't clear the window (on macOS). Just so that you know...

@tacaswell
Copy link
Member

tacaswell commented Oct 30, 2020

Interesting, still suspect it is a fussy reference counting issue, but apparently a platform dependent one...

@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 27, 2021
@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Sep 25, 2021
@QuLogic QuLogic modified the milestones: v3.6.0, v3.7.0 Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants