-
-
Notifications
You must be signed in to change notification settings - Fork 709
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
create_resource
panics when server is compiled to wasm32-wasi
#2056
Comments
I re-read the Hydration Bugs part of the book and now wonder if the correct fix is to enclose the |
Excellent, thanks for raising this point! Initially But I have a question: Is this really the recommended way to do async in Spin?
I don't know enough about the architecture: Does Spin handle separate requests as separate threads? i.e., is it safe to block on async like this? Or is there some other async support? |
@gbj Spin handles separate requests as separate WebAssembly instances: that is, each request instantiates a request handler but that handler runs entirely separately from any other handlers. The handler is, I believe, single-threaded. That said, Spin handlers do have a limited level of async/await. For example, if a handler makes an outbound HTTP call, it gets a future back, and can await that. But it isn't general-purpose async/await. We can't use That said, I don't know if it's possible to have a long-running WASI host - one where a single instance handles all requests within the same module. I don't think WASI threading would enable that yet, but it could be a future risk with my fix. I'll follow up with the Spin WASI experts to see what they think. |
In that case your solution sounds correct and I'd accept it as a PR in a heartbeat. |
Thanks for the encouragement @gbj. I ran this past my colleague @dicej who is more knowledgeable about WASI and Spin internals than me, and he flagged a concern that the implementation of I've tested this and it Works On My Machine for the Longer term there is some stuff in the Spin SDK that we might be able to extract and generalise into a more robust |
quick note about ie. #[tokio::main(flavor = "current_thread")]
async fn main() {
...
} & in your cargo.toml, tokio supports the following features: tokio = { version = "1.25.0", features = [
"rt",
"sync",
"macros",
"io-util",
], optional = true } In a wasm32-wasi context, you can use the "time" feature in tokio too. See here for more info: https://docs.rs/tokio/latest/tokio/#wasm-support I still don't have it sorted out yet, but I'm working on putting together a wasm/wasi template for Leptos+Axum - great to see your work here as well! |
@diversable Hmm, interesting. A Spin handler can't declare a |
FYI: here it is for reference: hyperium/hyper#2900 tokio does have wasi support, though, and apparently there aren't any blockers from axum itself. I'm not familiar enough with Spin to know if it would even support running hyper .. i'm still learning the finer details of wasi rt's there is this example with hyper client wasi support, though: https://github.com/WasmEdge/wasmedge_hyper_demo |
Describe the bug
I am using SSR and building the server as a Spin application. This means the server is compiled to
wasm32-wasi
rather than native code.When I tried to run a variant of the
counter_isomorphic
example, the server panicked on thecreate_resource
call (based onleptos/examples/counter_isomorphic/src/counters.rs
Line 102 in 1cd6603
The relevant part of the backtrace is:
The issue appears to arise in the
leptos-reactive
spawn_local
function (leptos/leptos_reactive/src/spawn.rs
Line 78 in 1cd6603
wasm32
it useswasm_bindgen_futures::spawn_local
, which depends on access to JavaScript globals (https://github.com/rustwasm/wasm-bindgen/blob/bb12058e1facd26c5c647282a125088535be577e/crates/futures/src/queue.rs#L88). In Spin SSR, the server code is Wasm but JavaScript is not present, hence, panic.Leptos Dependencies
The server environment was Spin 2.0.1, although this is of course not an official integration, and is very much experimental at this point!
To Reproduce
Steps to reproduce the behavior:
wasm32-wasi
targetcounter-isomorphic
branchcargo leptos build --release
(this builds the client side Wasm and the assets; we ignore the native binary)spin up --build
(this builds the app with thessr
feature, and runs it)Expected behavior
Expected to see Incr and Decr buttons and a counter that went up and down
Additional context
I understand that what I am doing is completely experimental, and I am very much finding my way. It's entirely possible I've gone about this the wrong way and the problem is all on my end!
I had a crack at fixing it by adding the following clause at the top of the
spawn_local
function:This appeared to work and I am now seeing a working page. I don't know if it's a reasonable approach or if it could cause problems in more sophisticated apps, and I appreciate you may be reluctant to take a fix that is presumably not hit by testing. But I'd be delighted to send a PR if you're open to it.
The text was updated successfully, but these errors were encountered: