Skip to content

fix: fix Jupyter event loop conflict - #1357

Merged
laurencap merged 3 commits into
amazon-braket:mainfrom
laurencap:fix/issue-857-event-loop-conflict
Sep 28, 2026
Merged

laurencap merged 3 commits into
amazon-braket:mainfrom
laurencap:fix/issue-857-event-loop-conflict

Conversation

@laurencap

Copy link
Copy Markdown
Contributor

fix: avoid nested event loop in AwsQuantumTask.result() in Jupyter

Issue #, if available:

Fixes amazon-braket/amazon-braket-examples#857

Written with Kiro Opus 5.5.

Description of changes:

Bug: Calling AwsQuantumTask.result() from a thread with a running event loop (e.g. a Jupyter cell) nested run_until_complete() on that loop via nest_asyncio. On Python 3.12 this logs "RuntimeError: cannot enter context: ... is already entered" from Task.__step, even though the result is returned.

New: When a loop is already running, result() now polls on a worker thread with its own event loop via asyncio.run() instead of nesting. Interrupting the kernel (e.g. from jupyter) will cancel the polling loop.
_get_future() creates the polling task with loop.create_task(), which removes the need for _create_future() and fixes the issue in the case where the customer calls await task.async_result().

Follow up work: we no longer need nest_asyncio.

Testing done:

I was able to reliably reproduce the error by running this example notebook: https://github.com/amazon-braket/amazon-braket-examples/blob/main/examples/braket_features/Allocating_Qubits_on_QPU_Devices.ipynb

With these changes, the issue no longer appears.

Added new unit tests.

Merge Checklist

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@laurencap
laurencap requested a review from a team as a code owner September 25, 2026 19:53
@github-actions github-actions Bot added the aws label Sep 25, 2026
@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (428fefa) to head (db447d9).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1357   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          171       171           
  Lines        11648     11667   +19     
  Branches      1538      1539    +1     
=========================================
+ Hits         11648     11667   +19     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@laurencap
laurencap force-pushed the fix/issue-857-event-loop-conflict branch from 4f73f73 to c5ba929 Compare September 25, 2026 20:12
@laurencap
laurencap merged commit 916bc12 into amazon-braket:main Sep 28, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Jupyter / Braket SDK event loop conflict

2 participants