🐛 Fix: Memory leak in image processing endpoint#1513
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/1513
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 1 PendingAs of commit 25c346d with merge base 270a90f ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @dongxiaolong! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Oh right, wouldn't this however be fixed without at try/except statement as well and just using by adding |
Thanks for the feedback! You're right. I've simplified the code to just use |
@cpuhrsch
🐛 Fix: Memory leak causing process termination under high load
Issue Description
When processing large batches of images through the
/uploadendpoint in production, we encountered two critical issues:RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (
matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory.Under high load with multiple concurrent requests, the server process would eventually terminate due to memory exhaustion caused by unclosed matplotlib figures accumulating in memory.
Root Cause
The
/uploadendpoint creates new matplotlib figures for each request but doesn't properly clean them up, leading to:Changes Made
Before
After
Testing Done
AttributeError: __enter__error when usingwithstatementProduction Impact
This fix addresses a critical stability issue where image processing services would terminate under load due to memory exhaustion, requiring manual restarts and potentially causing service interruptions.
Let me know if you'd like me to make any adjustments to this PR or if you need additional testing information.