Skip to content

Conversation

@nvborisenko
Copy link

This PR make Playwright events waiter more friendly with sync-over-async pattern.

Issue

This piece of code demonstrates deadlock. Real code, you can run it (I ran it in NUnit).

await page.RunAndWaitForResponseAsync(async () =>
{
    await page.GotoAsync("https://linked.in");
}, new Regex(".*google.*"));

//await Task.Yield();

Task.Run(async () => await page.Locator(".nav__logo-link").ClickAsync()).GetAwaiter().GetResult();

Here GetAwaiter().GetResult() is blocked. We understand that it is anti-pattern, sync-over-async. To fix it we can add await Task.Yield(). But what if Playwright can be more friendly for dummy users like me?

Solution?

This PR allows tasks continuations asynchronously. Now the code above just works.

@nvborisenko
Copy link
Author

Apologies for the inquiry, but after a month without activity, I'm concerned I may have done something improperly. @mxschmitt, if you happen to know the cause, I would be very grateful for a hint. Thank you.

@Skn0tt
Copy link
Member

Skn0tt commented Dec 18, 2025

Hi Nikolay, sorry for not taking a look earlier. I'll ask our colleague Dima for a review, but it'll take a little longer due to holidays.

@Skn0tt Skn0tt requested a review from dgozman December 18, 2025 10:35
Copy link
Member

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

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

I did some digging on the ramifications of this options, and it seems like a safe change for us. The only negative implication is a slight overhead since the continuation now needs to wait for the scheduler, but that microsecond impact is neglegible given we're dealing with slow browsers.

My understanding of DotNet's async scheduling is limited, so please take another look @dgozman.

@nvborisenko
Copy link
Author

It is absolutely safe, moreover it is recommended for libraries to prevent deadlocks I demonstrated in the original issue.

Copy link
Contributor

@dgozman dgozman left a comment

Choose a reason for hiding this comment

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

The fix looks good to me. Let's add a test though, to avoid regressing this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants