Skip to content

chore: allow forking control flow in route #1375

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

Merged
merged 1 commit into from
Jun 24, 2022
Merged
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
7 changes: 6 additions & 1 deletion playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ async def route(
) -> None:
self._routes.insert(
0,
RouteHandler(URLMatcher(self._options.get("baseURL"), url), handler, times),
RouteHandler(
URLMatcher(self._options.get("baseURL"), url),
handler,
True if self._dispatcher_fiber else False,
times,
),
)
if len(self._routes) == 1:
await self._channel.send(
Expand Down
3 changes: 3 additions & 0 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ def dispatch(self, msg: ParsedMessagePayload) -> None:
try:
if self._is_sync:
for listener in object._channel.listeners(method):
# Each event handler is a potentilly blocking context, create a fiber for each
# and switch to them in order, until they block inside and pass control to each
# other and then eventually back to dispatcher as listener functions return.
g = greenlet(listener)
g.switch(self._replace_guids_with_channels(params))
else:
Expand Down
25 changes: 19 additions & 6 deletions playwright/_impl/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
)
from urllib.parse import urljoin

from greenlet import greenlet

from playwright._impl._api_structures import NameValue
from playwright._impl._api_types import Error, TimeoutError

Expand Down Expand Up @@ -208,23 +210,34 @@ def __init__(
self,
matcher: URLMatcher,
handler: RouteHandlerCallback,
is_sync: bool,
times: Optional[int] = None,
):
self.matcher = matcher
self.handler = handler
self._times = times if times else math.inf
self._handled_count = 0
self._is_sync = is_sync

def matches(self, request_url: str) -> bool:
return self.matcher.matches(request_url)

def handle(self, route: "Route", request: "Request") -> None:
self._handled_count += 1
result = cast(
Callable[["Route", "Request"], Union[Coroutine, Any]], self.handler
)(route, request)
if inspect.iscoroutine(result):
asyncio.create_task(result)
def impl() -> None:
self._handled_count += 1
result = cast(
Callable[["Route", "Request"], Union[Coroutine, Any]], self.handler
)(route, request)
if inspect.iscoroutine(result):
asyncio.create_task(result)

# As with event handlers, each route handler is a potentially blocking context
# so it needs a fiber.
if self._is_sync:
g = greenlet(impl)
Copy link
Member

Choose a reason for hiding this comment

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

Ah…so this is the key bit here to prevent blocking where we were getting stuck before?

g.switch()
else:
impl()

@property
def is_active(self) -> bool:
Expand Down
27 changes: 16 additions & 11 deletions playwright/_impl/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,21 @@ def _on_frame_detached(self, frame: Frame) -> None:
self.emit(Page.Events.FrameDetached, frame)

def _on_route(self, route: Route, request: Request) -> None:
for handler_entry in self._routes:
if handler_entry.matches(request.url):
try:
handler_entry.handle(route, request)
finally:
if not handler_entry.is_active:
self._routes.remove(handler_entry)
if len(self._routes) == 0:
asyncio.create_task(self._disable_interception())
return
self._browser_context._on_route(route, request)
# Make this artificially async so that we could chain routes.
async def inner_route() -> None:
for handler_entry in self._routes:
if handler_entry.matches(request.url):
try:
handler_entry.handle(route, request)
finally:
if not handler_entry.is_active:
self._routes.remove(handler_entry)
if len(self._routes) == 0:
asyncio.create_task(self._disable_interception())
return
self._browser_context._on_route(route, request)

asyncio.create_task(inner_route())

def _on_binding(self, binding_call: "BindingCall") -> None:
func = self._bindings.get(binding_call._initializer["name"])
Expand Down Expand Up @@ -578,6 +582,7 @@ async def route(
RouteHandler(
URLMatcher(self._browser_context._options.get("baseURL"), url),
handler,
True if self._dispatcher_fiber else False,
times,
),
)
Expand Down
8 changes: 7 additions & 1 deletion playwright/sync_api/_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ def __enter__(self) -> SyncPlaywright:
self._watcher = ThreadedChildWatcher()
asyncio.set_child_watcher(self._watcher) # type: ignore

# Create a new fiber for the protocol dispatcher. It will be pumping events
# until the end of times. We will pass control to that fiber every time we
# block while waiting for a response.
def greenlet_main() -> None:
self._loop.run_until_complete(self._connection.run_as_sync())

dispatcher_fiber = greenlet(greenlet_main)

self._connection = Connection(
dispatcher_fiber,
create_remote_object,
Expand All @@ -77,9 +81,11 @@ def callback_wrapper(playwright_impl: Playwright) -> None:
self._playwright = SyncPlaywright(playwright_impl)
g_self.switch()

# Switch control to the dispatcher, it'll fire an event and pass control to
# the calling greenlet.
self._connection.call_on_object_with_known_name("Playwright", callback_wrapper)

dispatcher_fiber.switch()

playwright = self._playwright
playwright.stop = self.__exit__ # type: ignore
return playwright
Expand Down