Comments (8)
@diversable Hmm, interesting. A Spin handler can't declare a tokio::main
(it's a wasi-http
incoming-handler
) but presumably we could create a Tokio single-threaded runtime by hand at the start of the request. I'm not sure whether that would add overhead or how it would interact with host async features, but definitely another angle to investigate. Thanks!
from leptos.
I re-read the Hydration Bugs part of the book and now wonder if the correct fix is to enclose the create_resource
in a create_effect
. But I am not sure what the scope of that create_effect
should be, because the counter resource is used later on the page.
from leptos.
Excellent, thanks for raising this point! Initially wasm-bindgen-futures
was only used for the browser, then a PR was made to use it for all wasm
targets so that wasm32-unknown-unknown
running in a JS environment on the server(less?) (Deno Deploy, Cloudflare Workers, etc.) would work. But obviously this then doesn't work for wasm32-wasi
, which I definitely want to move toward full support for.
But I have a question: Is this really the recommended way to do async in Spin?
if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] {
futures::executor::block_on(fut)
}
else if #[cfg(target_arch = "wasm32")] {
// ... rest as before ...
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?
from leptos.
@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 tokio
or other concurrent runtimes. It is, as far as I know, strictly bound to implementations of WASI pseudo-futures in the Spin host.
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.
from leptos.
In that case your solution sounds correct and I'd accept it as a PR in a heartbeat.
from leptos.
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 futures::executor::block_on
would block forever if the future was pending when first polled. There's no great solution here, but Joel's advice was that we should poll the future in the same way as futures::executor::block_on
, but if the future was not ready, we should panic rather than looping and polling again.
I've tested this and it Works On My Machine for the counter_isomorphic
sample. I'll send a PR and can cover off some of the considerations there.
Longer term there is some stuff in the Spin SDK that we might be able to extract and generalise into a more robust poll_list
-based executor for WASI. I'm not sure what the effort and risks there are though...!
from leptos.
... We can't use
tokio
or other concurrent runtimes. ....
quick note about tokio
, since I've spent some time trying to get Axum & Tokio to work properly with Leptos in a wasm / wasi context: Tokio does actually have a 'single-threaded' mode if you add the right 'flavor' to your tokio main fn.
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!
from leptos.
FYI:
there seems to be an issue with running the full Leptos+Axum stack in a wasi context - it looks like hyper hasn't accepted the pull request that adds wasi support (yet)..
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
from leptos.
Related Issues (20)
- DynChild is populated as a single Child in the components' children property HOT 4
- Breaking change in view macro between 0.6.10 and 0.6.11
- Allow custom derive for server_fns with custom encoding HOT 3
- Add Trigger::dispose
- leptos_macro 0.6.11 leptos::view! breaks with some comments
- Non-'static signals
- the format!() bug in <a href={format!()}> HOT 2
- Effects/Memos should't be able to "own" a signal HOT 4
- Nested Suspense ignores SSR Mode and breaks Hydration HOT 2
- Error when running server function inside spawn_local HOT 5
- Click events are registered twice when two leptos custom elements are on a page HOT 3
- about mobile support
- Recursive effects never run after recursing HOT 4
- Clippy warnings in some components with rust 1.78 HOT 1
- rkyv, avoid the copy to byte ? HOT 4
- Check ActionForm input names at compile time HOT 4
- Allow server components in islands architecture to call code behind ssr without need for #[server] HOT 1
- porting over some react code to my project, having hydration issues
- leptos_router parent Route attr view is not generating view for dynamic routes HOT 4
- relese build causes SIGSEGV and SIGBUS while dev build is just fine HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leptos.