Skip to content

Add way to query whether suspender will trap? #29

@hoodmane

Description

@hoodmane

The spec says the following:

The WebAssembly function returned by WebAssembly.Function is a function whose behavior is determined as follows:
...
3. Traps if suspender's state is not Active[caller] for some caller
4. Lets frames be the stack frames since caller
5. Traps if there are any frames of non-suspendable functions in frames

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions