-
Notifications
You must be signed in to change notification settings - Fork 23
Description
The spec says the following:
The WebAssembly function returned by
WebAssembly.Function
is a function whose behavior is determined as follows:
...
3. Traps ifsuspender
's state is not Active[caller
] for somecaller
4. Letsframes
be the stack frames sincecaller
5. Traps if there are any frames of non-suspendable functions inframes
It's reasonably easy for us to manually track whether our suspender is in state Active[caller
]. On the other hand, it's much harder to know if there are non-suspendable functions on the stack, especially in more complex code. We can't ask forgiveness because there is no way to recover from a trap from within wasm. It would be helpful if there were a way to ask for permission so that we could avoid the trap ahead of time.
My specific use case is as follows:
pyodide.runPythonSyncifying(`
from asyncio import sleep, ensure_future
from ctypes import CFUNCTYPE, c_int
def f():
print(1)
ensure_future(sleep(1)).syncify()
print(2)
# works perfectly:
f()
# Force a trampoline call through JavaScript. Fatally fails because
# 5. Traps if there are any frames of non-suspendable functions in frames
ctypes_f = CFUNCTYPE(None)(f)
ctypes_f()
`);
I would like to fix it to raise a Python exception instead. You can try it out in the debug console here in a browser with JSPI enabled:
https://pyodide.org/en/stable/console.html