You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's a feature that allows running Python code asynchronously along side JavaScript using the Emterpreter's Asyncify. It basically performs a emscripten_sleep(1) in the interpreter loop of CPython every 100 Python bytecode instructions. By sleeping for 1ms, it yields back to the browser to handle any queued up events, and then returns back to the Python interpreter running in the Emterpreter. This all worked fine with Emterpreter's Asyncify.
I saw the new Asyncify option released recently and ported the project to use that instead (since it is much faster, great feature!) but have been running into issues getting it to work. It in general works sometimes, but produces a random RuntimeError: memory access out of bounds error for some, but not all, Python code (using no Python C imports other than builtin modules). The exact same code works fine on the Emterpreter Async. Here's what a stack trace looks like:
It looks like it resumes and restores the stack after the _emscripten_sleep, but then crashes a little bit later on in a seemingly random place due to corrupted memory?
Here are some notes on my configuration:
Running in Chrome (but crash also happens in Firefox)
I am using the upstream LLVM backend, but have this flag -s WASM_OBJECT_FILES=0 set to turn off WASM object files. Don't think this should affect anything?
I'm calling the function using ccall. I know Asyncify doesn't support ccall Promises yet (ccall + ASYNCIFY promise support #9029), so I call it with the 'async' flag turned off (to prevent the assertion error).
Here's what I've learned from debugging attempts so far:
Build with just -s ASYNCIFY=1 and let it do static analysis to figure out which functions to instrument. (Didn't work).
Build with -s ASYNCIFY_WHITELIST=@async_funcs.tmp with all functions in the wasm binary using a dump of all function names from llvm-nm in case the static analyzer is missing some functions for some reason. (Doesn't work)
I'm fairly confident this isn't some issue with CPython trying to use corrupted memory. The exact same Python code works if you remove the call to emscripten_sleep(1). It seems to explicitly have to do with unwinding and rewinding the stack and something happening in that process.
Any advice for further debugging would be greatly appreciated.
The text was updated successfully, but these errors were encountered:
Solved. It was ccall. It is actually not safe to use it with Asyncify at all, even if you don't care about the return value not being returned as a Promise, because the stackRestore() call corrupts the memory. It seems like it makes sense then to go ahead and fix Issue #9029.
Hi,
I'm working on a full-featured port of CPython to JavaScript using Emscripten (still a WIP): https://github.com/plasticityai/coldbrew
There's a feature that allows running Python code asynchronously along side JavaScript using the Emterpreter's Asyncify. It basically performs a
emscripten_sleep(1)
in the interpreter loop of CPython every 100 Python bytecode instructions. By sleeping for 1ms, it yields back to the browser to handle any queued up events, and then returns back to the Python interpreter running in the Emterpreter. This all worked fine with Emterpreter's Asyncify.I saw the new Asyncify option released recently and ported the project to use that instead (since it is much faster, great feature!) but have been running into issues getting it to work. It in general works sometimes, but produces a random
RuntimeError: memory access out of bounds
error for some, but not all, Python code (using no Python C imports other than builtin modules). The exact same code works fine on the Emterpreter Async. Here's what a stack trace looks like:It looks like it resumes and restores the stack after the
_emscripten_sleep
, but then crashes a little bit later on in a seemingly random place due to corrupted memory?Here are some notes on my configuration:
-s WASM_OBJECT_FILES=0
set to turn off WASM object files. Don't think this should affect anything?ccall
. I know Asyncify doesn't supportccall
Promises yet (ccall + ASYNCIFY promise support #9029), so I call it with the 'async' flag turned off (to prevent the assertion error).Here's what I've learned from debugging attempts so far:
-s ASYNCIFY=1
and let it do static analysis to figure out which functions to instrument. (Didn't work).-s ASYNCIFY_WHITELIST=@async_funcs.tmp
with all functions in the wasm binary using a dump of all function names fromllvm-nm
in case the static analyzer is missing some functions for some reason. (Doesn't work)emscripten_sleep(1)
. It seems to explicitly have to do with unwinding and rewinding the stack and something happening in that process.Any advice for further debugging would be greatly appreciated.
The text was updated successfully, but these errors were encountered: