Skip to content

WASM: raise OSError for network calls on Emscripten (#931) - #948

Open
kp992 wants to merge 1 commit into
QuantEcon:mainfrom
kp992:wasm-931-browser-errors
Open

kp992 wants to merge 1 commit into
QuantEcon:mainfrom
kp992:wasm-931-browser-errors

Conversation

@kp992

@kp992 kp992 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Closes #931.

  • fetch_nb_dependencies and GAMReader.from_url now raise OSError with a descriptive message on sys.platform == "emscripten" instead of failing with a cryptic socket error.
  • probvec docstring notes that parallel=True executes serially on Emscripten via Numba patch 0007.
  • Two smoke_test.py tests verify the new errors fire in the browser kernel (skipped on native).

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 90.753% (-0.03%) from 90.78% — kp992:wasm-931-browser-errors into QuantEcon:main

@mmcky

mmcky commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Thanks @kp992 — this PR implements exactly what #931 asked for but the issue was wrong. See comments from @oyamad. Better to flag it now than after you've built on it. When building these out, please take an adversarial view of the issue and validate it before proceeding on a PR. Let's design before we implement. Does that make sense?

The evidence. Daisuke tested both call paths in a live xeus-python deployment on 6 Sep (#931 comment): GAMReader.from_url read a .gam file from raw.githubusercontent.com and returned a full 3-player game, and requests.get(...) returned 200 with 4117 bytes. Both functions this PR guards currently work in the browser.

Why they work. urllib3 has supported Emscripten natively since 2.2.0, and per its documentation that support "is automatically enabled if sys.platform is emscripten" — it substitutes EmscriptenHTTPConnection and routes through XMLHttpRequest, or fetch on a worker thread for streaming requests. requests sits on urllib3, so fetch_nb_dependencies inherits it. emscripten-forge has already acted on this: requests-wasm-polyfill was dropped as redundant (recipes#782) and the companion request to patch urllib.urlopen was closed as "not needed anymore" (requests-wasm-polyfill#4).

The awkward part is that sys.platform == "emscripten" is the very condition that switches urllib3 onto its browser transport — so the guard fires precisely when the network path is being enabled.

Keep: the probvec docstring note. It is correct and independent of all the above — patch 0007 does fall back from target='parallel' to 'cpu', so parallel=True genuinely executes serially. That is item 2 of #931 and it should land.

Drop: both sys.platform == "emscripten" guards, in notebooks.py and game_converters.py, along with the two Emscripten-gated smoke tests asserting them.

If we still want a friendlier error, the shape to aim for is wrapping a failure that actually occurs rather than refusing before trying — let the call proceed, and if it raises, re-raise with guidance about pre-downloading data. That keeps the working case working and still improves the message where the runtime genuinely cannot do it. Better as a follow-up once #933's browser runner exists and we can see what a real failure looks like, rather than guessing now.

Housekeeping: the description says "Closes #931". With the guards removed this covers one of the issue's four items, so "Part of #931" is more accurate. Separately, we will correct item 1 of #931 itself — its claim that these calls "perform socket-level HTTP, which does not exist in WASM" is what sent you down this path, and leaving it would send the next person the same way.

One thing to wait for before reworking. There is an open question about deprecating quantecon/util/notebooks.py in 0.12.0 and dropping requests as a dependency. If that goes ahead, fetch_nb_dependencies is on its way out and any work on it now is wasted. Give us a few days to settle it, then the rework is just the probvec note plus whatever we decide about GAMReader.from_url.

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.

WASM: document browser limitations and raise helpful platform-specific errors

3 participants