question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] (RunAnd)WaitFor(Request/Response/RequestFinished)Async methods ignore exceptions thrown by action

See original GitHub issue

Context:

  • Playwright Version: 1.31.0
  • Operating System: any (Windows, Linux, MacOS)
  • .NET version: .NET 7
  • Browser: Edge (Chromium)

Code Snippet

using System.Threading.Tasks;
using Microsoft.Playwright;

class Program
{
    public static async Task Main()
    {
        using var playwright = await Playwright.CreateAsync();
        await using var browser = await playwright.Chromium.LaunchAsync();
        var page = await browser.NewPageAsync();
        var options = new() { Predicate = r => true };
        var wait = page.RunAndWaitForRequestFinishedAsync(()=>throw new Exception(), options)
        await wait;
        // dead waiting - only timeout and no original exception
    }
}

Description

(RunAnd)WaitFor(Request/Response/RequestFinished)Async method family are implemented by InnerWaitForEventAsync in such a way that ignores exceptions thrown by action. If the action ends up in Failed state (the resulting Task has failed because of some Exception) the actual Wait inside Playwright doesn’t inspect the exception and the failed path ends up in timeout state. The Waiter makes that happen by throwing TimeoutException.

The actual BUG is the fact that the Waiter still waits for some request to filter - but the action that was supposed to trigger some chit-chat that would caused some Requests has thrown in this scenario.

Action throwing -> Exception captured and set on a transient task in WhenAll -> the Waiter waits until timeout.

Secondly there is no CancellationToken to cancel Wait when a caller of (...)WaitFor(...) has some knowledge that the wait became pointless. There is some meaningful amount of time that can be recovered by providing a cancellation here.

So, to summarize:

  1. Exceptions from action are missing
  2. No cancellation option for Waits

The exact places in code: playwright-dotnet\src\Playwright\Core\Page.cs : 435-438 playwright-dotnet\src\Playwright\Core\Waiter.cs - whole implementation - there should be a RejectOnCancellation or something along those lines.

Patched by: https://github.com/microsoft/playwright-dotnet/pull/2535

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mxschmittcommented, Apr 19, 2023

Thanks!

0reactions
maw136commented, Apr 19, 2023

PR approved, awaiting merge and release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Catching Exceptions in async methods when not called ...
Any exception thrown from async state machine will get caught and re-thrown when the task is await ed (except for those async void...
Read more >
Bug - async and uncaught Exceptions
Hello everyone, In one of the projects I'm working on, we're using async and await in order to execute code asynchronously.
Read more >
Why exceptions in async methods are “dangerous” in C# | ...
No exception was thrown because the MyAsyncMethod routine is not awaited and the exception is literally lost with the Task. In that case,...
Read more >
Testing-library: avoid these mistakes in async tests
As waitFor is non-deterministic and you cannot say for sure how many times it will be called, you should never run side-effects inside...
Read more >
Because this call is not awaited, execution of the current ...
An equally important issue is what happens with exceptions that are raised in the called async method. An exception that's raised in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found