Skip to content

Make host callback result cap configurable#6

Merged
simonw merged 3 commits into
mainfrom
codex/configurable-host-result-cap
Jun 2, 2026
Merged

Make host callback result cap configurable#6
simonw merged 3 commits into
mainfrom
codex/configurable-host-result-cap

Conversation

@simonw

@simonw simonw commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Refs:

Raise the default host callback result size from 64 KiB to 256 KiB and expose a host_result_bytes option on run(), run_micropython_wasi(), MicroPythonSession, and MicroPythonReplaySession.

Wire the configured limit through a new micropython_wasm.host_result_cap import so the guest C host module can allocate according to the runtime setting instead of relying on a compile-time constant.

Reuse the guest-side result buffer across host calls and register it as a MicroPython VM root, avoiding repeated large heap allocations while keeping the buffer visible to the GC.

Increase the default fuel budget from 5M to 20M to match the 4x larger default host result cap, update the README examples and argument docs, and add tests for the new default, lower configured limits, and host_result_bytes validation.

Rebuild the packaged micropython-wasi.wasm artifact so it includes the new host_result_cap import and reusable buffer behavior.

simonw added 3 commits June 2, 2026 10:55
Raise the default host callback result size from 64 KiB to 256 KiB and expose a host_result_bytes option on run(), run_micropython_wasi(), MicroPythonSession, and MicroPythonReplaySession.

Wire the configured limit through a new micropython_wasm.host_result_cap import so the guest C host module can allocate according to the runtime setting instead of relying on a compile-time constant.

Reuse the guest-side result buffer across host calls and register it as a MicroPython VM root, avoiding repeated large heap allocations while keeping the buffer visible to the GC.

Increase the default fuel budget from 5M to 20M to match the 4x larger default host result cap, update the README examples and argument docs, and add tests for the new default, lower configured limits, and host_result_bytes validation.

Rebuild the packaged micropython-wasi.wasm artifact so it includes the new host_result_cap import and reusable buffer behavior.
…on Python 3.11)

Persistent sessions run Wasmtime in a background Python thread. When a wall-clock timeout interrupts guest code, Wasmtime raises a trap inside that thread and _thread_main stores the exception so the foreground run() call can report it.

On Linux Python 3.11 with wasmtime 45.0.0, keeping that exception's traceback alive also keeps the _run_bootstrap() frame alive. That frame contains Wasmtime objects such as the engine, store, linker, module, and instance. The Python session thread is joined, but those Rust-backed objects can survive until interpreter shutdown through the traceback reference chain.

CI then prints pytest's success summary and aborts during process teardown with a Rust panic from tokio-rt-worker: panic in a function that cannot unwind. Commit 06b58d1 fixed the normal close path by joining timer/session threads and clearing store references, but the trapped timeout path still retained teardown-sensitive Wasmtime state through the saved exception traceback.

Detach __traceback__, __cause__, and __context__ before storing the thread error. The public error message is preserved because run() only formats the exception value, while the traceback frame references are released promptly enough for Wasmtime resources to be destroyed before Python shutdown.

Add a regression assertion to the timeout test to ensure stored thread errors remain detached from traceback/cause/context chains. Reproduced the abort in Docker with python:3.11-slim and verified the full suite now exits cleanly there as well as locally via uv run pytest.

Docker test commands:

First reproduced the CI-shaped failure in a one-shot Linux Python 3.11 container:

    docker run --rm -v "$PWD":/work -w /work -e RUST_BACKTRACE=full python:3.11-slim sh -lc 'python -V && python -m pip install -U pip && pip --version && pip install . --group dev && python - <<"PY"
    import importlib.metadata
    print("wasmtime", importlib.metadata.version("wasmtime"))
    print("pytest", importlib.metadata.version("pytest"))
    PY
    python -m pytest'

Then kept a container around for focused iteration:

    docker rm -f mpw311 >/dev/null 2>&1 || true
    docker run -d --name mpw311 -v "$PWD":/work -w /work -e RUST_BACKTRACE=full python:3.11-slim sleep infinity
    docker exec mpw311 sh -lc 'python -m pip install -U pip >/dev/null && pip install . --group dev >/dev/null && python - <<"PY"
    import importlib.metadata
    print("wasmtime", importlib.metadata.version("wasmtime"))
    print("pytest", importlib.metadata.version("pytest"))
    PY'

Used the persistent container to isolate and verify the failing test file:

    docker exec mpw311 sh -lc 'python -m pytest tests/test_persistent_session.py -q; printf "exit=%s\n" "$?"'

Finally verified the complete suite exited cleanly after the fix:

    docker exec mpw311 sh -lc 'python -m pytest -q; printf "exit=%s\n" "$?"'
    docker rm -f mpw311
@simonw

simonw commented Jun 2, 2026

Copy link
Copy Markdown
Owner Author

That CI failure was gnarly, had Codex figure it out using Docker to replicate CI on Python 3.11 and write a very detailed commit message: ac5b88b

@simonw simonw merged commit 7afe409 into main Jun 2, 2026
10 checks passed
@simonw simonw deleted the codex/configurable-host-result-cap branch June 2, 2026 19:15
simonw added a commit that referenced this pull request Jun 2, 2026
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