Add wasm32-wasip2 backend using wasi:clocks#85
Open
cargopete wants to merge 1 commit into
Open
Conversation
The thread-based native backend cannot run on wasm32-wasip2 (the WASI component model is single-threaded), so polling a Delay there aborts with "timer has gone away". Add a target-gated backend that arms a wasi:clocks/monotonic-clock timer and awaits it through the wstd reactor. Selected via target_env = "p2" so wasip1 and future previews keep the native backend. The wstd/wasip2 dependencies are pulled in only for wasm32-wasip2, where the native backend is already broken at runtime, so there is no regression for existing targets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
wasm32-wasip2backend forDelay, backed bywasi:clocks/monotonic-clockand driven by thewstdreactor.Motivation
On
wasm32-wasip2there is nowasm-bindgen, sofutures-timerfalls into thenativebackend — which spawns a background timer-wheel thread. The WASI Preview 2 component model is single-threaded, so that thread never runs and the first poll of anyDelayaborts at runtime:This makes
futures-timerunusable onwasm32-wasip2today, which in turn breaks every downstream library that relies on it (e.g. severalrust-libp2pbehaviours: ping, kad, gossipsub, request-response, rendezvous, and the Swarm idle-connection timeout). Because[patch.crates-io]only applies in the final binary's workspace, downstream libraries cannot fix this for their own users — it has to be fixed here.What this does
Adds a third backend selected via
target_env = "p2"(sowasm32-wasip1and any future preview stay onnative):wasm32-wasip2wasi:clocks+wstdreactorwasm32+wasm-bindgen(non-wasip2)wasm(setTimeoutviagloo-timers)native(thread-based timer wheel)The new
Delaymatches the existing API exactly:new(dur),reset(&mut self, dur),impl Future<Output = ()>,impl Debug.The
wstd/wasip2dependencies are gated tocfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p2")), so they are pulled in only for the target where the existing backend is already broken — there is no dependency or behavioural change for any currently-working target.Why this is low-risk
The
nativebackend already panics at runtime onwasm32-wasip2, so this is a pure fix: no working configuration changes.native(host) andwasm32-wasip1(stays onnative) are unaffected, and thewasm-bindgenbrowser path is unchanged.Testing
wasm32-wasip2(new backend),wasm32-wasip1(falls through tonative, nowstdpulled in), and the host target (unchanged).rust-libp2p: alibp2p-pingSwarm running as awasm32-wasip2component completes a full ping round-trip (real RTT, no panic) whenfutures-timeris patched to this branch.Note for reviewers
wstdis the de-facto async reactor forwasm32-wasip2; the backend awaits thewasi:clockspollable through it. A fully reactor-agnostic implementation isn't possible because WASI 0.2 has no ambient reactor — each runtime supplies its own. If a target-gatedwstddependency is unwelcome, I'm happy to move the backend behind an opt-inwasip2feature instead of auto-selecting it. Let me know which you'd prefer.