Make Executor session handling safer#377
Merged
MoritzWillmann merged 21 commits intosQUlearn:developfrom Jan 22, 2026
Merged
Conversation
ProfessorNova
approved these changes
Jan 22, 2026
MoritzWillmann
added a commit
that referenced
this pull request
Apr 7, 2026
* move destructor up in file * replace destructor with weakref finalize * add conditional context management * always add finalizer if session is present * add some tests * change mock_session scope to function * only register finalizer if not present * use static method call * use _cleanup_session directly on session ref * remove unnecessary test, add finally * clean up tests * only open session if needed in context manager * add docstrings and lint statements * add SessionContextMisuseWarning * adapt docstrings to encourage context managed use * encorage use of context manager in user guide * adapt example_executor_qiskit * use new ibm platform details * adapt backend mitigation example to use context manager * new black
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The current handling of Qiskit IBM Runtime
Sessionsin theExecutorclass is extremely unsafe. In most cases the user will have to manually close the session after use or else it could use valuable computation time. In my initial conception this mostly happens in two cases (could be more):Executorcreates theSessionand the destructor (which was supposed to close the session) is never calledExecutoris kept in the context long time after usage (e.g. in the scope of a notebook) and thus theSessionis never closedIt could also be related to the
Executornot being released properly after deletion (which could also be related to #358), but this is just a hunch and would need further investigation.Approach
My approach in this PR to mitigate these problems:
weakref.finalizerinstead of__del__, since it's apparantly safer and always assign whenSessionis used for cases when context managed usage is not feasible or users forget to useStill Open Points