-
Notifications
You must be signed in to change notification settings - Fork 757
fastapi: Fix uninstrument memory leak #3683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fastapi: Fix uninstrument memory leak #3683
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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
([#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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note related to #3683 (comment), it's probably better to remove uninstrument_app
from this test, since it should still not leak
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense @anuraaga
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 |
Description
Fixes a memory leak in the
FastAPIInstrumentor.uninstrument_app()
method, where the FastAPIapp
instance was not being removed from the internal_instrumented_apps
list. 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, theapp
is no longer present in the_instrumented_apps
list.Steps to reproduce:
FastAPIInstrumentor.instrument_app()
uninstrument_app()
_instrumented_apps
)Tested using:
Does This PR Require a Core Repo Change?
Checklist: