juggler: a live event of the engine never overtakes the replay of the events a new page missed (the cause of the popups left at about:blank on engine 63) (DRAFT, 0.51.1)
The browser buffers the events of a session nobody reads yet and `PageDispatcher` is handed them (`replay`) after it subscribed its lifecycle and injected script. The replay ran on the adoption thread while the engine's reader went on delivering live events to the same subscribers: the destination's commit (live) fell into the middle of the replay of the popup's first events and the replay then brought older ones after it - the initial about:blank (no id) and the navigationStarted of the same navigation. The lifecycle ended at about:blank (a popup at about:blank), or had its navigation pending again (Execution context was destroyed). The second Page.frameAttached of every popup in the traces of the proof is the replay: the engine sends each event once. The reader takes a gate for each event of a session; the page takes it for "subscribe, then replay" as one step (BrowserDispatcher.events_gate), with a bounded wait for the reader (5 s) so a holder that never lets go cannot stop the engine's stream. Test: tests/test_a_live_event_does_not_overtake_the_replay_of_a_new_page.py, five stops of the replay, RED on the old code (the lifecycle ends at about:blank), green with the gate. wrapper.unit.page 62 -> 67. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f18091c5a7
commit
80bc4788ef
4 files changed
+169
-19
No files matched your search
@@ -21,6 +21,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
so `page.on("load")` runs once); an event the engine sends while the held ones are being given back waits for them (a response is never handled
|
||||
before its request); a document committed at the same address as the previous one is told; a page closed while it was being adopted is not given
|
||||
frames, and is not announced, after its `__dispose__`; one frame never gets two dispatchers.
|
||||
- A popup could still be handed to the client at `about:blank` (or with its navigation pending again, and "Execution context was destroyed" in its
|
||||
first evaluate) when the destination committed while the events the page had missed were being replayed to it: the engine's reader went on
|
||||
delivering the live events to the lifecycle, and the replay then brought older ones after them. The page now subscribes its readers and is
|
||||
handed the missed events in one step, with the delivery of the sessions' events held, so a live event never overtakes the replay.
|
||||
|
||||
## [0.51.0] - 2026-11-12
|
||||
|
||||
|
||||
@@ -7,5 +7,5 @@
|
||||
{"id": "wrapper.e2e.realness", "giro": "G2", "os": ["linux", "windows"], "min_tests": 32, "exact": true, "tests": ["tests/test_realness_behind_a_declared_exit_e2e.py"], "marker": "e2e", "needs_root": ["linux"], "note": "The realness check without a real proxy: six declared exits, the values a page reads, and the roads. Linux runs as root without xdist like the DoH suite (the resolver trap); on Windows the trap does not exist and the assertion on the system resolver is the only one omitted."},
|
||||
{"id": "wrapper.unit.udp", "giro": "G1", "os": ["linux", "windows"], "min_tests": 219, "exact": true, "tests": ["tests/test_a_served_launch_cannot_turn_the_launch_counter_off.py", "tests/test_the_witness_picks_a_port_both_sockets_bind.py", "tests/test_preload_newtab_reaches_the_core_only_when_asked.py", "tests/test_the_resolver_trap_counts_its_own_users.py", "tests/test_the_doh_e2e_launch_on_a_virtual_display.py", "tests/test_the_realness_helpers.py", "tests/test_the_h3_line_helpers.py", "tests/test_the_fake_doh_delays_and_refuses.py", "tests/test_the_host_address_helper.py", "tests/test_the_udp_chain_helper.py", "tests/test_the_fake_doh_answers.py", "tests/test_doh_session_prefs_unit.py", "tests/test_the_leak_watcher_sees_a_direct_connection.py", "tests/test_the_http_proxy_helper_asks_for_credentials.py", "tests/test_every_route_command_is_answered_may_fail.py", "tests/test_the_network_declaration_reaches_the_engine.py", "tests/test_the_resolver_trap_is_required_by_the_gate.py", "tests/test_the_exit_of_a_context_is_discovered_through_its_own_gateway.py", "tests/test_the_witness_hears_a_direct_send.py"], "marker": "unit", "needs_root": false, "note": "92 = 6+32+21+7+8+3+9+6. The leak watcher file defines exactly one of its two resolver-trap tests per host (7 everywhere), so no test is skipped. `exact`: min_tests is raised by hand when a test is added."},
|
||||
{"id": "wrapper.e2e.page", "giro": "G2", "os": ["linux", "windows"], "min_tests": 115, "exact": true, "tests": ["tests/test_a_tiny_download_on_a_served_browser_is_saved_e2e.py", "tests/test_a_page_that_moves_on_e2e.py", "tests/test_a_popup_is_reported_when_the_engine_says_it_is_ready_as_playwright_does_e2e.py", "tests/test_a_popup_of_a_link_reaches_the_client_e2e.py", "tests/test_popup_destinations_e2e.py", "tests/test_popups_reach_the_client_e2e.py", "tests/test_route_superseded_e2e.py"], "marker": "e2e", "needs_root": false, "note": "114 = 100 + the 14 of test_a_page_that_moves_on_e2e.py (a navigation the page overtook with location.replace ends; the engine fix 860ef8a6d9 is proved by it). 100 counted at the collection; was 96 = 10+32+16+34+4 before the popup tests of 6200bc2). A page is handed over when the engine says Page.ready; a popup reaches the client (link, noopener, window.open; own site and another; with and without a route; in-process and over launch_server) as Playwright's does; a superseded route. Needs the candidate binary, and the stock Playwright client of the dev extra."},
|
||||
{"id": "wrapper.unit.page", "giro": "G1", "os": ["linux", "windows"], "min_tests": 62, "exact": true, "tests": ["tests/test_a_download_finished_before_it_was_announced_is_finished.py", "tests/test_every_order_of_the_events_of_a_popup_gives_one_page_event.py", "tests/test_ci_drive_gate_goes_through_the_core.py", "tests/test_a_page_is_handed_over_only_when_the_engine_says_it_is_ready.py", "tests/test_a_new_page_waits_for_the_engine.py", "tests/test_page_number_follows_the_call.py", "tests/test_a_commit_at_any_point_of_the_hand_over_reaches_the_client_once.py", "tests/test_no_event_of_any_family_is_lost_by_the_hand_over.py", "tests/test_the_hand_over_of_a_popup_holds_under_concurrency.py", "tests/test_a_page_closed_before_its_announcement_is_not_announced.py"], "marker": "unit", "needs_root": false, "note": "62 = 44 + 3+9+5+1 of the popup hand-over (24 counted at the collection before; was 20 = 3+8+1+8 before 6200bc2). The page dispatcher's wait for Page.ready and what it hands over; the drive gate goes through the core (the engine waits for the session's road)."}
|
||||
{"id": "wrapper.unit.page", "giro": "G1", "os": ["linux", "windows"], "min_tests": 67, "exact": true, "tests": ["tests/test_a_download_finished_before_it_was_announced_is_finished.py", "tests/test_every_order_of_the_events_of_a_popup_gives_one_page_event.py", "tests/test_ci_drive_gate_goes_through_the_core.py", "tests/test_a_page_is_handed_over_only_when_the_engine_says_it_is_ready.py", "tests/test_a_new_page_waits_for_the_engine.py", "tests/test_page_number_follows_the_call.py", "tests/test_a_commit_at_any_point_of_the_hand_over_reaches_the_client_once.py", "tests/test_no_event_of_any_family_is_lost_by_the_hand_over.py", "tests/test_the_hand_over_of_a_popup_holds_under_concurrency.py", "tests/test_a_page_closed_before_its_announcement_is_not_announced.py", "tests/test_a_live_event_does_not_overtake_the_replay_of_a_new_page.py"], "marker": "unit", "needs_root": false, "note": "67 = 44 + 3+9+5+1+5 of the popup hand-over (24 counted at the collection before; was 20 = 3+8+1+8 before 6200bc2). The page dispatcher's wait for Page.ready and what it hands over; the drive gate goes through the core (the engine waits for the session's road)."}
|
||||
]
|
||||
@@ -2824,9 +2824,33 @@ class PageDispatcher(Dispatcher):
|
||||
self._announce_lock = threading.RLock()
|
||||
self._announced = False
|
||||
self._said_before_announcement: List = []
|
||||
self.lifecycle = Lifecycle(conn, session)
|
||||
self.injected = InjectedScript(conn, session)
|
||||
conn.add_listener(self._route_juggler_event)
|
||||
# ⛔ SUBSCRIBE AND BE HANDED THE MISSED EVENTS IN ONE STEP, with the events of the sessions held (`events_gate`): an event the
|
||||
# engine's reader delivers between the subscription and the replay would reach the lifecycle BEFORE the older ones the replay
|
||||
# brings, and the lifecycle would end at the older state (a popup at about:blank after its destination committed). Nothing in the
|
||||
# step waits for an event or a reply: the readers are subscribed, the buffer is handed over, the gate opens.
|
||||
try:
|
||||
with context.browser.events_gate():
|
||||
self.lifecycle = Lifecycle(conn, session)
|
||||
self.injected = InjectedScript(conn, session)
|
||||
conn.add_listener(self._route_juggler_event)
|
||||
# ⛔ THE EVENTS THIS PAGE ALREADY MISSED, handed over now that the two things that need them exist. `Page.frameAttached` and
|
||||
# the `Runtime.executionContextCreated` pair are sent by the browser BEFORE the `Browser.newPage` reply that told us which
|
||||
# session to build this object for, so without the replay the lifecycle waits twenty seconds for a main frame that was
|
||||
# announced before it was born.
|
||||
#
|
||||
# ⛔ AND IT DELIBERATELY DOES NOT REACH `_on_juggler_event`, which is not wired yet and cannot be: that handler emits on
|
||||
# `self.channel`, and the channel does not exist until `super().__init__` has run, which needs the main frame this replay
|
||||
# is what delivers. The events that precede a page describe its birth, and the initializer built below carries that - the
|
||||
# main frame is in it by name. (The page's own listener holds them: `_held`.)
|
||||
self.replayed_events = context.browser.replay(session, conn.dispatch_event)
|
||||
except BaseException:
|
||||
conn.remove_listener(self._route_juggler_event)
|
||||
for part in ("lifecycle", "injected"):
|
||||
try:
|
||||
getattr(self, part).detach()
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
try:
|
||||
self._build(server, context, session, conn, opener)
|
||||
except BaseException:
|
||||
@@ -2879,21 +2903,6 @@ class PageDispatcher(Dispatcher):
|
||||
context.browser.session_seed, context.browser.motion_budget_s,
|
||||
lambda: self.viewport_size,
|
||||
draws=context.browser.pointer_draws)
|
||||
# ⛔ THE EVENTS THIS PAGE ALREADY MISSED, handed over now that the two
|
||||
# things that need them exist. `Page.frameAttached` and the
|
||||
# `Runtime.executionContextCreated` pair are sent by the browser BEFORE
|
||||
# the `Browser.newPage` reply that told us which session to build this
|
||||
# object for, so without the replay the lifecycle waits twenty seconds
|
||||
# for a main frame that was announced before it was born.
|
||||
#
|
||||
# ⛔ AND IT DELIBERATELY DOES NOT REACH `_on_juggler_event`, which is
|
||||
# not wired yet and cannot be: that handler emits on `self.channel`,
|
||||
# and the channel does not exist until `super().__init__` has run,
|
||||
# which needs the main frame this replay is what delivers. The events
|
||||
# that precede a page describe its birth, and the initializer built
|
||||
# below carries that - the main frame is in it by name.
|
||||
replayed = context.browser.replay(session, conn.dispatch_event)
|
||||
self.replayed_events = replayed
|
||||
self._frames: Dict[str, Any] = {}
|
||||
#: The child frames the client was told of (`frameAttached`), to tell it of the ones born before the hook was in.
|
||||
self._told_attached: set = set()
|
||||
@@ -4773,6 +4782,9 @@ class BrowserDispatcher(Dispatcher):
|
||||
METHODS = {"newContext": "op_new_context", "close": "op_close",
|
||||
"newPage": "op_new_page"}
|
||||
|
||||
#: How long the engine's reader waits for the gate of the events before it delivers the event anyway (see `events_gate`).
|
||||
GATE_WAIT_S = 5.0
|
||||
|
||||
def __init__(self, server, browser_type: "BrowserTypeDispatcher",
|
||||
conn: Any, version: str, session_seed: Any = None,
|
||||
motion_budget_s: Any = None,
|
||||
@@ -4780,6 +4792,32 @@ class BrowserDispatcher(Dispatcher):
|
||||
layout: str = "us", pointer_draws: bool = False,
|
||||
udp_stun_servers: Optional[list] = None) -> None:
|
||||
self.conn = conn
|
||||
# ⛔ THE EVENTS OF A SESSION GO THROUGH A GATE. A new page is handed the events it missed (`replay`) by the thread that builds it,
|
||||
# while the engine's reader goes on delivering the live ones to the same subscribers: a popup's destination committed (live) in the
|
||||
# middle of the replay of its first events, and the replay then went on with older ones (the initial about:blank, the
|
||||
# `navigationStarted` of the same navigation) - the lifecycle ended at about:blank, or with the navigation pending again. The
|
||||
# reader takes the gate for each event of a session; the page takes it for the whole of "subscribe, then replay" (`events_gate`).
|
||||
self._events_gate = threading.RLock()
|
||||
self._gate_timeouts = 0
|
||||
if not getattr(conn, "_events_gate_installed", False):
|
||||
raw_dispatch = conn.dispatch_event
|
||||
|
||||
def gated_dispatch(method, params, session):
|
||||
if not session:
|
||||
return raw_dispatch(method, params, session)
|
||||
if self._events_gate.acquire(timeout=self.GATE_WAIT_S):
|
||||
try:
|
||||
return raw_dispatch(method, params, session)
|
||||
finally:
|
||||
self._events_gate.release()
|
||||
# whoever holds the gate does not let go: the event is delivered all the same (the old, unordered way) and counted
|
||||
self._gate_timeouts += 1
|
||||
return raw_dispatch(method, params, session)
|
||||
conn.dispatch_event = gated_dispatch
|
||||
try:
|
||||
conn._events_gate_installed = True
|
||||
except Exception:
|
||||
pass
|
||||
#: Where this session measures a proxy's UDP exit from (the launch's `udp_stun_servers`, "ip:port"
|
||||
#: strings; None = the two public defaults): the launch and every context proxy use the same list.
|
||||
self.udp_stun_servers = udp_stun_servers
|
||||
@@ -5035,6 +5073,13 @@ class BrowserDispatcher(Dispatcher):
|
||||
if len(held) < self.BUFFER_CAP:
|
||||
held.append((method, params))
|
||||
|
||||
@contextlib.contextmanager
|
||||
def events_gate(self):
|
||||
"""Hold the delivery of the events of every session: the engine's reader waits (at most `GATE_WAIT_S`) at its next event of a
|
||||
session. For a short step that waits for no event and no reply: a page subscribes its readers and is handed the events it missed."""
|
||||
with self._events_gate:
|
||||
yield
|
||||
|
||||
def replay(self, session: str, deliver) -> int:
|
||||
"""Hand a new consumer the events of its session that it missed.
|
||||
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
"""An event the engine sends LIVE while the events a new page missed are being replayed to it does not reach it before them.
|
||||
|
||||
NO BROWSER HERE (the real dispatchers over a scripted Juggler connection, as `test_the_hand_over_of_a_popup_holds_under_concurrency`).
|
||||
|
||||
The browser buffers the events of a session nobody reads yet, and `PageDispatcher` is handed them (`BrowserDispatcher.replay`) once its
|
||||
lifecycle and its injected script are subscribed. The replay ran on the adoption thread while the engine's reader went on delivering the
|
||||
live events to the same subscribers: a popup's destination committed (live) in the middle of the replay of its first events, and the
|
||||
replay then went on with the OLDER ones - the initial `about:blank` (no navigation of ours) and the `navigationStarted` of the same
|
||||
navigation. The lifecycle ended at `about:blank` for good (a popup "at about:blank": the client was never told of the destination), or
|
||||
had the navigation pending again after its own commit ("Execution context was destroyed" in the first evaluate). Measured on Windows with
|
||||
the engine's events traced (`POPUP-63-SECONDA-CAUSA.md`): the second `Page.frameAttached` of every popup in the traces IS the replay, and
|
||||
the engine sends each event once - 13 reds in 20 runs of the popup file on engine 63 after the hand-over fix, none of them with a second
|
||||
`frameAttached` from the engine (`PageHandler._onFrameAttached` answers nothing for a frame it holds live).
|
||||
|
||||
Sequence of the test: the first events of a popup are buffered (frame, blank commit, the destination's navigation started) -> the page is
|
||||
built and the replay starts, stopped after its first event -> the engine's reader delivers the destination's commit -> the replay goes on.
|
||||
Whatever the interleaving, the lifecycle must end at the destination, with the navigation committed and not pending, and the client's
|
||||
frame must be told the destination.
|
||||
|
||||
Known-bad input: the replay as it was -> the lifecycle ends at `about:blank` and the navigation of the destination is pending again.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from invisible_playwright._juggler import server as server_mod
|
||||
|
||||
from test_a_commit_at_any_point_of_the_hand_over_reaches_the_client_once import DEST
|
||||
from test_the_hand_over_of_a_popup_holds_under_concurrency import World, _finish
|
||||
from test_every_order_of_the_events_of_a_popup_gives_one_page_event import FRAME, SESSION, TARGET, _thread, _wait
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
def _case(stop_after):
|
||||
"""The replay is stopped after `stop_after` of its events; the destination's commit is delivered by another thread (the reader)."""
|
||||
parked, go, delivered = threading.Event(), threading.Event(), []
|
||||
with pytest.MonkeyPatch.context() as mp:
|
||||
original = server_mod.BrowserDispatcher.replay
|
||||
|
||||
def replay(self, session, deliver):
|
||||
if session != SESSION:
|
||||
return original(self, session, deliver)
|
||||
count = [0]
|
||||
|
||||
def slow(method, params, s):
|
||||
deliver(method, params, s)
|
||||
delivered.append(method)
|
||||
count[0] += 1
|
||||
if count[0] == stop_after:
|
||||
parked.set()
|
||||
go.wait(10)
|
||||
return original(self, session, slow)
|
||||
mp.setattr(server_mod.BrowserDispatcher, "replay", replay)
|
||||
# the adoption thread is held before the page is built, so that the first events of the popup are buffered when it is
|
||||
entered, begin = threading.Event(), threading.Event()
|
||||
original_build = server_mod.BrowserDispatcher._enter_building
|
||||
|
||||
def enter_building(self, *args, **kwargs):
|
||||
if threading.current_thread().name == "invpw-adopt-%s" % TARGET:
|
||||
entered.set()
|
||||
begin.wait(10)
|
||||
return original_build(self, *args, **kwargs)
|
||||
mp.setattr(server_mod.BrowserDispatcher, "_enter_building", enter_building)
|
||||
world = World()
|
||||
reader = None
|
||||
try:
|
||||
conn = world.conn
|
||||
conn.attach(world.opener_target, world.context.context_id)
|
||||
assert entered.wait(5), "the adoption never started"
|
||||
conn.frame()
|
||||
conn.commit() # the initial about:blank (navigation n1)
|
||||
conn.start() # the navigation to the destination starts (n2)
|
||||
begin.set() # the page is built now, and the replay hands it those events
|
||||
assert parked.wait(5), "the replay never delivered %d event(s): %s" % (stop_after, delivered)
|
||||
reader = threading.Thread(target=conn.commit_dest, daemon=True, name="engine-reader")
|
||||
reader.start()
|
||||
reader.join(0.3) # with the replay ordered, the reader waits behind it; unordered, it is through
|
||||
go.set()
|
||||
reader.join(5)
|
||||
assert not reader.is_alive(), "the reader never got through"
|
||||
conn.ready()
|
||||
assert _wait(lambda: _thread() is None), "the adoption thread never ended"
|
||||
page = world.browser._page_targets.get(TARGET)
|
||||
known = page.lifecycle.frames[FRAME]
|
||||
return known.url, known.navigation, sorted(known.states), page.frame.url
|
||||
finally:
|
||||
begin.set()
|
||||
go.set()
|
||||
world.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stop_after", [1, 2, 3, 4, 5])
|
||||
def test_the_destination_commit_delivered_while_the_replay_goes_on_is_the_last_word(stop_after):
|
||||
url, navigation, states, told = _case(stop_after)
|
||||
assert url == DEST, "the lifecycle ended at %r: an older event of the replay came after the live commit" % url
|
||||
assert navigation == "n2", "the frame's navigation is %r" % navigation
|
||||
assert "commit" in states
|
||||
assert told == DEST, "the client's frame is at %r" % told
|
||||
Reference in new issue
Block a user