Skip to content

fix: fastapi memory leak only #3688

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
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

artemziborev
Copy link
Contributor

Description

Fix FastAPI instrumentation memory leak by tracking instrumented apps with a WeakSet instead of a strong-referenced set. This avoids retaining strong references to fastapi.FastAPI instances, allowing them to be garbage-collected even if uninstrument_app() isn’t called. Also aligns with the approach used in the Starlette instrumentation.

Scope minimized to FastAPI only:

  • Use WeakSet for _instrumented_fastapi_apps and discard() on removal
  • Add a GC-based test that verifies the app is collected without calling uninstrument_app()
  • Remove the separate memory leak test file and integrate the test into test_fastapi_instrumentation.py
  • Update CHANGELOG (Unreleased → Fixed)

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added unit test:
    • test_fastapi_app_is_collected_after_instrument in test_fastapi_instrumentation.py, which:
      • creates an app
      • instruments it
      • drops the strong reference and forces gc.collect()
      • asserts the weakref is cleared (no leak)
  • Existing FastAPI test suite continues to apply

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@artemziborev artemziborev requested a review from a team as a code owner August 12, 2025 13:09
@@ -358,6 +359,10 @@ def uninstrument_app(app: fastapi.FastAPI):
app.middleware_stack = app.build_middleware_stack()
app._is_instrumented_by_opentelemetry = False

# Remove the app from the set of instrumented apps to prevent memory leaks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since WeakSet accomplishes avoiding memory leaks here, I think this is more Avoid calling u instrument twice if the instrumentation is later disabled or such

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment to clarify purpose of removing app from WeakSet

@artemziborev artemziborev requested a review from anuraaga August 12, 2025 13:57
@artemziborev artemziborev changed the title Fix/fastapi memory leak only fix: fastapi memory leak only Aug 13, 2025
@artemziborev
Copy link
Contributor Author

Hi @anuraaga, just a gentle reminder — could you take a look at this PR? All checks have passed 👍

@anuraaga
Copy link
Contributor

Hi @artemziborev - LGTM. I had already approved since was just a comment nit, but I'm not a maintainer so it's a grey check

CHANGELOG.md Outdated
@@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `opentelemetry-instrumentation-fastapi`: Fix memory leak in `uninstrument_app()` by properly removing apps from the tracking set
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to move that to # Unreleased section

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to #Unreleased section

CHANGELOG.md Outdated
@@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `opentelemetry-instrumentation-fastapi`: Fix memory leak in `uninstrument_app()` by properly removing apps from the tracking set
([#3688](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3688), [#3683](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3683))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
([#3688](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3688), [#3683](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3683))
([#3688](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3688)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added correct link to PR

@artemziborev artemziborev requested a review from emdneto August 16, 2025 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants