Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WapcHost usage in async contexts #92

Open
muse254 opened this issue Jul 28, 2024 · 8 comments · May be fixed by #96
Open

WapcHost usage in async contexts #92

muse254 opened this issue Jul 28, 2024 · 8 comments · May be fixed by #96

Comments

@muse254
Copy link
Contributor

muse254 commented Jul 28, 2024

I'd like to request for WapcHost to have another implementation that can be used in async environments by the caller.
I'm aware that execution of WebAssembly modules is single threaded.

No amount of ceremony helped sidestep this issue. Also redoing the implementation in project doesn't work because of private fields/methods accessed.

Here's what I settled for reference: https://github.com/muse254/wapc-rs/pull/1/files

The error related to this: (self holds WapcHost in it's fields)

error: future cannot be sent between threads safely
  --> harness-node/src/main.rs:50:5
   |
50 | /     pub(crate) async fn handler(
51 | |         &mut self,
52 | |         req: Request,
53 | |     ) -> HarnessResult<Response<Cursor<Vec<u8>>>> {
   | |_________________________________________________^
   |
   = note: `(dyn wapc::wapchost::traits::WebAssemblyEngineProvider + 'static)` doesn't implement `std::marker::Send`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#future_not_send
@flavio
Copy link
Member

flavio commented Aug 5, 2024

@muse254 that's super nice. This is something I wanted to implement since a long time. I'll take a look at the drafted PR during this week.

@flavio
Copy link
Member

flavio commented Aug 12, 2024

I looked into your draft PR, then I started working on the topic during the weekend.

The amount of changes to be done was massive. Everything seems to be working 😀
I’m going to open a PR in the next days

@muse254
Copy link
Contributor Author

muse254 commented Aug 13, 2024

I'll be looking forward to the next release. Thank you for the fast turnaround.

flavio added a commit to flavio/wapc-rs that referenced this issue Aug 13, 2024
Allow waPC host to be used inside of an asynchronous runtime

New structs and traits have been added:

- New struct `WapcHostAsync`, this is the equivalent of `WapcHost` but for async environments.
- New trait `WebAssemblyEngineProviderAsync`, this is the equivalent of `WebAssemblyEngineProvider` but for async environments.
- New trait `ModuleHostAsync`, this is the equivalent of `ModuleHost` but for async environments.

An async runtime has an `async` host callback function. The new type alias `HostCallbackAsync` is used to define this function.
The signature of this function is slightly different from the one of the `sync` counterpart. All the input paremeters are owned types,
not pointers. That's required because the function can be sent to different threads by the async runtime; introducing lifetime constraints
proved to be impossible.

The async support is behind a feature flag named `asyc`. This flag is enabled by default.
The implementation relies on the tokio crate.

Other notable changes:

- The `sync` code has been moved to dedicated files to separate it from the `async` code.
- The documentation now states which feature flags might be required to use a struct, type, trait, or method.

Fixes wapc#92

Signed-off-by: Flavio Castelli <[email protected]>
flavio added a commit to flavio/wapc-rs that referenced this issue Aug 13, 2024
Allow usage of wasmtime-provider inside of an asynchronous runtime.

New structs and traits have been added:

- New struct `WasmtimeEngineProviderAsync`, this is the equivalent of `WasmtimeEngineProvider` but for async environments.
- New struct `WasmtimeEngineProviderAsyncPre`, this is the equivalent of `WasmtimeEngineProviderPre` but for async environments.
- The `WasmtimeEngineProviderBuilder` has been extened to allow the
  creation of the new "async" objects describe above.

The async support is behind a feature flag named `asyc`. This flag is enabled by default.
The implementation relies on the tokio crate.

Other notable changes:

- The `sync` code has been moved to dedicated files to separate it from the `async` code.
- The documentation now states which feature flags might be required to use a struct, type, trait, or method.
- New and improved tests
- Reorganized the examples, new ones have been introduced for the async
  counterparts
- Improved `make test` target to cover all the different combination of
  feature flags
- Fix compilation errors caused by different flag combinations

Fixes wapc#92

Signed-off-by: Flavio Castelli <[email protected]>
@flavio flavio linked a pull request Aug 13, 2024 that will close this issue
@flavio
Copy link
Member

flavio commented Aug 13, 2024

@muse254 take a look at the linked PR and let me know what do you think about it. I've also added examples

@muse254
Copy link
Contributor Author

muse254 commented Aug 13, 2024

Hey, it looks really fleshed out. I'll be trying it out to see if I have any issues to report

@muse254
Copy link
Contributor Author

muse254 commented Aug 13, 2024

@flavio I've done some initial tests, and everything seems to be working well, at least for my use cases with WapcHostAsync and related functionality. I'm okay with the implementation as it is.
Thanks again for the quick turnaround!

@muse254
Copy link
Contributor Author

muse254 commented Sep 2, 2024

Hey, how's the review coming along. Saw that it's stale.
@flavio

@flavio
Copy link
Member

flavio commented Sep 3, 2024

@muse254 I've pinged the other waPC maintainers on #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants