Avoid blocking SemaphoreSlim.Wait(0) on single-threaded browser WASM - #2756
Merged
Merged
Conversation
pavelsavara
marked this pull request as ready for review
August 10, 2026 16:02
JamesNK
approved these changes
Aug 11, 2026
This was referenced Sep 21, 2026
Bump the nuget-production-dependencies group with 22 updates
PraveenKumarDova/opentelemetry-demo#406
Closed
Bump the nuget-production-dependencies group with 22 updates
laurentpf5/opentelemetry-nginx-demo#541
Closed
Closed
Closed
This was referenced Sep 24, 2026
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.
Fixes #2753.
Related runtime issue: dotnet/runtime#131859.
Problem
On .NET 11 preview single-threaded browser-wasm (Blazor WebAssembly), every
gRPC-Web call made through
GrpcChannelhangs forever: the returned task nevercompletes, no HTTP request is issued, and no exception surfaces anywhere.
The root cause is a synchronous, non-blocking try-acquire in the balancer:
A runtime change (dotnet/runtime#123329)
makes
SemaphoreSlim.WaitCorethrowPlatformNotSupportedExceptiononsingle-threaded WASM for any potentially-blocking wait. The multithreading guard
sits ahead of the immediate-acquire path, and only the fail-fast branch
(
timeout == 0 && count == 0) escapes it. SoWait(0)against an availablesemaphore — an operation that by definition cannot block — walks into the guard
and throws:
Because the connect runs as a fire-and-forget task (
_ = RunCall(...)), theexception never resolves the call's response, so the caller's
awaitstayspending forever with no observable error.
The balancer path is active even for a plain
GrpcChannel.ForAddress("https://…", new GrpcChannelOptions { HttpHandler = new GrpcWebHandler(...) })channel, because
BalancerHttpHandlerwraps the user handler chain whenSUPPORT_LOAD_BALANCINGis compiled in — which it is for the TFM build BlazorWebAssembly serves.
Fix
Replace the blocking
Wait(0)withWaitAsync(millisecondsTimeout: 0):SemaphoreSlim.WaitAsyncnever blocks a thread, so it does not go through theThrowIfMultithreadingIsNotSupportedguard. With a zero timeout it alwayscompletes synchronously, returning a cached completed task whose
Resultreports whether the semaphore was acquired. This preserves the exact original
semantics — try-acquire without waiting, and otherwise queue a full async wait —
while working correctly on single-threaded browser WASM.
Testing
Grpc.Net.Clientbuilds cleanly across all target frameworks.Grpc.Net.Client.Testsonnet10.0: full suite passes (510/510), includingthe Balancer/Subchannel connect tests (62/62).