Skip to content

Fix memory leaks in Context#3220

Merged
fedimser merged 4 commits into
mainfrom
fedimser/context-weakref
May 13, 2026
Merged

Fix memory leaks in Context#3220
fedimser merged 4 commits into
mainfrom
fedimser/context-weakref

Conversation

@fedimser
Copy link
Copy Markdown
Contributor

@fedimser fedimser commented May 12, 2026

  • Use weakref when passing make_callable and make_class to Interpreter, which breaks circular reference (Context -> Interpreter -> callback -> Context) and allows the Context object (and Interpreter) to be garbage collected.
  • Do not register callables and classes for non-global Contexts in sys.modules. Use types.SimpleNamespace instead. Users access symbols for global Context via qdk.code, and symbols for other Contexts via context_variable.code.
    • This way we don't need to clear sys.modules when Context is garbage collected.
    • We still clear qdk.code when global context is invalidated on init().

Comment thread source/qdk_package/qdk/_context.py Outdated
@fedimser fedimser changed the title Use weakref to break reference loop in Context. Fix memory leaks in Context May 12, 2026
@fedimser fedimser marked this pull request as ready for review May 12, 2026 22:20
@fedimser fedimser requested a review from idavis May 12, 2026 22:20
@fedimser fedimser added this pull request to the merge queue May 12, 2026
Merged via the queue into main with commit ffea713 May 13, 2026
13 checks passed
@fedimser fedimser deleted the fedimser/context-weakref branch May 13, 2026 00:13
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