Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/scripts/ft-wasm/build-wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# Emscripten 5.0.3. The flags below are the #175 investigation's proposal, not a decision.
set -euo pipefail
here="$(cd "$(dirname "$0")" && pwd)"
# _pm_upcall.c, smoke.py and embed-probe.mjs live beside build-cpython.sh, which builds the same
# runtime for the library (Decision 10 a1); this script is deleted once wasm.yml is green on it.
wasm_scripts="$(cd "$here/../../../python-multiplatform/scripts/wasm" && pwd)"

export FT_WORK="${FT_WORK:-$PWD/.tmp/ft-wasm}"
export FT_OUT="${FT_OUT:-$PWD/.tmp/ft-wasm-out}"
Expand Down Expand Up @@ -213,7 +216,7 @@ patch_makefile_exports
# Built in statically through the build tree's Modules/Setup.local (configure leaves an empty
# one there; makesetup reads it and finds the source under $(srcdir)/Modules). Its exported
# functions (EMSCRIPTEN_KEEPALIVE) are checked below; smoke.py tests the routing.
cp "$here/_pm_upcall.c" Modules/_pm_upcall.c
cp "$wasm_scripts/_pm_upcall.c" Modules/_pm_upcall.c
printf '*static*\n_pm_upcall _pm_upcall.c\n' >> "$hostdir/Modules/Setup.local"
log "Setup.local: $(tr '\n' ' ' < "$hostdir/Modules/Setup.local")"

