Skip to content

fix: fastapi memory leak only#3688

Merged
xrmx merged 16 commits intoopen-telemetry:mainfrom
artemziborev:fix/fastapi-memory-leak-only
Aug 23, 2025
Merged

fix: fastapi memory leak only#3688
xrmx merged 16 commits intoopen-telemetry:mainfrom
artemziborev:fix/fastapi-memory-leak-only

Conversation

@artemziborev
Copy link
Copy Markdown
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
@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
Copy Markdown
Contributor Author

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

@anuraaga
Copy link
Copy Markdown
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

Comment thread CHANGELOG.md
Comment thread CHANGELOG.md Outdated
@artemziborev artemziborev requested a review from emdneto August 16, 2025 06:28
@emdneto emdneto moved this to Ready for review in Python PR digest Aug 21, 2025
Comment thread CHANGELOG.md Outdated
Comment thread CHANGELOG.md
@xrmx xrmx merged commit 5fa222f into open-telemetry:main Aug 23, 2025
632 checks passed
@github-project-automation github-project-automation Bot moved this from Ready for review to Done in Python PR digest Aug 23, 2025
@pantuza
Copy link
Copy Markdown

pantuza commented Aug 28, 2025

Hi folks, thank you for solving this issue.
Do we have a release version that contains this fix?

sightseeker added a commit to sightseeker/opentelemetry-python-contrib that referenced this pull request Mar 11, 2026
* 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 <9735060+emdneto@users.noreply.github.com>
Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants