Skip to content

Cache: avoid saving object references#534

Merged
braddf merged 2 commits intomainfrom
fix/cache-ref-obj-chain
Mar 2, 2026
Merged

Cache: avoid saving object references#534
braddf merged 2 commits intomainfrom
fix/cache-ref-obj-chain

Conversation

@braddf
Copy link
Contributor

@braddf braddf commented Mar 2, 2026

Pull Request

Description

Hopefully this is a demonstrable fix for the API cache memory leak. Looks like our custom cache middleware saves references to full Pydantic/SQLAlchemy objects etc. directly from the route handlers that are not cleaned up, and can be multiple times the size of the intended cached responses. These are orphaned when the cache is "cleared" and build up over time. This PR solves this by serialising the objects before caching them, severing the refs and ensuring they aren't swept under the rug in the first place.

Fixes #

How Has This Been Tested?

  • Tested on local API to dev DB and many concurrent API requests across large-response endpoints with Postman.

Postman Results

image
  • Fresh from API restart
image
  • Before fix, after making a string of API calls e.g. /forecast/all or /national/forecast with diff query params
image
  • Same, further calls made
image
  • Next API call after cache should have expired, and even with an explicit garbage collection – memory allocations persist
image
  • After serialisation fix, API calls made with diff query params over short period; attributed to FastAPI encoder
image
  • Same, further varying API calls within 2 min period
image
  • After cache expires, memory freed and values much lower

If your changes affect data processing, have you plotted any changes? i.e. have you done a quick sanity check?

  • Yes

Checklist:

  • My code follows OCF's coding style guidelines
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked my code and corrected any misspellings

@braddf braddf requested a review from peterdudfield March 2, 2026 08:42
@braddf braddf self-assigned this Mar 2, 2026
top = snapshot.statistics("lineno")[:20]
return [{"file": str(s.traceback), "size_mb": s.size / 1024 / 1024} for s in top]


Copy link
Collaborator

Choose a reason for hiding this comment

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

nice, this will be useful

@peterdudfield
Copy link
Collaborator

nice comments here btw

@braddf braddf merged commit 7b7eb63 into main Mar 2, 2026
3 checks passed
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.

2 participants