Repository navigation
Commit c440779
Close matplotlib figures after rendering them in
* Close matplotlib figures after rendering in gr.Plot
Figures returned to gr.Plot were never closed after being encoded,
so every plot update left another open figure in pyplot's global
registry. Long-running apps leaked memory and eventually hit
matplotlib's "More than 20 figures have been opened" warning.
Close the figure in Plot.postprocess once it has been encoded.
Only Figure instances are closed; the legacy path where a handler
returns the pyplot module itself is left untouched. savefig still
works on a closed figure, so apps that keep a figure around and
return it repeatedly are unaffected (covered by a test).
Fixes #11701
* add changeset
* Only close Figure instances, use png in equality test
Address Copilot review comments: guard plt.close() with an
isinstance check against matplotlib.figure.Figure so non-Figure
matplotlib objects cannot raise TypeError, and force png format in
test_postprocess_accepts_closed_figure so the byte-equality
assertion does not depend on webp encoder stability.
* Close figure in finally without importing pyplot
Per Copilot review: postprocess can run at component init outside
MatplotlibBackendMananger, so look up an already-imported pyplot via
sys.modules instead of importing it (a figure can only be in the
registry if pyplot was imported), and close in a finally block so
the figure is cleaned up even if encoding fails.
* Guard Figure import behind pyplot presence check
Per Copilot review: only import matplotlib.figure inside the finally
block after confirming pyplot is already in sys.modules, so the
cleanup can never raise and mask an encoding error.
* Assert figure registry is empty after postprocess
Per Copilot review: close pre-existing figures first and assert
plt.get_fignums() is empty instead of checking a single figure
number, making the regression test slightly stronger.
* Use try/except ImportError idiom, hoist test imports
Address abidlabs's review comments: replace the sys.modules lookup
with the codebase's usual try-import/except-ImportError pattern, and
move the matplotlib import to the top of the test file.
* Return early when matplotlib import fails
Address abidlabs's review comment: replace the pass in the
ImportError branch with an early return of the encoded PlotData,
dropping the try/finally so the guard-clause return is safe.
---------
Co-authored-by: gradio-pr-bot <[email protected]>gr.Plot (#13522)1 parent 2d6eb2a commit c440779
3 files changed
Lines changed: 37 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
146 | 153 | | |
147 | 154 | | |
148 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
61 | 86 | | |
62 | 87 | | |
63 | 88 | | |
| |||
0 commit comments