Skip to content

Make Executor session handling safer#377

Merged
MoritzWillmann merged 21 commits intosQUlearn:developfrom
MoritzWillmann:executor_session_handling
Jan 22, 2026
Merged

Make Executor session handling safer#377
MoritzWillmann merged 21 commits intosQUlearn:developfrom
MoritzWillmann:executor_session_handling

Conversation

@MoritzWillmann
Copy link
Copy Markdown
Collaborator

@MoritzWillmann MoritzWillmann commented Jan 20, 2026

Problem

The current handling of Qiskit IBM Runtime Sessions in the Executor class 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):

  1. Something goes wrong after the Executor creates the Session and the destructor (which was supposed to close the session) is never called
  2. The Executor is kept in the context long time after usage (e.g. in the scope of a notebook) and thus the Session is never closed

It could also be related to the Executor not 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:

  • Encourage context managed use such as
with Executor(...) as executor:
    ...
  • Use weakref.finalizer instead of __del__, since it's apparantly safer and always assign when Session is used for cases when context managed usage is not feasible or users forget to use

Still Open Points

  • fix tests, it seems the finalizer is not called in some of the tests
  • adapt documentation to encourage context managed approach
  • create warning when not using context managed approach
  • cleanup to make more robust and conform to styling

@MoritzWillmann MoritzWillmann marked this pull request as ready for review January 21, 2026 14:51
@MoritzWillmann MoritzWillmann merged commit b58a3ca into sQUlearn:develop Jan 22, 2026
7 checks passed
@MoritzWillmann MoritzWillmann deleted the executor_session_handling branch January 22, 2026 10:23
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
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