From bb7c0c2b612563fd9cd226589f7a11fd9b29943e Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sat, 19 Aug 2023 09:49:57 +0100 Subject: [PATCH 1/8] update `test_cancel_job_running` in `test_job.py` to wait_for_status() --- test/integration/test_job.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/test_job.py b/test/integration/test_job.py index 0beb0f435..70848348c 100644 --- a/test/integration/test_job.py +++ b/test/integration/test_job.py @@ -161,6 +161,7 @@ def test_cancel_job_running(self, service): service, circuits=[ReferenceCircuits.bell()] * 10, ) + wait_for_status(job, JobStatus.CANCELLED) if not cancel_job_safe(job, self.log): return time.sleep(10) # Wait a bit for DB to update. From 9fbdec9bbe416333bea029d2c02d3a00b1dc12aa Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sat, 19 Aug 2023 09:52:27 +0100 Subject: [PATCH 2/8] update `cancel()` in `runtime_job.py` to include `ex.status_code == 204` --- qiskit_ibm_runtime/runtime_job.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_ibm_runtime/runtime_job.py b/qiskit_ibm_runtime/runtime_job.py index 43a5d0f62..26dac1211 100644 --- a/qiskit_ibm_runtime/runtime_job.py +++ b/qiskit_ibm_runtime/runtime_job.py @@ -245,7 +245,7 @@ def cancel(self) -> None: try: self._api_client.job_cancel(self.job_id()) except RequestsApiError as ex: - if ex.status_code == 409: + if ex.status_code == 409 or ex.status_code == 204: raise RuntimeInvalidStateError(f"Job cannot be cancelled: {ex}") from None raise IBMRuntimeError(f"Failed to cancel job: {ex}") from None self.cancel_result_streaming() From 3569bd88ca4fa66d89c09a6dd9f7c34507a371dc Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sat, 19 Aug 2023 11:41:44 +0100 Subject: [PATCH 3/8] release notes fix_test_cancel_job_running-771511870d82d4e4.yaml --- .../fix_test_cancel_job_running-771511870d82d4e4.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml diff --git a/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml b/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml new file mode 100644 index 000000000..86dc2c973 --- /dev/null +++ b/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml @@ -0,0 +1,7 @@ +fixes: + - | + Fixes a race condition in the function `test_cancel_running_job()` in `test_job()`. + Modified the `cancel()` method in `runtime_job.py` to handle status 204 as case + where job cancellation cannot be performed due to specific conditions. + Refer to #1019 _ for + more details. From aef7e0582ec3c4b4470d78cbabfe0c5b825b6dc1 Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sat, 19 Aug 2023 21:31:54 +0100 Subject: [PATCH 4/8] substitute line 248 with `ex.status_code in (409, 204)` in `runtime_job.py` for semplicity --- qiskit_ibm_runtime/runtime_job.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_ibm_runtime/runtime_job.py b/qiskit_ibm_runtime/runtime_job.py index 26dac1211..58d8f5a0b 100644 --- a/qiskit_ibm_runtime/runtime_job.py +++ b/qiskit_ibm_runtime/runtime_job.py @@ -245,7 +245,7 @@ def cancel(self) -> None: try: self._api_client.job_cancel(self.job_id()) except RequestsApiError as ex: - if ex.status_code == 409 or ex.status_code == 204: + if ex.status_code in (409, 204): raise RuntimeInvalidStateError(f"Job cannot be cancelled: {ex}") from None raise IBMRuntimeError(f"Failed to cancel job: {ex}") from None self.cancel_result_streaming() From 28a852ff4950172672b1a75a9602d5a7cdc44b3f Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sun, 20 Aug 2023 09:00:10 +0100 Subject: [PATCH 5/8] revert to `ex.status == 409` `in cancel()` function in `runtime_job.py` --- qiskit_ibm_runtime/runtime_job.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_ibm_runtime/runtime_job.py b/qiskit_ibm_runtime/runtime_job.py index 58d8f5a0b..43a5d0f62 100644 --- a/qiskit_ibm_runtime/runtime_job.py +++ b/qiskit_ibm_runtime/runtime_job.py @@ -245,7 +245,7 @@ def cancel(self) -> None: try: self._api_client.job_cancel(self.job_id()) except RequestsApiError as ex: - if ex.status_code in (409, 204): + if ex.status_code == 409: raise RuntimeInvalidStateError(f"Job cannot be cancelled: {ex}") from None raise IBMRuntimeError(f"Failed to cancel job: {ex}") from None self.cancel_result_streaming() From 43b4f417f58eb4d28960c0b1269bd7ded793f952 Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sun, 20 Aug 2023 09:08:40 +0100 Subject: [PATCH 6/8] update release notes for `fix_test_cancel_job_running-771511870d82d4e4.yaml` --- .../notes/fix_test_cancel_job_running-771511870d82d4e4.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml b/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml index 86dc2c973..d75da5533 100644 --- a/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml +++ b/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml @@ -1,7 +1,6 @@ fixes: - | - Fixes a race condition in the function `test_cancel_running_job()` in `test_job()`. - Modified the `cancel()` method in `runtime_job.py` to handle status 204 as case - where job cancellation cannot be performed due to specific conditions. + Fixes a race condition in the function `test_cancel_running_job()` in `test_job()` + where job cancellation cannot be performed. Refer to #1019 _ for more details. From 2fbf7423c622131f0f1f02adf4737d872099e136 Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sun, 20 Aug 2023 15:58:50 +0100 Subject: [PATCH 7/8] fix `test_cancel_job_running.py` in `test_job.py` --- test/integration/test_job.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/integration/test_job.py b/test/integration/test_job.py index 70848348c..f1e56e78a 100644 --- a/test/integration/test_job.py +++ b/test/integration/test_job.py @@ -161,11 +161,10 @@ def test_cancel_job_running(self, service): service, circuits=[ReferenceCircuits.bell()] * 10, ) - wait_for_status(job, JobStatus.CANCELLED) - if not cancel_job_safe(job, self.log): - return - time.sleep(10) # Wait a bit for DB to update. rjob = service.job(job.job_id()) + if not cancel_job_safe(rjob, self.log): + return + time.sleep(10) self.assertEqual(rjob.status(), JobStatus.CANCELLED) @run_integration_test From 1d2801a887ebb214e13da524c4703929dc83366b Mon Sep 17 00:00:00 2001 From: robotAstray Date: Sun, 20 Aug 2023 16:03:34 +0100 Subject: [PATCH 8/8] release notes: `fix_test_cancel_job_running-771511870d82d4e4.yaml` --- .../notes/fix_test_cancel_job_running-771511870d82d4e4.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml b/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml index d75da5533..016667bff 100644 --- a/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml +++ b/releasenotes/notes/fix_test_cancel_job_running-771511870d82d4e4.yaml @@ -1,6 +1,6 @@ fixes: - | - Fixes a race condition in the function `test_cancel_running_job()` in `test_job()` - where job cancellation cannot be performed. + Fixes a race condition in the function `test_cancel_running_job()` in `test_job.py` + where job cancellation could not be performed. Refer to #1019 _ for more details.