The download of a tab that is not announced yet is said after the page event of the tab
The tab opened for an attachment is announced when the engine takes it away, a few milliseconds after it says the download; the download
(reported on the opener) went to the client first, the popup after: the order the other way round from Playwright's, and a client that read
the popup the moment the download arrived found none on a one-CPU machine (the 4 download cases of GitHub, 3 of 4 red; the same on a one-CPU
container: 1, 0, 2 of 50 on a90dadc). The adoption holds the download on its record and says it right after the page event (never lost: also
when the tab is dropped or fails).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a90dadc204
commit
65f7580182
2 files changed
+96
-57
No files matched your search
@@ -2579,7 +2579,7 @@ class _Adoption:
|
||||
the build, between the build and the wait, and a command answered 'cannot find session').
|
||||
"""
|
||||
|
||||
__slots__ = ("target_id", "opener", "context_id", "requested", "state", "gone", "page", "session", "trace", "t0")
|
||||
__slots__ = ("target_id", "opener", "context_id", "requested", "state", "gone", "page", "session", "trace", "t0", "downloads")
|
||||
|
||||
#: What the engine says about a target that the trace keeps (the events of its page's birth and of its disappearance).
|
||||
TRACED = ("Page.frameAttached", "Page.navigationStarted", "Page.navigationCommitted", "Page.navigationAborted",
|
||||
@@ -2597,6 +2597,9 @@ class _Adoption:
|
||||
#: of the events is what decides the outcome of an adoption).
|
||||
self.t0 = time.monotonic()
|
||||
self.trace: List[str] = ["attached +0ms"]
|
||||
#: The `download` events for the opener of a tab not announced yet, in order: said right after the `page` event of the tab, as
|
||||
#: Playwright's server says them (the popup is reported, then the download of the opener).
|
||||
self.downloads: List = []
|
||||
self.requested: Optional[bool] = None
|
||||
self.state = "attached"
|
||||
self.gone = False
|
||||
@@ -4913,6 +4916,18 @@ class BrowserDispatcher(Dispatcher):
|
||||
self._pages_ready.notify_all()
|
||||
return gone
|
||||
|
||||
def _flush_downloads(self, adoption: Optional["_Adoption"]) -> None:
|
||||
"""Say the downloads that waited for the tab to be announced (or to be dropped: they are never lost)."""
|
||||
if adoption is None:
|
||||
return
|
||||
with self._targets_lock:
|
||||
pending, adoption.downloads = adoption.downloads, []
|
||||
for origin, payload in pending:
|
||||
try:
|
||||
origin.emit("download", payload)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _adopt_target(self, target_id: str, session_id: Optional[str],
|
||||
browser_context_id: Optional[str],
|
||||
opener: Optional[PageDispatcher],
|
||||
@@ -4960,6 +4975,7 @@ class BrowserDispatcher(Dispatcher):
|
||||
self._dropped.append((target_id, adoption.trace_text()))
|
||||
self._adoptions.pop(target_id, None)
|
||||
self._pages_ready.notify_all()
|
||||
self._flush_downloads(adoption)
|
||||
return
|
||||
# ⛔ THE EVENTS THAT COME WITH THE READY ARE APPLIED FIRST. The engine says Page.ready and, in the same turn, the commit of
|
||||
# the document that made the page ready: announced between the two, the page tells its client `about:blank` (3 of 4 popups
|
||||
@@ -4968,7 +4984,9 @@ class BrowserDispatcher(Dispatcher):
|
||||
page.settle_events()
|
||||
# ⛔ `page` BEFORE the registration, on purpose: the client always sees the page before its close, never a close for a page it
|
||||
# has not met.
|
||||
if self._enter_announced(adoption, page, context):
|
||||
gone_at_announce = self._enter_announced(adoption, page, context)
|
||||
self._flush_downloads(adoption)
|
||||
if gone_at_announce:
|
||||
page.announce_closed()
|
||||
except Exception as failure:
|
||||
if page is not None:
|
||||
@@ -4977,6 +4995,7 @@ class BrowserDispatcher(Dispatcher):
|
||||
page.announce_closed()
|
||||
except Exception:
|
||||
pass
|
||||
self._flush_downloads(adoption)
|
||||
with self._targets_lock:
|
||||
trace = adoption.trace_text() if adoption is not None else "no record of the target"
|
||||
self._dropped.append((target_id, trace))
|
||||
@@ -5408,9 +5427,21 @@ class BrowserDispatcher(Dispatcher):
|
||||
"frameId": frame.id, "navigationId": frame.navigation,
|
||||
"errorText": "Download is starting"})
|
||||
if origin is not None:
|
||||
origin.emit("download", {"url": params.get("url") or "",
|
||||
"suggestedFilename": params.get("suggestedFileName") or "",
|
||||
"artifact": artifact.channel})
|
||||
payload = {"url": params.get("url") or "",
|
||||
"suggestedFilename": params.get("suggestedFileName") or "",
|
||||
"artifact": artifact.channel}
|
||||
deferred = False
|
||||
with self._targets_lock:
|
||||
# ⛔ AFTER THE `page` EVENT OF THE TAB, NOT BEFORE IT. The download of a tab that is not announced yet is said on its
|
||||
# opener, and the tab is announced a few milliseconds after the engine says the download (the adoption thread announces
|
||||
# it when the tab goes away): said at once, the client got the download first and the popup after, the order the
|
||||
# other way round from Playwright's, and a client that reads the popup the moment the download arrives found none
|
||||
# on a loaded machine.
|
||||
if announced is None and adoption is not None and self._adoptions.get(target) is adoption:
|
||||
adoption.downloads.append((origin, payload))
|
||||
deferred = True
|
||||
if not deferred:
|
||||
origin.emit("download", payload)
|
||||
if early is not None:
|
||||
artifact.report_finished(early)
|
||||
|
||||
|
||||
@@ -305,19 +305,22 @@ def test_a_popup_is_announced_with_the_address_of_the_commit_that_came_with_its_
|
||||
assert urls == [url], "the popup was announced with %r, not with the address of the commit that came with its ready" % (urls,)
|
||||
|
||||
|
||||
def test_a_download_started_by_a_popup_that_is_not_announced_is_reported_on_its_opener(late_browser):
|
||||
"""`<a target=_blank href=file>` with `Content-Disposition: attachment`: the new tab never gets a Page.ready and the engine closes it
|
||||
at once. Playwright reports the download on the OPENER (ffBrowser._onDownloadCreated); looking only among the announced pages
|
||||
lost it (no `download` event anywhere)."""
|
||||
def test_a_download_started_by_a_popup_that_is_not_announced_is_reported_on_its_opener_after_the_popup(late_browser):
|
||||
"""`<a target=_blank href=file>` with `Content-Disposition: attachment`: the new tab never gets a Page.ready and the engine closes it at
|
||||
once. Playwright reports the download on the OPENER (ffBrowser._onDownloadCreated) and the popup before it; looking only among the
|
||||
announced pages lost the download, and saying it at once put it before the popup (a client that read the popup the moment the
|
||||
download arrived found none on a loaded machine)."""
|
||||
server, conn = late_browser.server, late_browser.conn
|
||||
context = _new_context(late_browser)
|
||||
conn.released = {"s1"}
|
||||
server.handle({"guid": context.guid, "method": "newPage", "params": {"pageNumber": 1}})
|
||||
opener_target = next(iter(conn.sessions))
|
||||
opener = next(o for o in server._objects.values() if o.TYPE == "Page" and o.target_id == opener_target)
|
||||
said = []
|
||||
original = opener.emit
|
||||
opener.emit = lambda event, params=None: (said.append((event, params)), original(event, params))[1]
|
||||
order = []
|
||||
original_opener = opener.emit
|
||||
opener.emit = lambda event, params=None: (order.append(event), original_opener(event, params))[1]
|
||||
original_context = context.emit
|
||||
context.emit = lambda event, params=None: (order.append("context:" + event), original_context(event, params))[1]
|
||||
target, session = conn.popup_born(opener_target, context.context_id, 1)
|
||||
deadline = time.monotonic() + 5
|
||||
while not (target in late_browser._adoptions and late_browser._adoptions[target].state == "waiting") and time.monotonic() < deadline:
|
||||
@@ -325,9 +328,14 @@ def test_a_download_started_by_a_popup_that_is_not_announced_is_reported_on_its_
|
||||
conn.dispatch_event("Browser.downloadCreated", {
|
||||
"uuid": "u1", "browserContextId": context.context_id, "pageTargetId": target, "frameId": "f" + target,
|
||||
"url": "http://127.0.0.1/file", "suggestedFileName": "f.bin"}, None)
|
||||
downloads = [p for e, p in said if e == "download"]
|
||||
assert len(downloads) == 1, "the download of the popup was not reported on its opener: %r" % (said,)
|
||||
assert downloads[0]["suggestedFilename"] == "f.bin"
|
||||
assert "download" not in order, "the download was said before the popup it came from: %r" % (order,)
|
||||
conn.dispatch_event("Browser.detachedFromTarget", {"targetId": target, "sessionId": session}, None) # the tab is closed
|
||||
deadline = time.monotonic() + 5
|
||||
while "download" not in order and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
assert "download" in order, "the download of the popup was not reported on its opener: %r" % (order,)
|
||||
assert order.index("context:page") < order.index("download"), "the download came before the popup: %r" % (order,)
|
||||
|
||||
|
||||
|
||||
def test_the_load_of_a_popup_said_before_its_announcement_is_said_again_right_after_it(late_browser, monkeypatch):
|
||||
@@ -440,45 +448,45 @@ def test_a_command_of_the_adoption_that_finds_the_session_gone_announces_and_clo
|
||||
time.sleep(0.2)
|
||||
assert "page" in announced, "the popup whose session was gone was discarded instead of announced"
|
||||
assert not [w for w in caught if "adoption" in str(w.message)], [str(w.message) for w in caught]
|
||||
|
||||
|
||||
def test_a_popup_that_is_dropped_or_fails_leaves_the_trace_of_what_the_engine_said(late_browser, monkeypatch):
|
||||
"""The log of a red run on a machine nobody can look at does not say in which order the events came: the adoption keeps a short trace
|
||||
per target and writes it in the warning of a failed adoption and in the record of the dropped ones."""
|
||||
import warnings
|
||||
from invisible_playwright._juggler.server import PageDispatcher
|
||||
server, conn = late_browser.server, late_browser.conn
|
||||
context = _new_context(late_browser)
|
||||
conn.released = {"s1"}
|
||||
server.handle({"guid": context.guid, "method": "newPage", "params": {"pageNumber": 1}})
|
||||
opener_target = next(iter(conn.sessions))
|
||||
# 1. dropped with its context
|
||||
target, session = conn.popup_born(opener_target, context.context_id, 1)
|
||||
deadline = time.monotonic() + 5
|
||||
while not (target in late_browser._adoptions and late_browser._adoptions[target].state == "waiting") and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
server.handle({"guid": context.guid, "method": "close", "params": {}})
|
||||
deadline = time.monotonic() + 5
|
||||
while not late_browser._dropped and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
assert late_browser._dropped, "a popup dropped with its context left no trace"
|
||||
dropped_target, text = late_browser._dropped[-1]
|
||||
assert dropped_target == target
|
||||
for word in ("attached", "frameAttached", "building", "waiting", "dropped: its context closed"):
|
||||
assert word in text, "the trace of the dropped popup lacks %r: %s" % (word, text)
|
||||
# 2. a failed adoption writes it in its warning
|
||||
context2 = _new_context(late_browser)
|
||||
|
||||
def boom(self, *args, **kwargs):
|
||||
raise ValueError("boom")
|
||||
|
||||
conn.released = {"s1", "s2"}
|
||||
monkeypatch.setattr(PageDispatcher, "__init__", boom)
|
||||
with warnings.catch_warnings(record=True) as caught:
|
||||
warnings.simplefilter("always")
|
||||
conn.popup_born(opener_target, context2.context_id, 2)
|
||||
deadline = time.monotonic() + 5
|
||||
while not [w for w in caught if "adoption" in str(w.message)] and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
messages = [str(w.message) for w in caught if "adoption" in str(w.message)]
|
||||
assert messages and "events of the target" in messages[0] and "attached" in messages[0], messages
|
||||
|
||||
|
||||
def test_a_popup_that_is_dropped_or_fails_leaves_the_trace_of_what_the_engine_said(late_browser, monkeypatch):
|
||||
"""The log of a red run on a machine nobody can look at does not say in which order the events came: the adoption keeps a short trace
|
||||
per target and writes it in the warning of a failed adoption and in the record of the dropped ones."""
|
||||
import warnings
|
||||
from invisible_playwright._juggler.server import PageDispatcher
|
||||
server, conn = late_browser.server, late_browser.conn
|
||||
context = _new_context(late_browser)
|
||||
conn.released = {"s1"}
|
||||
server.handle({"guid": context.guid, "method": "newPage", "params": {"pageNumber": 1}})
|
||||
opener_target = next(iter(conn.sessions))
|
||||
# 1. dropped with its context
|
||||
target, session = conn.popup_born(opener_target, context.context_id, 1)
|
||||
deadline = time.monotonic() + 5
|
||||
while not (target in late_browser._adoptions and late_browser._adoptions[target].state == "waiting") and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
server.handle({"guid": context.guid, "method": "close", "params": {}})
|
||||
deadline = time.monotonic() + 5
|
||||
while not late_browser._dropped and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
assert late_browser._dropped, "a popup dropped with its context left no trace"
|
||||
dropped_target, text = late_browser._dropped[-1]
|
||||
assert dropped_target == target
|
||||
for word in ("attached", "frameAttached", "building", "waiting", "dropped: its context closed"):
|
||||
assert word in text, "the trace of the dropped popup lacks %r: %s" % (word, text)
|
||||
# 2. a failed adoption writes it in its warning
|
||||
context2 = _new_context(late_browser)
|
||||
|
||||
def boom(self, *args, **kwargs):
|
||||
raise ValueError("boom")
|
||||
|
||||
conn.released = {"s1", "s2"}
|
||||
monkeypatch.setattr(PageDispatcher, "__init__", boom)
|
||||
with warnings.catch_warnings(record=True) as caught:
|
||||
warnings.simplefilter("always")
|
||||
conn.popup_born(opener_target, context2.context_id, 2)
|
||||
deadline = time.monotonic() + 5
|
||||
while not [w for w in caught if "adoption" in str(w.message)] and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
messages = [str(w.message) for w in caught if "adoption" in str(w.message)]
|
||||
assert messages and "events of the target" in messages[0] and "attached" in messages[0], messages
|
||||
Reference in new issue
Block a user