Stream: general

Topic: questions about stack switching


view this post on Zulip Timothy Galebach (Jun 29 2023 at 09:08):

Hi all, been digging into Wasmtime and am excited. I'm trying to figure out whether something is in principle doable, or whether I'd be fighting the system too much. Is it possible for a module to pause/yield its own execution, have its Store be written, and then have the host restart it from the point where it was in execution? The goal is just to enable async/await semantics from inside modules.

Apologies if this is the wrong place to post--just feeling my way around Zulip today.

view this post on Zulip YAMAMOTO Yuji (Jun 29 2023 at 09:19):

I guess https://github.com/WebAssembly/stack-switching is similar to what you want to do, or https://emscripten.org/docs/porting/asyncify.html will help you if Emscripten is available.

A repository for the stack switching proposal. Contribute to WebAssembly/stack-switching development by creating an account on GitHub.

view this post on Zulip Timothy Galebach (Jun 29 2023 at 09:25):

Yes, stack-switching definitely matches what I want. What is its status...still just a proposal?

view this post on Zulip YAMAMOTO Yuji (Jun 29 2023 at 09:27):

Unfortunately, yes. I don't know any implementation of it.

view this post on Zulip Ramon Klass (Jun 29 2023 at 09:34):

I think what you describe is already how host function calls work, I mean, your wasm can call a host function. This halts the wasm vm and lets the host do whatever the function does, when the function is done the return value is written to wasm memory and then the wasm vm gets resumed with a pointer to the correct place in memory where the new data was written, at least that's how wasi_preview1 worked

view this post on Zulip Timothy Galebach (Jun 29 2023 at 09:40):

Ramon Klass said:

I think what you describe is already how host function calls work, I mean, your wasm can call a host function. This halts the wasm vm and lets the host do whatever the function does, when the function is done the return value is written to wasm memory and then the wasm vm gets resumed with a pointer to the correct place in memory where the new data was written, at least that's how wasi_preview1 worked

Thanks, I think this is exactly what I was looking for. If anyone can confirm or show a quick example, would be amazing.

view this post on Zulip Daniel Hillerström (Jun 29 2023 at 10:45):

Yes, stack-switching definitely matches what I want. What is its status...still just a proposal?

Unfortunately, yes. I don't know any implementation of it.

FWIW I am one of the coauthors of typed continuations proposal for stack switching. We have an implementation of it in Wasmtime, that I intend to announce at the next stacks subgroup meeting. I am hopeful that we can advance the phase of the stack switching proposal later this year.

view this post on Zulip Lann Martin (Jun 29 2023 at 13:01):

If you use the async feature (read these docs carefully!) of wasmtime then host-implemented functions can themselves be async. The asyncness is invisible to the guest environment but it allows the host to perform e.g. async I/O

view this post on Zulip Notification Bot (Jun 29 2023 at 15:06):

8 messages were moved here from #general > new streams by Alex Crichton.

view this post on Zulip fitzgen (he/him) (Jun 29 2023 at 15:19):

Timothy Galebach said:

Is it possible for a module to pause/yield its own execution, have its Store be written, and then have the host restart it from the point where it was in execution? T

When you say "have its Store be written", do you mean like actually serialized and written out to disk? Wasmtime does not support this kind of snapshotting and rehydration at this time. It is a very big feature to implement.

But in general, Wasmtime's async support lets you pause Wasm execution at any host call, epoch interrupt, or when fuel runs out. You can then resume execution whenever you'd like. But the Wasm instance, though paused, is still resident in the process's memory and cannot be written to disk or resumed in a different process or anything like that.

view this post on Zulip Timothy Galebach (Jun 30 2023 at 14:05):

fitzgen (he/him) said:

Timothy Galebach said:

Is it possible for a module to pause/yield its own execution, have its Store be written, and then have the host restart it from the point where it was in execution? T

When you say "have its Store be written", do you mean like actually serialized and written out to disk? Wasmtime does not support this kind of snapshotting and rehydration at this time. It is a very big feature to implement.

But in general, Wasmtime's async support lets you pause Wasm execution at any host call, epoch interrupt, or when fuel runs out. You can then resume execution whenever you'd like. But the Wasm instance, though paused, is still resident in the process's memory and cannot be written to disk or resumed in a different process or anything like that.

Thanks, that's exactly what I was asking. I didn't mean writing to disk--I just meant writing to the Store's memory, which you answered.

I've seen the fuel calls and epoch interrupt...could you point me to documentation of using an arbitrary host call to pause Wasm execution?

view this post on Zulip Lann Martin (Jun 30 2023 at 14:28):

"Pausing" execution is the async feature. If an async host call returns a pending future, that gets (transparently to the guest) passed back down the stack to the intial async call into the guest, which effectively pauses the guest until the future is ready

view this post on Zulip Robin Brown (Jun 30 2023 at 14:29):

To summarize, all you have to do to pause a Wasm execution at a host call is pause in the implementation of that host function.


Last updated: Nov 22 2024 at 16:03 UTC