Title: Emscripten: static (non-MAIN_MODULE) builds fail at startup on dynamic-linking-only JS helpers · Issue #156780 · python/cpython · GitHub
Open Graph Title: Emscripten: static (non-MAIN_MODULE) builds fail at startup on dynamic-linking-only JS helpers · Issue #156780 · python/cpython
X Title: Emscripten: static (non-MAIN_MODULE) builds fail at startup on dynamic-linking-only JS helpers · Issue #156780 · python/cpython
Description: Bug description: CPython's Emscripten platform code is written for the -sMAIN_MODULE dynamic-linking configuration it is built and tested in. Several EM_JS bodies reference JS helpers that only exist in that configuration. Because EM_JS ...
Open Graph Description: Bug description: CPython's Emscripten platform code is written for the -sMAIN_MODULE dynamic-linking configuration it is built and tested in. Several EM_JS bodies reference JS helpers that only exi...
X Description: Bug description: CPython's Emscripten platform code is written for the -sMAIN_MODULE dynamic-linking configuration it is built and tested in. Several EM_JS bodies reference JS helpers that only...
Opengraph URL: https://github.com/python/cpython/issues/156780
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Emscripten: static (non-MAIN_MODULE) builds fail at startup on dynamic-linking-only JS helpers","articleBody":"### Bug description:\n\nCPython's Emscripten platform code is written for the `-sMAIN_MODULE` dynamic-linking configuration it is built and tested in. Several `EM_JS` bodies reference JS helpers that only exist in that configuration. Because `EM_JS` bodies are emitted verbatim into the output, Emscripten's dependency tracker cannot see those references, so a static link silently emits JS calling helpers it never included and fails at runtime instead of at link time.\n\nFour such references, each confirmed against Emscripten 6.0.9:\n\n1. `Python/emscripten_syscalls.c:110` hooks `resolveGlobalSymbol()` to wrap `main()` in `WebAssembly.promising()`. That helper is defined in `src/lib/libdylink.js`, which `src/modules.mjs:99` links in only `if (MAIN_MODULE)`; the file itself `#error`s otherwise, and the symbol is marked `__internal: true`. The read sits below the `if (!WebAssembly.promising) return;` guard on line 106, so this only fires once a runtime exposes JSPI.\n2. `Python/emscripten_trampoline.c:54` and `:82` use `wasmTable` and `addFunction()`. Neither is included by default; `MAIN_MODULE` happens to pull both in through libdylink's own `__deps`.\n3. The promising-main wrapper calls `_emscripten_exit_with_live_runtime()` (`emscripten_syscalls.c:128`), which is likewise dropped from a static link.\n4. `Python/emscripten_trampoline.c:93` reads `__PyRuntime`, which is bound into the JS glue only because `configure` lists it in `-sEXPORTED_FUNCTIONS` for CPython's own `python.mjs` link. An embedder linking `libpython` into their own application gets no such flag. Unlike the others, this one fails on every runtime, JSPI or not.\n\n### Minimal reproducer\n\nThis is the shape of (1) with no CPython involved:\n\n```c\n#include \u003cemscripten.h>\nEM_JS(void, probe, (void), {\n if (!WebAssembly.promising) return;\n const orig = resolveGlobalSymbol;\n})\n__attribute__((constructor)) void c(void) { probe(); }\nint main(void) { return 0; }\n```\n\n```console\n$ emcc t.c -o t.js # links clean, no diagnostic at all\n$ node t.js # fine, the guard returns early\n$ node --experimental-wasm-jspi t.js\nReferenceError: resolveGlobalSymbol is not defined\n at probe (t.js:1513:136)\n at initRuntime (t.js:540:35)\n```\n\nEmscripten does warn for library symbols it knows about — referencing `addFunction` the same way produces \"`addFunction` is a library symbol and not included by default\". `resolveGlobalSymbol` gets nothing, because without `MAIN_MODULE` libdylink.js is never loaded and the name is not in the symbol set to warn about.\n\n### Full reproduction\n\nI built `libpython3.14.a` for wasm32-emscripten with `--disable-wasm-dynamic-linking MODULE_BUILDTYPE=static CFLAGS=-DPY_CALL_TRAMPOLINE` and linked it into a small C application that runs an interactive Python console in the browser. It aborts during startup on `__PyRuntime`; with that symbol exported by hand it then aborts on `resolveGlobalSymbol` as soon as JSPI is enabled, and works with JSPI off.\n\n### Note on the fix\n\n`EM_JS_DEPS` (`emscripten/em_macros.h`) exists precisely to declare these, and `emscripten_syscalls.c:43` already uses it for `$SYSCALLS`. Declaring `$resolveGlobalSymbol` converts the silent breakage into a link-time `error: undefined symbol: $resolveGlobalSymbol`, which is the behaviour you would want in a configuration that genuinely cannot support it.\n\n### Why this is not caught today\n\nEvery wasm test builds through `Platforms/emscripten/__main__.py`, which hard-codes `--enable-wasm-dynamic-linking` at line 510. `web_example`, `web_example_pyrepl_jspi` and `browser_test` therefore all exercise `MAIN_MODULE` only; the static configuration has no coverage.\n\nContext for the report: this blocks embedding CPython in Kodi's WebAssembly port, which links statically.\n\n### CPython versions tested on:\n\n3.14, CPython main branch\n\n### Operating systems tested on:\n\nOther\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156781\n* gh-156798\n* gh-157238\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/clementperon","@type":"Person","name":"clementperon"},"datePublished":"2026-09-01T14:51:00Z","url":"https://github.com/python/cpython/issues/156780"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e1bb591f-82d4-39bb-fd48-45afcf897dc9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DED0:4BB79:35E9325:44E6547:6B01F253 |
| html-safe-nonce | f9ca7a399e1dff9245614d8953266c07592e12db26010875d4a206fc9357f515 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERUQwOjRCQjc5OjM1RTkzMjU6NDRFNjU0Nzo2QjAxRjI1MyIsInZpc2l0b3JfaWQiOiIzMjYwMDMxNzAxNjEwNTI1MjY3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | cc7f43209143c3a7d20fae9d51de9343cd29d9102d485e52c213590c73741a66 |
| hovercard-subject-tag | issue:5313313509 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/156780/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0bb2c45e9874a708e6edf8858c05118262253b63da7f7bb02f7fa03b19c57366/python/cpython/issues/156780 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0bb2c45e9874a708e6edf8858c05118262253b63da7f7bb02f7fa03b19c57366/python/cpython/issues/156780 |
| og:image:alt | Bug description: CPython's Emscripten platform code is written for the -sMAIN_MODULE dynamic-linking configuration it is built and tested in. Several EM_JS bodies reference JS helpers that only exi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | clementperon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width