diff --git a/qiskit_ibm_runtime/session.py b/qiskit_ibm_runtime/session.py index 29bad2357..fdc125183 100644 --- a/qiskit_ibm_runtime/session.py +++ b/qiskit_ibm_runtime/session.py @@ -369,6 +369,7 @@ 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( @@ -376,6 +377,8 @@ def from_id( ) session = cls(service, backend) + if state == "closed": + session._active = False session._session_id = session_id return session diff --git a/release-notes/unreleased/1780.feat.rst b/release-notes/unreleased/1780.feat.rst new file mode 100644 index 000000000..4ebc561a6 --- /dev/null +++ b/release-notes/unreleased/1780.feat.rst @@ -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. \ No newline at end of file diff --git a/test/integration/test_session.py b/test/integration/test_session.py index a0d99463c..8ffffe1d9 100644 --- a/test/integration/test_session.py +++ b/test/integration/test_session.py @@ -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)