Skip to content

Commit

Permalink
Set _active flag to false if session already closed (#1780)
Browse files Browse the repository at this point in the history
* Set _active flag to false if session already closed

* release note

* Add test
  • Loading branch information
kt474 authored Jul 3, 2024
1 parent c607150 commit 040f8af
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions qiskit_ibm_runtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ def from_id(
response = service._api_client.session_details(session_id)
backend = response.get("backend_name")
mode = response.get("mode")
state = response.get("state")
class_name = "dedicated" if cls.__name__.lower() == "session" else cls.__name__.lower()
if mode != class_name:
raise IBMInputValueError(
f"Input ID {session_id} has execution mode {mode} instead of {class_name}."
)

session = cls(service, backend)
if state == "closed":
session._active = False
session._session_id = session_id
return session

Expand Down
2 changes: 2 additions & 0 deletions release-notes/unreleased/1780.feat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
If jobs are run in a session created with :meth:`QiskitRuntimeService.Session.from_id` where the
session is already closed, the jobs are rejected immediately.
3 changes: 3 additions & 0 deletions test/integration/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def test_session_from_id(self, service):

new_session = Session.from_id(session_id=session._session_id, service=service)
self.assertEqual(session._session_id, new_session._session_id)
self.assertTrue(new_session._active)
new_session.close()
self.assertFalse(new_session._active)

with self.assertRaises(IBMInputValueError):
Batch.from_id(session_id=session._session_id, service=service)

0 comments on commit 040f8af

Please sign in to comment.