diff --git a/src/braket/aws/aws_quantum_task.py b/src/braket/aws/aws_quantum_task.py index 3d28dfede..3e91b8fd9 100644 --- a/src/braket/aws/aws_quantum_task.py +++ b/src/braket/aws/aws_quantum_task.py @@ -316,10 +316,9 @@ def id(self) -> str: return self._arn def _cancel_future(self) -> None: - """Cancel the future if it exists. Else, create a cancelled future.""" - if not hasattr(self, "_future"): - self._future = asyncio.Future() - self._future.cancel() + """Cancel the future if it exists.""" + if hasattr(self, "_future"): + self._future.cancel() def cancel(self) -> None: """Cancel the quantum task. This cancels the future and the quantum task in Amazon diff --git a/test/unit_tests/braket/aws/test_aws_quantum_task.py b/test/unit_tests/braket/aws/test_aws_quantum_task.py index c00d78167..7671bf137 100644 --- a/test/unit_tests/braket/aws/test_aws_quantum_task.py +++ b/test/unit_tests/braket/aws/test_aws_quantum_task.py @@ -349,7 +349,23 @@ def test_cancel_without_fetching_result(quantum_task): quantum_task.cancel() assert quantum_task.result() is None - assert quantum_task._future.cancelled() + quantum_task._aws_session.cancel_quantum_task.assert_called_with(quantum_task.id) + + +def test_cancel_without_event_loop(quantum_task): + errors = [] + + def run(): + try: + quantum_task.cancel() + except BaseException as e: + errors.append(e) + + thread = threading.Thread(target=run) + thread.start() + thread.join() + + assert not errors quantum_task._aws_session.cancel_quantum_task.assert_called_with(quantum_task.id)