Skip to content

Conversation

magodo
Copy link
Contributor

@magodo magodo commented Jul 30, 2025

The typespec_client_core::sleep::sleep is using the async_runtime's sleep implementation, which is complicated to support wasm32, and will panic now when targeting to wasm32. Instead of make the standard_runtime to support wasm32 sleep (I wonder if it's feasible), we can instead just introduce another sleep implementation to simply use the gloo_timers::future::sleep.

Also, this PR adds a cfg_attr for conditionally remove the Send trait from the async_trait for the RetryPolicy, as the other parts of the code do.

Fix #2754
Supersedes: #2770

Test

sdk/typespec/typespec_client_core on  wasm_sleep is 📦 v0.6.0 via 🦀 v1.88.0
💤  cargo test -q

running 90 tests
i.................................................................i.................... 87/90
...
test result: ok. 88 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out; finished in 0.84s


running 23 tests
.......................
test result: ok. 23 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.27s


sdk/typespec/typespec_client_core on  wasm_sleep is 📦 v0.6.0 via 🦀 v1.88.0 took 4s
💤  cargo test --features=tokio -q

running 91 tests
i.....................................................................i................ 87/91
....
test result: ok. 89 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out; finished in 0.10s


running 23 tests
.......................
test result: ok. 23 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.21s


sdk/typespec/typespec_client_core on  wasm_sleep is 📦 v0.6.0 via 🦀 v1.88.0 took 3s
💤  RUSTFLAGS='--cfg getrandom_backend="wasm_js"' wasm-pack test --node --features wasm-bindgen
[INFO]: 🎯  Checking for the Wasm target...
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
[INFO]: ⬇️   Installing wasm-bindgen...
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.11s
     Running unittests src/lib.rs (/home/magodo/github/azure-sdk-for-rust/target/wasm32-unknown-unknown/debug/deps/typespec_client_core-365ee3948d4ee278.wasm)
running 2 tests
test async_runtime::tests::wasm_bindgen_task_execution ... ok
test async_runtime::tests::test_sleep ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; finished in 0.17s

Some note about the wasm32 test:

  • We can't run the spawned job using futures::executor::block_on(handle), as it introduces a deadlock in browser (since the block_on blocks the current thread)
  • The sleep test I can't use Instant::now() when using node as runtime

@Copilot Copilot AI review requested due to automatic review settings July 30, 2025 04:41
@github-actions github-actions bot added Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Jul 30, 2025
@github-actions
Copy link

Thank you for your contribution @magodo! We will review the pull request and get back to you soon.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces WASM support for typespec_client_core by adding a new web_runtime module that provides a WASM-compatible async runtime implementation. The main goal is to fix the sleep functionality that was previously panicking on wasm32-unknown-unknown targets.

  • Adds a new WasmBindgenRuntime implementation using gloo-timers for sleep and wasm-bindgen-futures for task spawning
  • Updates the RetryPolicy trait to conditionally remove the Send bound for WASM targets
  • Adds comprehensive test coverage for WASM functionality using wasm-bindgen-test

Reviewed Changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Cargo.toml Adds workspace dependencies for gloo-timers and wasm-bindgen-futures
sdk/typespec/typespec_client_core/Cargo.toml Adds optional dependencies and WASM-specific features configuration
sdk/typespec/typespec_client_core/src/async_runtime/mod.rs Updates runtime creation logic to prioritize WASM runtime when appropriate
sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs Implements new WASM-compatible async runtime using browser APIs
sdk/typespec/typespec_client_core/src/async_runtime/tests.rs Adds WASM-specific tests and conditional compilation for existing tests
sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs Updates sleep method signature to use TaskFuture type alias
sdk/typespec/typespec_client_core/src/http/policies/retry/mod.rs Conditionally removes Send bound from async_trait for WASM compatibility
eng/dict/crates.txt Adds gloo to the dictionary for spell checking

magodo and others added 17 commits August 19, 2025 13:10
…` when target wasm

The `typespec_client_core::sleep::sleep` is using the async_runtime's sleep implementation, which is complicated to support `wasm32`, and will panic now when targeting to `wasm32`. Instead of make the `standard_runtime` to support wasm32 sleep (I wonder if it's feasible), we can instead just introduce another `sleep` implementation to simply use the `gloo_timers::future::sleep`.

Also, this PR adds a `cfg_attr` for conditionally remove the `Send` trait from the `async_trait` for the `RetryPolicy`, as the other parts of the code do.

Additionally, the `getrandom` crate will enable `wasm_js` features when targetting to `wasm`, which is necessary.
If we only test wasm_bindgen, some json tests were failing. Also updates CHANGELOG.
Copy link
Member

@heaths heaths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with my changes in there.

@heaths
Copy link
Member

heaths commented Aug 19, 2025

The macOS issue is the #2887 issue and not really a blocker.

@heaths
Copy link
Member

heaths commented Aug 19, 2025

/check-enforcer override

@heaths
Copy link
Member

heaths commented Aug 19, 2025

Thanks @magodo for all the work on this!

@heaths heaths merged commit 36b1667 into Azure:main Aug 19, 2025
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasm32-unknown-unknown failed to run

3 participants