Hey <@U04EHEUCP9R>! Is there by any chance a way i...
# webassembly
a
Hey @Charlie Tapping! Is there by any chance a way in Chasm to suspend Wasm module execution and restore it later, potentially on a different host? For the context - consider a Wasm module that exports a function
wasm_main
, which in turn calls a host function
host_start_job
. This host function might return an immediate result or run for days, and the host worker might be restarted during that time. Suppose that the module in question doesn't have any state in linear memory but might have some global state that it can pass to
host_start_job
for temporary storage. I'm wondering if it might be possible to store the current Wasm module execution stack (e.g., in a database) if the
host_start_job
call doesn't complete immediately, and then reinitialize the module later (possibly in a different worker), continuing the execution of the module as if the function completed synchronously, transparently to the guest module?
c
Not currently no, but you could build this yourself with host functions. I’ve seen wasm programs in the crypto space have a notion of “gas” or “metering”, effectively you would store the amount of credit you have left in some global and insert checks into your program at intervals which stop execution when the amount drops below zero. Heres a really good blog on just that: https://agryaznov.com/posts/wasm-gas-metering/
a
Thanks, this is a related question of interrupting Wasm module execution at arbitrary points but it still wouldn't make it possible to snapshot the state of the Wasm module with it's stack, globals, etc to be restored later. In the case of a guest -> host function call, the host receives the control back from the guest anyway and I'm looking for ways to suspend the module completely, store its state somewhere in the db for example and later, when the host function call result is ready, re-initialize the module, restore the execution stack and pass the return value to the in-flight function call. This is something similar to what Asyncify does but it seems to be deprecated and fails with some internal error with Kotlin-produced Wasm. Do you maybe know of any approach to achieve this?
c
Yes and no 😅 . Wasm modules can export most of their state, state is stored in: memories, tables, globals. If you could serialise these then you would be most of the way there, however currently these classes are not serialisable. Pausing an in process function would also require me to give access to the interpreters execution stack which is currently not accessible. If you were really keen you could make a syscall from a host function to something like CRIU and just freeze everything from a higher level of abstraction
a
Yeah, that would be too removed from the Wasm execution 🙂 Ok, thanks!