[webassembly] 'runPythonAsync' api missing { async: true } option.
Port, board and/or hardware
webassembly standard build
MicroPython version
3.4.0; MicroPython v1.24.1 on 2025-03-09
Reproduction
const mp = await loadMicroPython();
await mp.runPythonAsync("import gc; gc.collect()");
Expected behaviour
In the web console, it should be no output, and the gc.collect() call succeed.
Observed behaviour
It raised an error in the web console
Aborted(Assertion failed: The call to mp_js_do_exec_async is running asynchronously. If this was intended, add the async option to the ccall/cwrap call.)
Additional Information
In the file ports/webassembly/api.js, int the runPythonAsync function, the Module.ccall missing the option { async: true }.
After adding that option to the ccall, it works as expected.
Code of Conduct
Yes, I agree
ports/webassembly: Make mp_js_do_str and mp_js_process_char asynchronous.
This fixes a bug tracked by #10692 where gc.collect() would crash due to emscripten_scan_stack being called synchronously within mp_js_do_str and mp_js_process_char.
The fix was to make mp_js_do_str and mp_js_process_char asynchronous by marking them as {async: true} in the Module.cwrap() call, and then to call the function asynchronously in wrapper.js.
GC test results before this patch:
$ (cd tests && MICROPY_MICROPYTHON=../ports/webassembly/node_run.sh ./run-tests.py -j1 stress/gc_trace.py)
FAIL stress/gc_trace.py
1 tests performed (2 individual testcases)
0 tests passed
1 tests failed: gc_trace
$ cat tests/results/stress_gc_trace.py.out
Aborted(Assertion failed: The call to mp_js_do_str is running asynchronously. If this was intended, add the async option to the ccall/cwrap call.)
/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:213
throw ex;
^
RuntimeError: Aborted(Assertion failed: The call to mp_js_do_str is running asynchronously. If this was intended, add the async option to the ccall/cwrap call.)
at abort (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:754:11)
at assert (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:455:5)
at ccall (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:4460:9)
at /Users/eli/projects/micropython/ports/webassembly/build/micropython.js:4479:16
at Object.mainProgram [as onRuntimeInitialized] (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:78:30)
at doRun (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:5238:71)
at run (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:5255:5)
at runCaller (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:5199:19)
at removeRunDependency (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:717:7)
at receiveInstance (/Users/eli/projects/micropython/ports/webassembly/build/micropython.js:935:5)
Node.js v18.12.1
CRASH%
GC test results after this patch:
$ (cd tests && MICROPY_MICROPYTHON=../ports/webassembly/node_run.sh ./run-tests.py -j1 stress/gc_trace.py)
pass stress/gc_trace.py
1 tests performed (2 individual testcases)
1 tests passed
I also updated the PR to indicate that mp_js_do_str and mp_js_process_char need to be called asynchronously.