Open bluenote10 opened 9 months ago
Although you are only using Matplotlib from the main thread, some of the objects that Matplotlib creates are getting garbage collected on the background threads (note all of the tk related code is in __del__
). The weakness of this explanation as to the problem is that it implies any mixing of Tk with threads runs the risk of this issue which seems a very broad swath of buggy programs.
A possible test to of this would be to add
def really_plot(*args, **kwargs):
import gc
plot_something(*args, **kwargs)
gc.collect()
and then call that function in your loop instead.
A possible test to of this would be to add
Indeed, with an explicit garbage collection this pattern seems to work fine, pretty cool, thanks!
Feel free to close the issue if there isn't anything actionable on the matplotlib side. Explicit garbage collection sounds like a reasonable work-around. For instance, I fancy just writing a small @with_garbage_collection
decorator that I could wrap around my plotting functions.
A few random ideas what could be done on the matplotlib side (wild guesses):
gc.collect()
e.g. in plt.close()
explicitly? This may have too much of an impact and performance though, and is also just a heuristic after all..close()
or .release()
function, and __del__
basically just calls that as well. Then plt.close()
could forward explicit calls to these .close()
/.release()
functions. The path via __del__
would only be taken in case no explicit call path did the cleanup already. Not sure if this is makes any sense.Calling gc.collect()
can be very expensive when there are a lot of user-space long lived collection like objects so we can not really call gc.collect()
. To ammeliorate that we called gc.collect(1)
, however that lead to some pathological cases where cyclic gc never ran and we had effectively unbounded memory leakage (https://github.com/matplotlib/matplotlib/issues/23701#issuecomment-1223390883 and https://github.com/matplotlib/matplotlib/pull/23712).
We do have a destroy
method on the FigureManager
called from https://github.com/matplotlib/matplotlib/blob/ff0497cc042332313afd3d2e9ba8f949c3857896/lib/matplotlib/_pylab_helpers.py#L66 and https://github.com/matplotlib/matplotlib/blob/ff0497cc042332313afd3d2e9ba8f949c3857896/lib/matplotlib/backends/_backend_tk.py#L560-L584 is the implementation for tk. There is a bunch of complexity there to delay tearing things down to avoid other tk issues (but the way it is delayed should still put it on the main thread because it is run via a callback on the event loop).
However, I suspect that there is more we can do in there to manually break any reference cycles. Probably something like https://github.com/matplotlib/mpl-gui/blob/aec794a5f5f639208c6504d05a32bffe433b42a9/mpl_gui/__init__.py#L321-L326 to break the cycle between the canvas and the Figure
(unfortunately we have had a hard cycle there ~forever and I am quite sure that making either one a weakref will break some users).
If I may bring 2 cents here, I was using tkinter
for some totally unrelated project to yours, and got the same behaviour (same error, same fix with gc.collect
)
The problem for me was that I was calling the quit()
function before destroy()
function, reversing the order worked like a charm.
Hope that'll help
attn @richardsheridan Do we need to audit how we stop the main loop again?
This is really a tkinter problem, releasing resources that need an event loop in the __del__
method. If you've got cycles, and we do, sometimes that can cause the items to be GC'd from a thread. So, if you're mixing tkinter and threads, you can't be stopping and starting the event loop. There is a small amount of wiggle room, which is probably why this is so nondeterministic:
If you hit one of these calls in a thread, you have one second to re-activate the event loop or it will error out. I've advocated for removing this grace period as being more harmful than helpful.
I do an always-on GUI and event loop, and have tasks like computation and data loading running in background threads or processes which call back to the event loop, so I never see things like this.
The only way matplotlib could help is by guaranteeing that its tkinter object structure is entirely cleaned up before plt.show()
returns in the default case. plt.pause()
is right out. Even then I'm sure there will be edge cases.
I've been trying to understand this error in a similar project using joblib Parallel, following interactive plots with matplotlib and tkinter. This is the best, clearest discussion of the issue I've found and actually gives a clear workaround to implement.
So, just popping in to say thanks for writing it down!
Bug summary
I recently started running into the infamous
Tcl_AsyncDelete: async handler deleted by the wrong thread
crash quite frequently despite using matplotlib strictly from the main thread.This occurs every time when using a pytorch
DataLoader
in multi-processing mode in combination with doing some plotting. I'm aware that matplotlib itself is not thread safe. Note however that the multiprocessing usage is internal to pytorch. The usage of matplotlib plotting is purely from the main process/thread, so there shouldn't be such an interference between pytorch and matplotlib leading to crashes.Also, the usual work-around of enforcing the
agg
backend is not applicable in my use cases: In some cases I do want to do some interactive plotting during training neural networks, because it offers more possibilities to inspect what a neural model is doing exactly. (And not using theDataLoader
in multi-processing mode slows down the training a lot.)The pytorch version for reproduction is 2.1.2.
Code for reproduction
Actual outcome
The snippets runs for a while and eventually crashes. The crashes are non-deterministic in nature, but the snippet crashes for me typically after half a minute or so. The output is (truncated the messages up to the crash):
Note that the reported pid / thread id for the plotting code is obviously just the main pid / thread id.
Expected outcome
It should not crash.
Additional information
I'm not sure, but it may have worked, because I'm using this pattern for a long time, and it only started crashing recently. However due to the non-deterministic nature of the crash it is hard to verify. It may just be that my previous usages of the pattern where not plotting "aggressively" enough.
Operating system
Ubuntu 22.04
Matplotlib Version
3.8.2
Matplotlib Backend
TkAgg
Python version
Python 3.10.12
Jupyter version
none
Installation
pip