Close leftover figure in plot/cplot/splot on error (fixes #1007) - #1123
Merged
Conversation
plot(), cplot() and splot() create the matplotlib figure up front but only show or save it at the very end. If the user-supplied function raised an exception that is not in plot_ignore (e.g. plot(lambda t: 'a', (0, 2))), the figure was created and never closed, so it lingered in matplotlib's global figure registry and was displayed as a stale blank window on the next successful call. Wrap the body of each function in try/except and close the internally created figure before re-raising, so nothing is left behind. Figures supplied by the caller via axes= are untouched. Fixes mpmath#1007.
skirpichev
self-requested a review
July 9, 2026 23:27
skirpichev
requested changes
Jul 9, 2026
skirpichev
left a comment
Collaborator
There was a problem hiding this comment.
Sorry, no. This touches too much code without good reason.
Can't you just add another except block after except ctx.plot_ignore? For example (please don't use bare except):
diff --git a/mpmath/visualization.py b/mpmath/visualization.py
index d87beb67..e0c7b1fa 100644
--- a/mpmath/visualization.py
+++ b/mpmath/visualization.py
@@ -81,6 +81,9 @@ def plot(ctx, f, xlim=[-5,5], ylim=None, points=200, file=None, dpi=None,
if segment:
segments.append(segment)
segment = []
+ except Exception:
+ pylab.close(fig)
+ raise
if segment:
segments.append(segment)
for segment in segments:
skirpichev
approved these changes
Jul 10, 2026
skirpichev
left a comment
Collaborator
There was a problem hiding this comment.
OK, I did suggested changes.
Collaborator
|
Thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
plot(),cplot()andsplot()create the matplotlib figure up front(
pylab.figure()/plt.subplots()) but only callshow()/savefig()at thevery end. When the user-supplied function raises an exception that is not in
plot_ignore(for exampleplot(lambda t: 'a', (0, 2)), which raisesTypeErrorinsidectx.isnan(v)), the exception propagates out of the function,but the figure that was already created is never closed. It stays registered in
matplotlib's global figure manager and is then displayed as a stale blank window
on the next successful call. This is issue #1007.
Fix
Wrap the body of each of the three functions in
try/except. On any exception,close the figure we created internally and re-raise. The cleanup only runs for
figures owned by the function (
fig is not None); when the caller passes theirown
axes=,figisNoneand their figure is left untouched. Successfulplotting is unchanged.
Tests / Verification
test_issue_1007inmpmath/tests/test_visualization.py. It drives allthree functions with a function that raises and asserts
pylab.get_fignums() == []afterwards. The test fails on the current code(a leftover figure
[1]remains) and passes with the fix.test_axesand the module doctests still pass.plot/cplot/splotcalls still save/showand that passing a caller-supplied
axesdoes not close the caller's figure,either on success or when the function raises.
flake518reports no issues on the changed files.CHANGESentry under Bug fixes.Disclosure: prepared with AI assistance; reviewed and verified locally.