Skip to content

Fix Memory Leak in RunRouter#373

Open
genematx wants to merge 3 commits into
bluesky:mainfrom
genematx:oom-fix-resource-cache
Open

Fix Memory Leak in RunRouter#373
genematx wants to merge 3 commits into
bluesky:mainfrom
genematx:oom-fix-resource-cache

Conversation

@genematx
Copy link
Copy Markdown
Contributor

Fix memory leak: clean up _resources and _stream_resources in RunRouter.stop()

Motivation and Context

_resources was keyed by resource_uid but stop() was popping with start_uid, so every Resource document from every completed run accumulated in _resources indefinitely (the pop was always a no-op).

_stream_resources was never cleaned up in stop() at all.

Description

Fix both by introducing _start_to_resources and _start_to_stream_resources reverse-lookup maps (mirroring the existing _start_to_descriptors pattern). These are populated in resource() and stream_resource() and used in stop() to efficiently remove all per-run entries without an O(N) scan.

How Has This Been Tested?

Adds a regression test covering both code paths across multiple runs.

…er.stop()

_resources was keyed by resource_uid but stop() was popping with start_uid,
so every Resource document from every completed run accumulated in _resources
indefinitely (the pop was always a no-op).

_stream_resources was never cleaned up in stop() at all.

Fix both by introducing _start_to_resources and _start_to_stream_resources
reverse-lookup maps (mirroring the existing _start_to_descriptors pattern).
These are populated in resource() and stream_resource() and used in stop()
to efficiently remove all per-run entries without an O(N) scan.

Adds a regression test covering both code paths across multiple runs.
Adds missing docstring for the `hints` field in RunStart, which was
present in the JSON schema but absent from the generated TypedDict.
Fixes the test_generated_json_matches_typed_dict CI failure.
@danielballan
Copy link
Copy Markdown
Member

Old resource documents do not have a run_start reference (under any name). I believe we intentionally retain/leak Resources for this reason.

If RunRouter is used to migrate old Resource documents, this issue is still relevant today. Perhaps we could put it behind a feature flag that does not leak by default, but can leak when we need to push very old Resource documents through a RunRouter.

@genematx
Copy link
Copy Markdown
Contributor Author

Hmmm, interesting. thanks Dan

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