Expand Down Expand Up @@ -276,7 +279,7 @@ run_smoke() {
local label="$1"; shift
local out="$FT_WORK/smoke-$label.log" rc
set +e
timeout 600 "$@" "$here/smoke.py" > "$out" 2>&1
timeout 600 "$@" "$wasm_scripts/smoke.py" > "$out" 2>&1
rc=$?
set -e
cat "$out"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-ft-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
[ -d "$root" ] || { echo "no unpacked archive under $FT_WORK/wasm-check"; exit 1; }
log="$FT_WORK/embed-probe.log"
set +e
timeout 600 node .github/scripts/ft-wasm/embed-probe.mjs "$root" > "$log" 2>&1
timeout 600 node python-multiplatform/scripts/wasm/embed-probe.mjs "$root" > "$log" 2>&1
rc=$?
set -e
cat "$log"
Expand Down
104 changes: 61 additions & 43 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ name: WasmJS CI
# python-multiplatform/build.gradle.kts.
#
# `compile` is what a runner can always do honestly, and it is named for exactly that.
# `test` runs the real suite, and runs only when there is a runtime to run it against.
# `test` builds the runtime itself and runs the real suite against it. Leader, Decision 11 (a),
# 2026-11-26 (team report pmp-binder.md): "wasm.yml's test job runs build-cpython.sh itself, output in
# actions/cache keyed on the script and _pm_upcall.c hashes. No release asset." Until then the job
# was gated on a `WASM_RUNTIME_URL` repository variable that was never set, so it never ran.

on:
# Runs only as part of ci.yml, which is started by hand (owner, 2026-11-14: "개발 테스트 ci는
Expand Down Expand Up @@ -51,33 +54,14 @@ jobs:
- name: Compile wasmJs main and test sources
run: ./gradlew :python-multiplatform:compileKotlinWasmJs :python-multiplatform:wasmJsTestClasses --console=plain

# Says out loud what the green tick above does and does not cover, so that a reader of this
# workflow's result does not have to infer it. Drops away the moment the runtime URL exists.
- name: Say what did not run
if: vars.WASM_RUNTIME_URL == ''
run: |
{
echo "### wasmJs tests did not run"
echo
echo "This job compiled \`wasmJsMain\` and \`wasmJsTest\`. **No wasm test was executed.**"
echo
echo "Running them needs a CPython Emscripten build, which this repository publishes as"
echo "\`org.thisisthepy:python-multiplatform-wasm-runtime\` (a 7 MB zip built by"
echo "\`:python-multiplatform:wasmBrowserRuntimeZip\`) but has never pushed to a remote"
echo "repository -- it exists only in the maintainer's \`mavenLocal\`."
echo
echo "Set the repository variable \`WASM_RUNTIME_URL\` to a URL that serves that zip"
echo "(a GitHub release asset is enough) and the \`test\` job below will run the real"
echo "suite: 344 Node tests and 10 browser tests."
} >> "$GITHUB_STEP_SUMMARY"
echo "::warning title=wasmJs tests did not run::No CPython Emscripten runtime is available to this runner; set the WASM_RUNTIME_URL repository variable. This job compiled only."

test:
name: WasmJS Node + browser tests
runs-on: ubuntu-latest
# The whole point of the guard: this job exists only when it can do its job. A job that does not
# run shows up grey in the checks list, which is a different claim from a green one.
if: vars.WASM_RUNTIME_URL != ''
name: WasmJS Node + browser tests (CPython 3.15t built here)
runs-on: ubuntu-24.04
# A cold cache is a full CPython build (a free-threaded build Python, then the wasm32 cross build).
timeout-minutes: 150
env:
# build-cpython.sh's default staging directory, and the one -PwasmPythonDir points at below.
WASM_RUNTIME_DIR: ${{ github.workspace }}/.caches/wasm-runtime
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -89,10 +73,12 @@ jobs:
distribution: 'zulu'
cache: 'gradle'

# CPython 3.15.0 pins node 24 (Platforms/emscripten/config.toml, node-version); the runtime is
# built with -fwasm-exceptions and pthreads, and the test tasks run it under this node.
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
node-version: '24'

- name: Cache Konan
uses: actions/cache@v4
Expand All @@ -102,40 +88,72 @@ jobs:
restore-keys: |
${{ runner.os }}-konan-

# The five files this unpacks are the whole interpreter as far as the test tasks are
# concerned -- python.wasm, python.mjs, the stdlib zip and the two glue modules. That is a
# property of `cpython.mjs`'s boot rather than an accident: the Node route used to reach the
# standard library through NODEFS against a CPython *source checkout*, which no artefact
# carries, and now installs the staged zip into MEMFS exactly as the browser route does.
- name: Fetch the CPython Emscripten runtime
# The staged runtime (python.wasm, python.mjs, the stdlib zip) is a pure function of the build
# script and the C module it compiles in, so those two hashes are the key (Decision 11 (a)).
# Restore and save are separate steps so that a red test run still keeps a runtime that built
# and passed its own checks; no restore-keys, a runtime from another script is never reused.
- name: Restore the CPython 3.15t runtime
id: runtime
uses: actions/cache/restore@v4
with:
path: .caches/wasm-runtime
key: wasm-cpython315t-${{ runner.os }}-${{ hashFiles('python-multiplatform/scripts/wasm/build-cpython.sh', 'python-multiplatform/scripts/wasm/_pm_upcall.c') }}

- name: Install build Python prerequisites
if: steps.runtime.outputs.cache-hit != 'true'
run: >
sudo apt-get update && sudo apt-get install -y
build-essential pkg-config libssl-dev zlib1g-dev libffi-dev
libbz2-dev liblzma-dev libsqlite3-dev libzstd-dev

# The script's default `all`: build, zip, verify (ABI tags, shared memory, Py_GIL_DISABLED,
# the _pm_upcall exports), smoke.py under python.sh, stage, and the embedding probe against the
# staged runtime. Any of them failing stops here, before a runtime is cached.
- name: Build CPython 3.15t for wasm32-emscripten
if: steps.runtime.outputs.cache-hit != 'true'
run: bash python-multiplatform/scripts/wasm/build-cpython.sh

- name: Save the CPython 3.15t runtime
if: steps.runtime.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: .caches/wasm-runtime
key: ${{ steps.runtime.outputs.cache-primary-key }}

- name: Check the runtime
run: |
set -euo pipefail
curl -fsSL "${{ vars.WASM_RUNTIME_URL }}" -o "$RUNNER_TEMP/wasm-runtime.zip"
mkdir -p "$RUNNER_TEMP/wasm-runtime"
unzip -q "$RUNNER_TEMP/wasm-runtime.zip" -d "$RUNNER_TEMP/wasm-runtime"
for f in python.wasm python.mjs; do
test -f "$RUNNER_TEMP/wasm-runtime/$f" \
|| { echo "::error::$f is not in the runtime zip served by WASM_RUNTIME_URL"; exit 1; }
for f in python.wasm python.mjs python3.15t.zip; do
test -f "$WASM_RUNTIME_DIR/$f" \
|| { echo "::error::$f is not in $WASM_RUNTIME_DIR"; exit 1; }
done
ls -l "$RUNNER_TEMP/wasm-runtime"
ls -l "$WASM_RUNTIME_DIR"

# `WasmCompiledWheelTest` imports a real compiled pyemscripten_2026_0 wheel. Under
# `-PrequireWasmRuntime` a missing wheel is a failure, not a skip, so fetch them first: by URL,
# each accepted only against the sha256 the script pins.
# each accepted only against the sha256 the script pins. The script pins none today: the
# wheel has to be a pypackpack-built cp315t one (leader, Decision 10 (c) c1, 2026-11-26), and
# until pypackpack #82 produces it this job fails on that one test, by name, by design.
- name: Fetch the pinned wasm wheels
run: bash python-multiplatform/scripts/wasm/build-cpython.sh wheels

# `-PrequireWasmRuntime` is the second half of the guard, and it is what makes this job unable
# to repeat the original defect: if either task would skip -- no interpreter, or no
# Chromium-family browser for karma -- the build fails naming what was missing, instead of
# reporting success having run nothing.
#
# `--continue`: one suite's failure must not hide the other's result. Without it a red
# wasmJsNodeTest (run 45829018834: only WasmCompiledWheelTest, Decision 10 (c) c1) stopped Gradle
# before wasmJsBrowserTest ran, and the browser suite reported nothing. The step still fails
# when either suite does.
- name: Run wasmJs tests
run: |
./gradlew \
:python-multiplatform:wasmJsNodeTest \
:python-multiplatform:wasmJsBrowserTest \
-PwasmPythonDir="$RUNNER_TEMP/wasm-runtime" \
-PwasmPythonDir="$WASM_RUNTIME_DIR" \
-PrequireWasmRuntime=true \
--continue \
--console=plain

# Belt to the guard's braces, and it guards a different failure: a task that *runs* but
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Honest summary, see the [roadmap](docs/roadmap/ROADMAP.md) for what is left to d
| Binding prebuilt jars, Compose from Python | ✅ desktop · ⏳ Android / iOS / wasm |
| Binding Kotlin/Native klibs | 🟡 scanner only |
| `.pyi` stub generation | 🟡 partial |
| Free-threaded CPython 3.15t (the only build on desktop since #158 and on Android / iOS since #328; wasm GIL until #175) | 🟡 desktop · 🟡 Android / iOS (compiled, not yet run on emulator or simulator) · ⏳ wasm |
| Free-threaded CPython 3.15t (the only build on desktop since #158, on Android / iOS since #328 and on wasm since #175) | 🟡 desktop · 🟡 Android / iOS (compiled, not yet run on emulator or simulator) · ⏳ wasm |

## 🧱 The ecosystem

Expand Down
16 changes: 10 additions & 6 deletions docs/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -1294,24 +1294,28 @@ All in `PM/commonTest`, so they run wherever the interpreter loads.
desktop JVM, `macosArm64` and `linuxX64` embed the free-threaded CPython 3.15t, with no GIL option
(`-PpythonFreeThreaded` is removed and the build fails if it is passed); Android (ART on arm64-v8a
and x86_64, and androidNative) and iOS embed the project's own free-threaded CPython 3.15t (T-2), with
no GIL option either; wasm embeds a GIL CPython until #175. The fact is per platform
(`embedsFreeThreadedPython`, an `expect`/`actual`, kept while wasm is the exception), and
no GIL option either; wasm (wasmJs) embeds the project's own free-threaded CPython 3.15t built with
pthreads by `python-multiplatform/scripts/wasm/build-cpython.sh` (#175; owner, 2026-10-04: "The wasm
interpreter is also 3.15t, built by us ... with pthreads"; leader, Decision 10 (a) a1, 2026-11-26), and
in a browser needs a cross-origin isolated page (T-8). The fact is per platform
(`embedsFreeThreadedPython`, an `expect`/`actual`), and
`Versions.abiFlags` (`t` or empty) and every name derived from it (`libpython3.15t`, `lib/python3.15t/`,
`libmultiplatform_python3.15t.so`) follow it. On the GIL platform Kotlin threads attach a thread state
and serialise on the GIL.
`libmultiplatform_python3.15t.so`) follow it.
`Status: implemented` on desktop, Android and iOS (CI run 44889681538, 2026-11-21: the Android x86_64
emulator, API 34, ran the instrumented suite on 3.15t, 539 tests, 0 failed, `AndroidFreeThreadedBuildTest`
among them; the iOS simulator ran the library suite on 3.15t, 513 tests, 0 failed, `IosFreeThreadedBuildTest`
among them, and the plugin-staged `ios-consumer` app printed `IOS_CONSUMER_PROBE stdlib=ok ext=ok payload=ok
ft=ok`; jobs `android-native.yml` `emulator` and `ios.yml` `ios-consumer`, owner-approved 2026-11-21, #328);
`partial` on androidNative (compiled against 3.15t, its `artTest` not run on a device or emulator) and
wasm (GIL build until #175),
wasm (3.15t `wasmJsNodeTest` run locally only; `wasm.yml`'s test job, which builds the runtime itself,
has not run in CI yet, ROADMAP 10.1),
`PM/desktopTest/.../DesktopFreeThreadedBuildTest.kt` (ABI flags, `Py_GIL_DISABLED` of the loaded
interpreter, checkpoint default), `PM/desktopNativeTest/.../DesktopNativeFreeThreadedBuildTest.kt`,
`PM/androidInstrumentedTest/.../AndroidFreeThreadedBuildTest.kt` (ABI flags, stdlib path, the AAR's
`python.properties`, `Py_GIL_DISABLED` of the loaded interpreter, checkpoint default),
`PM/artTest/.../AndroidNativeFreeThreadedBuildTest.kt`, `PM/iosSimulatorArm64Test/.../IosFreeThreadedBuildTest.kt`,
`PM/wasmJsTest/.../WasmGilBuildTest.kt`,
`PM/wasmJsTest/.../WasmFreeThreadedBuildTest.kt` (ABI flags, `Py_GIL_DISABLED` of the loaded
interpreter, `sys._is_gil_enabled()`),
`PM/commonTest/.../VersionsTest.kt`; the release after initialisation is tested by `GilReleaseTest` /
`GilReleaseNativeTest` (see C-4).
- **T-1a** On desktop, Python may be entered from a JVM virtual thread (#161 option B, user decision):
Expand Down
2 changes: 1 addition & 1 deletion docs/design/threading-and-abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ the build differs by platform (SPEC T-1):
|---|---|---|
| Desktop JVM, `macosArm64`, `linuxX64` | `python3.15t` (free-threaded), the only desktop build | Each Kotlin thread talks to its own free-running Python thread |
| Android (ART, androidNative; arm64-v8a and x86_64), iOS | `python3.15t` (free-threaded), the project's own build, the only mobile build since #328 | Each Kotlin thread talks to its own free-running Python thread (not yet run on an emulator or simulator) |
| wasmJs | GIL until #175 | single-threaded |
| wasmJs | `python3.15t` (free-threaded) with pthreads, the project's own build (`scripts/wasm/build-cpython.sh`, #175) | Kotlin runs on the main runtime thread only; Python threads are pthreads (Web Workers), and their upcalls and proxy slots are routed to the main thread by the `_pm_upcall` module. A browser page must be cross-origin isolated (SPEC T-8) |

The per-platform fact is `embedsFreeThreadedPython` (commonMain `EmbeddedPythonBuild.kt`, an
`expect`/`actual`); `-PpythonFreeThreaded` is removed. The interpreter version is pinned by
Expand Down
27 changes: 15 additions & 12 deletions docs/platforms/free-threaded-wasm.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Free-threaded CPython for wasm32-emscripten

Status: build recipe written from the CPython 3.15.0 source; **not yet run in CI.** Nothing
here has been compiled, linked or run. Tracking issue: #175 (this is its first step, the
build experiment). The owner approved the CI job on 2026-11-13 ("#175 may add the new wasm
3.15t CI build job").
Status: the experiment this file records is done. `.github/workflows/build-ft-wasm.yml` built the
3.15t pthreads interpreter in CI and passed its smoke test and embedding probe (run 45704188042 on
Emscripten 5.0.3), and its flags and patches moved into
`python-multiplatform/scripts/wasm/build-cpython.sh`, which now builds the runtime the wasmJs suite
uses (leader, Decision 10 (a) a1, 2026-11-26) and which `wasm.yml`'s test job runs itself (leader,
Decision 11 (a), 2026-11-26). The CI-only `.github/scripts/ft-wasm/` script and `build-ft-wasm.yml`
are deleted once `wasm.yml` is green on `build-cpython.sh`. Tracking issue: #175. The owner approved
the CI job on 2026-11-13 ("#175 may add the new wasm 3.15t CI build job").

python-multiplatform gets its parallelism from free-threaded CPython (decision 8 in
`AGENTS.md`). The wasmJs target today runs a GIL, no-threads CPython 3.14.2
`AGENTS.md`). The wasmJs target embedded a GIL, no-threads CPython 3.14.2 until #175
([`wasm-design.md`](wasm-design.md)). No prebuilt free-threaded CPython with pthreads exists
for Emscripten, so the workflow `.github/workflows/build-ft-wasm.yml` builds one with
CPython's own driver and checks that it runs threads without the GIL under node. It does not
change `wasm.yml`, `python-multiplatform/scripts/wasm/build-cpython.sh` or the runtime the
wasmJs suite uses.
for Emscripten, so this project builds one with CPython's own driver. The sections below describe
the experiment's archive layout and the questions it was run to answer; the "Open" list is as it
stood before the runs.

## What is built

Expand Down Expand Up @@ -143,7 +146,7 @@ and `wasmTable`, so a lost edit fails the build instead of the probe.

The workflow step `Embedding probe (no main)` runs after the archive upload, so a probe
failure or hang does not lose the archive:
`timeout 600 node .github/scripts/ft-wasm/embed-probe.mjs <unpacked archive>`. It prints one
`timeout 600 node python-multiplatform/scripts/wasm/embed-probe.mjs <unpacked archive>`. It prints one
`[x] PASS|FAIL` line per check and `FT-WASM-EMBED-PROBE-OK` only if all pass; on a timeout
(exit 124) the step reports the last check line printed.

Expand Down Expand Up @@ -179,7 +182,7 @@ Kotlin-side memory import).
- Whether `EMCC_CFLAGS=-pthread` is enough for libffi and mpdec, and whether the
`-sUSE_BZIP2` port links into a shared-memory build, is a prediction.
- Whether the JS trampoline fallback is enough, and what it costs per call compared with the
wasm-gc trampoline (`WasmCallTrampolineTest` covers only the current GIL build).
wasm-gc trampoline (`WasmCallTrampolineTest` was written against the GIL build).
- Only node is exercised; no browser (which also needs cross-origin isolation for
`SharedArrayBuffer`).
- The smoke test checks that four threads run and join with the GIL off. It is not a
Expand Down Expand Up @@ -246,7 +249,7 @@ an exported C function). No new dependency, workflow or kind of artefact.
- An exception raised in Kotlin reaches the Python thread.

**Implemented (2026-11-25, `feat/175-wasm-thread-upcalls`, not yet run).**
- `.github/scripts/ft-wasm/_pm_upcall.c`, built into the 3.15t runtime by `build-wasm.sh` through
- `python-multiplatform/scripts/wasm/_pm_upcall.c`, built into the 3.15t runtime by `build-wasm.sh` through
the build tree's `Modules/Setup.local`. It declares `Py_MOD_GIL_NOT_USED`, without which its
import would re-enable the GIL. Exports `pm_upcall_dispatch`, `pm_upcall_set_target`,
`pm_upcall_dispatch_index`; `build-wasm.sh` checks all three and that the module is built in.
Expand Down
2 changes: 1 addition & 1 deletion docs/platforms/python-version-acquisition.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ tests without throwing; the other flavour fails naming #158, as for `includeDir`
A consumer that builds against `python-multiplatform` must know which CPython it embeds (issue #61).
The answer is published three ways. The version is `pythonVersion` in `gradle.properties`; whether
the build is free-threaded differs by platform (desktop 3.15t since #158, Android, androidNative and
iOS 3.15t since #328, wasm GIL until #175), so each published artefact states its own.
iOS 3.15t since #328, wasm 3.15t since #175), so each published artefact states its own.

* **Extension** `pythonMultiplatform` (`python.multiplatform.gradle.EmbeddedPythonVersion`):
`pythonVersion` (`3.15.0`), `majorMinor` (`3.15`), `freeThreaded`. One value per project: the
Expand Down
Loading