Skip to content

[Blazor] Throw meaningful JSTimeoutException for JSRuntime calls that timeout #62539

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/JSInterop/Microsoft.JSInterop/src/JSRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,17 @@ public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, Cancellat
if (DefaultAsyncTimeout.HasValue)
{
using var cts = new CancellationTokenSource(DefaultAsyncTimeout.Value);
// We need to await here due to the using
return await InvokeAsync<TValue>(targetInstanceId, identifier, callType, cts.Token, args);

try
{
// We need to await here due to the using
return await InvokeAsync<TValue>(targetInstanceId, identifier, callType, cts.Token, args);
}
catch (OperationCanceledException) when (cts.Token.IsCancellationRequested)
{
// This was cancelled due to our timeout, throw a more meaningful exception
throw new TimeoutException("A JavaScript interop call timed out. Consider increasing the timeout duration if the operation is expected to take longer.");
}
Copy link
Member

@akoeplinger akoeplinger Jul 2, 2025

Choose a reason for hiding this comment

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

I think we should do the exception check in ComponentBase.cs like Safia mentioned in #21384 (comment)

The current code won't work if you e.g. pass in your own CancellationToken instead of setting DefaultAsyncTimeout.

Copy link
Member

Choose a reason for hiding this comment

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

That would be already a bigger change, with adding CancellationTokenSource parameter to public async methods like OnInitializedAsync. I did not see that when I was assigning copilot. In this case, that's not a fast fix. Let's keep it in the backlog.

}

return await InvokeAsync<TValue>(targetInstanceId, identifier, callType, CancellationToken.None, args);
Expand Down
22 changes: 20 additions & 2 deletions src/JSInterop/Microsoft.JSInterop/test/JSRuntimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void DispatchesAsyncCallsWithDistinctAsyncHandles()
}

[Fact]
public async Task InvokeAsync_CancelsAsyncTask_AfterDefaultTimeout()
public async Task InvokeAsync_ThrowsTimeoutException_AfterDefaultTimeout()
{
// Arrange
var runtime = new TestJSRuntime();
Expand All @@ -47,7 +47,7 @@ public async Task InvokeAsync_CancelsAsyncTask_AfterDefaultTimeout()
var task = runtime.InvokeAsync<object>("test identifier 1", "arg1", 123, true);

// Assert
await Assert.ThrowsAsync<TaskCanceledException>(async () => await task);
await Assert.ThrowsAsync<TimeoutException>(async () => await task);
}

[Fact]
Expand Down Expand Up @@ -82,6 +82,24 @@ public async Task InvokeAsync_CancelsAsyncTasksWhenCancellationTokenFires()
await Assert.ThrowsAsync<TaskCanceledException>(async () => await task);
}

[Fact]
public async Task InvokeAsync_CancelsWithTaskCanceledException_WhenCancellationTokenFiresBeforeTimeout()
{
// Arrange
using var cts = new CancellationTokenSource();
var runtime = new TestJSRuntime();
// Set a long timeout, but cancel before it fires
runtime.DefaultTimeout = TimeSpan.FromSeconds(10);

// Act
var task = runtime.InvokeAsync<object>("test identifier 1", cts.Token, new object[] { "arg1", 123, true });

cts.Cancel();

// Assert - Should throw TaskCanceledException, not TimeoutException
await Assert.ThrowsAsync<TaskCanceledException>(async () => await task);
}

[Fact]
public async Task InvokeAsync_DoesNotStartWorkWhenCancellationHasBeenRequested()
{
Expand Down
Loading