diff --git a/qiskit_ibm_runtime/runtime_job.py b/qiskit_ibm_runtime/runtime_job.py index c1ebe86a4..c1ea1cafb 100644 --- a/qiskit_ibm_runtime/runtime_job.py +++ b/qiskit_ibm_runtime/runtime_job.py @@ -228,7 +228,8 @@ def wait_for_final_state(self, timeout: Optional[float] = None) -> None: """ if self._status not in JOB_FINAL_STATES and not self._is_streaming(): self._ws_client_future = self._executor.submit(self._start_websocket_client) - self._ws_client_future.result(timeout) + if self._is_streaming(): + self._ws_client_future.result(timeout) self.status() def stream_results( diff --git a/releasenotes/notes/wait-for-final-state-bug-02ebbe8df7a252d0.yaml b/releasenotes/notes/wait-for-final-state-bug-02ebbe8df7a252d0.yaml new file mode 100644 index 000000000..0dc3b888a --- /dev/null +++ b/releasenotes/notes/wait-for-final-state-bug-02ebbe8df7a252d0.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fixed a bug where :meth:`qiskit_ibm_runtime.RuntimeJob.wait_for_final_state` + would result in a NoneType error if the job already completed and + :meth:`qiskit_ibm_runtime.RuntimeJob.status` was called beforehand. + + diff --git a/test/integration/test_job.py b/test/integration/test_job.py index 07671e340..715dc5dd4 100644 --- a/test/integration/test_job.py +++ b/test/integration/test_job.py @@ -15,7 +15,7 @@ import random import time -from qiskit.providers.jobstatus import JobStatus +from qiskit.providers.jobstatus import JOB_FINAL_STATES, JobStatus from qiskit.test.decorators import slow_test from qiskit_ibm_runtime.constants import API_TO_JOB_ERROR_MESSAGE @@ -220,6 +220,16 @@ def test_wait_for_final_state(self, service): job.wait_for_final_state() self.assertEqual(JobStatus.DONE, job.status()) + @run_integration_test + def test_wait_for_final_state_after_job_status(self, service): + """Test wait for final state on a completed job when the status is updated first.""" + job = self._run_program(service) + status = job.status() + while status not in JOB_FINAL_STATES: + status = job.status() + job.wait_for_final_state() + self.assertEqual(JobStatus.DONE, job.status()) + @run_integration_test def test_job_creation_date(self, service): """Test job creation date."""