fastapi: Fix uninstrument memory leak#3683
fastapi: Fix uninstrument memory leak#3683artemziborev wants to merge 10 commits intoopen-telemetry:mainfrom
Conversation
…nto refactor-flask-spanattributes
…ist to avoid memory leaks
|
Hi! All checks are green — would love to get a review when you have time 🙌 Let me know if you'd like anything changed! |
emdneto
left a comment
There was a problem hiding this comment.
Thanks. SGTM. We don't need the attributes fix for that PR. I suggest we minimize the scope of this PR only to the actual fix for FastAPI.
| ### Fixed | ||
|
|
||
| - `opentelemetry-instrumentation-fastapi`: Fix memory leak in `uninstrument_app()` method by properly removing apps from the tracking set | ||
| ([#XXXX](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/XXXX)) |
There was a problem hiding this comment.
| ([#XXXX](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/XXXX)) | |
| ([#3683](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3683)) |
There was a problem hiding this comment.
thank you for the repro, but can you instead create an issue with that instead and remove from the PR?
There was a problem hiding this comment.
I believe a better way to test would be just to add this to somewhere in test_fastapi_instrumentation.py:
def test_fastapi_app_is_collected_after_uninstrument(self):
import gc
import weakref
app = fastapi.FastAPI()
otel_fastapi.FastAPIInstrumentor.instrument_app(app)
app_ref = weakref.ref(app)
del app
gc.collect()
self.assertIsNone(app_ref())
There was a problem hiding this comment.
Just a note related to #3683 (comment), it's probably better to remove uninstrument_app from this test, since it should still not leak
|
Thanks @artemziborev - I had randomly noticed the same issue in starlette and fixed by using a b9a78e7#diff-a4901479b7a21ad4f5d45a7f3e47b2ada3f9d8ae4ae7f9e68c2001e8b8067611R303 I think it's more reliable to use but it's essentially dead code since |
Yup. +1 if the author is open to making this change |
|
Thanks for the reviews! Per feedback to minimize scope to FastAPI only (and drop unrelated changes), I opened a new, focused PR: #3688. That PR includes:
Closing this PR in favor of #3688. 🙏 |
* fix(fastapi-instrumentation): properly remove app from instrumented list to avoid memory leaks * fix(fastapi-instrumentation): fix tests and finalize memory leak fix * docs(changelog): add FastAPI uninstrument memory leak fix with PR references (#3683) * test(fastapi): add GC-based app collection test; refactor tracking to WeakSet and safe discard * docs(changelog): reference FastAPI memory leak fix PR (#3688) * refactor(fastapi): drop __del__ as WeakSet handles cleanup * chore(fastapi): formatting after ruff auto-fix * chore(test-fastapi): ruff import order * Update comment to clarify purpose of removing app from WeakSet * fix: codereview comments * Apply suggestions from code review --------- Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
* fix(fastapi-instrumentation): properly remove app from instrumented list to avoid memory leaks * fix(fastapi-instrumentation): fix tests and finalize memory leak fix * docs(changelog): add FastAPI uninstrument memory leak fix with PR references (open-telemetry#3683) * test(fastapi): add GC-based app collection test; refactor tracking to WeakSet and safe discard * docs(changelog): reference FastAPI memory leak fix PR (open-telemetry#3688) * refactor(fastapi): drop __del__ as WeakSet handles cleanup * chore(fastapi): formatting after ruff auto-fix * chore(test-fastapi): ruff import order * Update comment to clarify purpose of removing app from WeakSet * fix: codereview comments * Apply suggestions from code review --------- Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
Description
Fixes a memory leak in the
FastAPIInstrumentor.uninstrument_app()method, where the FastAPIappinstance was not being removed from the internal_instrumented_appslist. This caused a retained reference even after uninstrumentation, potentially resulting in memory usage growth over time.Fixes #3683
Type of change
How Has This Been Tested?
Added a regression test to ensure that after
uninstrument_app()is called, theappis no longer present in the_instrumented_appslist.Steps to reproduce:
FastAPIInstrumentor.instrument_app()uninstrument_app()_instrumented_apps)Tested using:
Does This PR Require a Core Repo Change?
Checklist: