Skip to content

FastAPIInstrumentor.uninstrument_app() doesn't remove the app from the static list of instrumented apps, causing memory leaks #3289

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

Open
brianmedigate opened this issue Feb 24, 2025 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@brianmedigate
Copy link

Describe your environment

OS: Ubuntu 22.04
Python version: Python 3.11.11
Package version: 0.45b0

What happened?

Calling FastAPIInstrumentor.uninstrument_app() doesn't remove the app parameter from the _InstrumentedFastAPI._instrumented_fastapi_apps set. If instrumenting and uninstrumenting is done repeatedly, for example in a test suite, this can lead to leaking significant amount of memory.

Steps to Reproduce

import sys

from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

app = FastAPI()
print(sys.getrefcount(app))
FastAPIInstrumentor.instrument_app(app)
print(sys.getrefcount(app))
FastAPIInstrumentor.uninstrument_app(app)
print(sys.getrefcount(app))

Expected Result

After the call to uninstrument_app() the refcount should be the same as it was before calling instrument_app()

Actual Result

The refcount is one greater.

Additional context

The mechanism of storing references to the apps was implemented in #1258.

Would you like to implement a fix?

Yes

@brianmedigate brianmedigate added the bug Something isn't working label Feb 24, 2025
@brianmedigate
Copy link
Author

@xrmx xrmx added the good first issue Good for newcomers label Feb 28, 2025
@pvital
Copy link

pvital commented Mar 3, 2025

@xrmx, could you please assign this one to me? TIA.

@rahulhacker
Copy link
Contributor

@xrmx : in case if no one is working, can you please assign to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants