Skip to content
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
10 changes: 8 additions & 2 deletions playwright/_impl/_greenlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ def wait_for_future(
) -> None:
__tracebackhide__ = True
g_self = greenlet.getcurrent()
waiting = True

def resume(_: "asyncio.Future") -> None:
g_self.switch()
if waiting:
g_self.switch()

# The wait owns the callback that resumes this greenlet, so that it can
# remove it again when the wait is abandoned. A callback left behind would
# remove it again when the wait ends. A callback left behind would
# resume this greenlet at an arbitrary later point.
future.add_done_callback(resume)
try:
Expand All @@ -118,6 +120,10 @@ def resume(_: "asyncio.Future") -> None:
future.cancel()
raise
finally:
# A done future has already queued its callbacks; removing them from the
# future cannot stop them, so also make a queued callback inactive.
waiting = False
future.remove_done_callback(resume)
# The loop is only running for as long as the dispatcher fiber is alive.
# Also after a signal handler interrupted it: run_forever() cleared this
# on its way out, but the dispatcher is going to run the loop again.
Expand Down
4 changes: 4 additions & 0 deletions tests/sync/python/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ def _test_call_sync_method_after_playwright_close_with_own_loop(
context = browser.new_context()
page = context.new_page()
page.goto(empty_page)
with page.expect_console_message(lambda m: m.text == "hello") as console_info:
page.evaluate("console.log('hello')")
assert console_info.value.text == "hello"
playwright.stop()
with pytest.raises(Error) as exc:
page.evaluate("1+1")
Expand All @@ -196,6 +199,7 @@ def _test_call_sync_method_after_playwright_close_with_own_loop(
def test_call_sync_method_after_playwright_close_with_own_loop(
server: Server, browser_name: str, launch_arguments: Dict[str, Any]
) -> None:
# Regression test for https://github.com/microsoft/playwright-python/issues/3307.
p = multiprocessing.get_context("spawn").Process(
target=_test_call_sync_method_after_playwright_close_with_own_loop,
args=[browser_name, launch_arguments, server.EMPTY_PAGE],
Expand Down
Loading