Skip to content

fix: stop a failed request from hanging Python in Safari - #502

Merged
neuromechanist merged 8 commits into
developfrom
fix/safari-python-hang
Sep 25, 2026
Merged

neuromechanist merged 8 commits into
developfrom
fix/safari-python-hang

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Refs #496. This does not close it: the issue closes after a check in Safari itself.

What was wrong

When a request fails, Safari's fetch rejects with a TypeError whose only own property is message. It has no stack.
Pyodide 0.29.5 treats a JavaScript value as an error only if it has a name, a message and a stack, or is a DOMException (JsProxy_compute_typeflags).
Any other rejection reaches _pyodide._future_helper.set_exception as a plain JsProxy. asyncio's Future.set_exception refuses it, and the coroutine awaiting the request never resumes.

In Safari, then, any request that fails hangs whatever awaits it:

  • Notebook: the cell stays [*] and the kernel stays busy.
  • Chat: the run waits for its 120-second deadline and loses the runtime's variables.

Chrome and Firefox are not affected, because their fetch errors carry a stack.

The change

  • buildRejectionGuardSource() in frontend/osa-egress.js wraps set_exception. Anything asyncio would refuse becomes a JsException with the JavaScript name and message, and a JavaScript undefined or null reads "a promise was rejected with no reason". Everything else passes through untouched, including a Python exception, which keeps its type.
  • The chat runtime runs the guard first in its data client, before the seal. The runtime bundle and the widget's integrity line are rebuilt.
  • notebook/osa-bridge.js sends the same Python, silently, to the kernel as soon as the notebook's panel exists, and again after a restart.
    • The bridge does not wait for the guard's reply. JupyterLite 0.8.4 runs each kernel's requests in the order they arrive, through one first-in, first-out lock, so being sent first is enough. Waiting could also hang forever: when a request fails, JupyterLite drops the requests queued behind it without replying.
    • One window remains, documented at the call site: a cell the reader runs while the kernel restarts goes to the kernel before the guard.

This fixes only the hang. The notebook's own reads in Safari and Firefox still fail, now at once and with the URL, until the eegprep-lean change below is re-vendored.

Tests

  • frontend/test-worker-core.js fails without the guard:
    • A refused connection raises an OSError.
    • The same session can still read afterwards.
    • A Python exception keeps its type, and a guard that wrapped every rejection fails this check.
    • A stackless AbortError raises as one.
    • null and undefined read as "no reason".
    • A canary fails if Bun's refused-connection rejection ever gains a stack, since the section would then stop testing the guard.
  • notebook/test-bridge.js checks that the bridge's copy equals the runtime's, and that both stackless shapes hang without the guard (controls). It covers the same cases under the guard.
  • notebook/e2e-check.js checks the guard in Chrome after opening and after a restart. With the restart path's guard removed, it fails.
  • Mutation checks: making set_exception always wrap fails the type check in both suites, and dropping the jsnull case fails the null check in both.
  • The Bun suites, the bundle --check and chrome.js pass.

Measured in WebKit 26.6 (Playwright 1.63, headless; not Safari)

before after
Notebook nm000132, second cell [*] forever, kernel busy TransportError ... (index.json) in 0.5 s, kernel idle, next execution runs
Chat runtime, one request failed at the network layer timeout after 120,001 ms, variables lost TransportError in 5 ms, variables kept

Not fixed here

Safari's fetch rejects with a TypeError that has no stack, which
Pyodide 0.29.5 does not take for an error, so an await on a failed
request never returned and the run hit its 120 s deadline. The data
client now installs a guard on _pyodide._future_helper.set_exception
before the seal. test-worker-core.js uses Bun's real refused-connection
rejection (same shape as Safari's) in place of the JS-made stand-in
that hid the hang; it fails without the guard. Refs #496
osa-bridge.js sends the runtime's guard, silently, to each new kernel
before the setup cells, so a failed request in Safari raises in its
cell instead of leaving the kernel busy. test-bridge.js checks the copy
equals buildRejectionGuardSource() and that a refused pyfetch hangs
without it and raises with it; e2e-check.js checks the guard reached
the kernel (fails in Chrome with the bridge change reverted). Refs #496
Post the notebook's 'none' setup status only after the guard is sent,
as the path with setup cells already does. A rejection with no reason
now says so rather than 'None'; test-bridge.js checks it (fails without
the change). Runtime bundle rebuilt. Refs #496
@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://fix-safari-python-hang-demo.osc.earth
Branch fix/safari-python-hang
Commit d560ce7

This preview will be updated automatically when you push new commits.

JavaScript null reaches the guard as Pyodide's jsnull and was reported
as "jsnull"; it now reads "a promise was rejected with no reason", as
undefined already did. Both copies of the guard change together, and
the runtime bundle and the widget's integrity line are rebuilt.

Tested: bun notebook/test-bridge.js and frontend/test-worker-core.js
(null cases in the next commit fail without this change), runtime
bundle --check, test-runtime-lifecycle.js, chrome.js. Refs #496
Both suites now check that a Python exception rejected through
JavaScript keeps its type (a guard that wrapped everything fails
here), that a stackless AbortError-shaped rejection raises as
JsException: AbortError, and that null and undefined read as no
reason. test-worker-core.js gains a canary that Bun's refused-connection
rejection still has no stack, and reads through osa.fetch_bytes after
the failure in the same session. test-bridge.js adds an unguarded
control for the stackless shape.

Tested: test-bridge.js 14/14, test-worker-core.js 197/197; mutating
the guard to always wrap fails the type check in both, and dropping
the jsnull case fails the null check in both. Refs #496
The bridge sends the guard as soon as the notebook's panel exists,
before the document and theme waits, and no longer awaits its reply:
JupyterLite 0.8.4 runs a kernel's requests in arrival order through
one FIFO mutex, so being sent first is enough, and a reply can be lost
when JupyterLite cancels requests queued behind a failed one. A probe
sent at the first moment the kernel's connection existed ran after the
guard in Chrome 153 and WebKit 26.6. The remaining window, a cell run
while the kernel restarts, is documented at the call site.
e2e-check.js now also checks the guard after a restart.

Tested: notebook/e2e-check.js all checks pass in Chrome; with the
restart path's guard removed, the new restart check fails (got null);
test-bridge.js 14/14; WebKit nm000132 second cell raises in 0.5 s and
the kernel keeps answering. Refs #496
@neuromechanist
neuromechanist merged commit d0ae091 into develop Sep 25, 2026
10 checks passed
@neuromechanist
neuromechanist deleted the fix/safari-python-hang branch September 25, 2026 01:09
neuromechanist added a commit that referenced this pull request Sep 25, 2026
Brings in #502, #503 and #508. The generated runtime bundle and its integrity
line are rebuilt from the merged sources; CHANGELOG keeps both Added entries.

Tested: bun scripts/build-runtime-bundle.js --check; test-worker-core (220),
test-data-lane (106), test-runtime-bundle (25), test-widget-tools (397),
test-runtime-lifecycle and test-egress all passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant