Skip to content
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

fix: asyncio.create_task race issue in page and brower_context #1864

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
23 changes: 21 additions & 2 deletions playwright/_impl/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
TYPE_CHECKING,
Any,
Callable,
Coroutine,
Dict,
List,
Optional,
Expand Down Expand Up @@ -308,6 +309,24 @@ def _on_video(self, params: Any) -> None:
artifact = from_channel(params["artifact"])
cast(Video, self.video)._artifact_ready(artifact)

async def _race_with_page_close(self, future: Coroutine) -> None:
fut = asyncio.create_task(future)
# Rewrite the user's stack to the new task which runs in the background.
setattr(
fut,
"__pw_stack__",
getattr(asyncio.current_task(self._loop), "__pw_stack__", inspect.stack()),
)
target_closed_future = self._closed_or_crashed_future
await asyncio.wait(
[fut, target_closed_future],
return_when=asyncio.FIRST_COMPLETED,
)
if fut.done() and fut.exception():
raise cast(BaseException, fut.exception())
if target_closed_future.done():
await asyncio.gather(fut, return_exceptions=True)

@property
def context(self) -> "BrowserContext":
return self._browser_context
Expand Down Expand Up @@ -640,8 +659,8 @@ async def route_from_har(

async def _update_interception_patterns(self) -> None:
patterns = RouteHandler.prepare_interception_patterns(self._routes)
await self._channel.send(
"setNetworkInterceptionPatterns", {"patterns": patterns}
await self._race_with_page_close(
self._channel.send("setNetworkInterceptionPatterns", {"patterns": patterns})
)

async def screenshot(
Expand Down