Hi all, I am trying to compile cpython for wasip2 and am getting the following issue when I try and run it with wasmtime:
Command:
wasmtime run --wasm max-wasm-stack=8388608 --wasi preview2 --dir {HOST_DIR}::{GUEST_DIR}
Output
Error: failed to run main module `/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/cross-build/wasm32-wasip2/python.wasm`
Caused by:
0: failed to instantiate "/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/cross-build/wasm32-wasip2/python.wasm"
1: unknown import: `wasi_snapshot_preview1::adapter_open_badfd` has not been defined
Traceback (most recent call last):
File "/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/./Tools/wasm/wasi.py", line 365, in <module>
main()
~~~~^^
File "/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/./Tools/wasm/wasi.py", line 361, in main
dispatch[context.subcommand](context)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
File "/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/./Tools/wasm/wasi.py", line 85, in wrapper
return func(context, working_dir)
File "/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/./Tools/wasm/wasi.py", line 272, in make_wasi_python
subprocess.check_call([exec_script, "--version"])
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/python@3.13/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/subprocess.py", line 419, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '[PosixPath('/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/cross-build/wasm32-wasip2/python.sh'), '--version']' returned non-zero exit status 1.
The weird thing is it seems like it should be available based on this: https://github.com/bytecodealliance/wasmtime/blob/cd67a6090d515189a9c286bec2d993a591da3739/crates/wasi-preview1-component-adapter/src/lib.rs#L171
I built with wasi-sdk v25 (which might be the issue) and using wasmtime v28 and v29 caused the issue.
If anyone has any ideas, I'd appreciate it, otherwise I'll keep trying different combinations of wasmtime and wasi-sdk. Thanks!
Is this issue possibly linked/relevant?
Hmm maybe... I will take a look. Thanks
Yeah it seems unlikely since the tooling probably isn't doing anything funny with the adapter name/does this properly... but I wonder if this is something that tripped people up somewhat recently, and specifying an adapter name (via --adapt name=<path>
might be necessary now where it wasn't before...
it is also possible that while I am telling cpython to compile with the target wasip2 that it is still doing it for p1 and that is also causing an issue...
Is the python.wasm
file a module or a component (use e.g. wasm-tools print
and look at the first line of the output)? If it's a module, then presumably wasm-component-ld
is not being run and the adapter isn't being used. Keep in mind that wasm32-wasip2
is not yet a supported target for CPython, so hacks may be required to get it working.
ah it is just a module... Ok this likely explains the issue thanks. I was just trying to check out Brett's flag to try a different target , which doesn't work yet. All good. thanks everyone!
Ben Brandt has marked this topic as resolved.
Ben Brandt has marked this topic as unresolved.
OK nevermind, even if I build for wasip1 I still have the same issue:
Checked 112 modules (69 built-in, 0 shared, 26 n/a on wasi-wasm32, 9 disabled, 8 missing, 0 failed on import)
Error: failed to run main module `/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/cross-build/wasm32-wasip1/python.wasm`
Caused by:
0: failed to instantiate "/Users/benjamin.brandt/github/wasi-wheels/cpython-3.13.1/cross-build/wasm32-wasip1/python.wasm"
1: unknown import: `wasi_snapshot_preview1::adapter_open_badfd` has not been defined
It also doesn't work with wasi-sdk 24... it is possible there's something wrong with my wasmtime or I messed something up in the build... I'll keep digging
Hmm, that definitely shouldn't happen. The only part of wasi-libc that references adapter_open_badfd
is descriptor_table.c
, which is explicitly excluded from the WASIp1 build. I wonder if files got accidentally cached across the p2 and p1 builds? Maybe try doing a clean build if you haven't already.
I'm running into the same issue just running the basic command listed here: https://devguide.python.org/getting-started/setup-building/index.html#wasi
I can ask @Brett Cannon again... for some reason it works for him, but I am having issues which leads me to believe I have something weird in my setup. But I guess this is more of a Cpython issue than a wasmtime issue...
I've also cleaned the entire cpython dir and replaced it multiple times but nothing seems to help...
everything works for Brett
Yeah maybe it is because I am on 3.13.1 and there are changes on main I
need to backport... I'll take a look
@Joel Dice I actually have the same problem with the build from the script in componentize-py... so maybe it has more to do with how we are invoking wasmtime (or my wasmtime is wrong)
componentize-py
only cares about libpython312.so
and simply ignores python.wasm
, so the latter could be broken in various ways without me noticing. What's your goal here? Are you planning to use python.wasm
for some specific purpose?
It was for building the wheels. And Brett's script does this validation.
But maybe I also only need the .so file and can just ignore it as well.
Last updated: Jan 24 2025 at 00:11 UTC