Solve init script race with Debugger.resume at frame init script injection time#1719
Solve init script race with Debugger.resume at frame init script injection time#1719
Conversation
|
5d411d6 to
351782b
Compare
Greptile SummaryThis PR fixes a race condition in init script injection by ensuring all CDP commands are queued and sent before resuming the debugger. Previously, Key changes:
Architecture insight: Minor observation: Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Browser
participant Context as V3Context
participant Session as CDPSession
participant Page
Browser->>Context: Target.attachedToTarget
Context->>Session: Queue Page.enable
Context->>Session: Queue Runtime.enable
Context->>Session: Queue Target.setAutoAttach
Context->>Session: Queue Network.enable (if headers)
Context->>Session: Queue Page.addScriptToEvaluateOnNewDocument (init scripts)
Context->>Session: Queue Page.addScriptToEvaluateOnNewDocument (piercer)
Context->>Session: Queue Runtime.runIfWaitingForDebugger
Note over Context,Session: All commands queued before resume
Session-->>Context: Await all promises together
alt resumeOk
Context->>Context: Set resumed = true
Context->>Session: Page.setLifecycleEventsEnabled
Context->>Page: Page.create()
Page->>Session: Page.enable (void, best-effort)
Page->>Session: Page.setLifecycleEventsEnabled (void)
Page-->>Context: Page instance
Context->>Context: Wire session to page
Context->>Context: Install frame bridges
alt piercer pre-registered
Context->>Context: Mark piercer installed
else piercer failed
Context->>Session: ensurePiercer() (void, lazy)
end
else !resumeOk
Context->>Context: Log error and return
end
Last reviewed commit: ef94203 |
There was a problem hiding this comment.
No issues found across 4 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant CDP as Browser (CDP)
participant Conn as CdpConnection
participant Ctx as V3Context
participant Sess as Target Session
participant P as Page Object
Note over CDP, P: Target Creation (New Page/Popup/OOPIF)
CDP->>Conn: Target.attachedToTarget (Paused)
Conn->>Ctx: onAttachedToTarget()
Note over Ctx, Sess: NEW: Batch Setup (Pre-Resume Queue)
Ctx->>Sess: 1. Page.enable / Runtime.enable
Ctx->>Sess: 2. Target.setAutoAttach (waitForDebuggerOnStart)
opt Init Scripts defined
Ctx->>Sess: 3. Page.addScriptToEvaluateOnNewDocument
end
Ctx->>Sess: 4. Register Piercer (Shadow DOM Hook)
Note over Ctx, Sess: CHANGED: Resume queued as last operation in batch
Ctx->>Sess: 5. Runtime.runIfWaitingForDebugger (Resume)
Ctx->>Ctx: await Promise.all([setup_ops, resume_op])
Sess-->>Ctx: All commands ACK'd (Execution resumed)
alt isTopLevelPage
Ctx->>P: Page.create()
P->>Sess: Page.enable / Lifecycle.enable
P-->>Ctx: pageInstance
Ctx->>Ctx: wireSessionToOwnerPage()
Ctx->>Ctx: CHANGED: applyInitScriptsToPage (seedOnly=true)
else isChildFrame (OOPIF/Iframe)
Ctx->>Sess: Page.getFrameTree
Sess-->>Ctx: frameTree
Ctx->>P: installFrameEventBridges()
end
Note over CDP, Conn: NEW: Event Fan-out Logic
CDP->>Sess: Target.* event (Child Session)
Sess->>Conn: dispatch(method, params)
Conn->>Conn: NEW: Forward Target.* events to root listeners
Conn-->>Ctx: Emit unified Target event
There was a problem hiding this comment.
1 issue found across 1 file (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/core/lib/v3/tests/iframe-ctx-addInitScript-race.spec.ts">
<violation number="1" location="packages/core/lib/v3/tests/iframe-ctx-addInitScript-race.spec.ts:15">
P2: The monkey-patching of `_sendViaSession` is a no-op by default because `INIT_SCRIPT_DELAY_MS` defaults to `0`. The `if (INIT_SCRIPT_DELAY_MS > 0 && ...)` guard will never be true, so this test doesn't actually reproduce the race condition it claims to test unless the env var `IFRAME_INIT_SCRIPT_SEND_DELAY_MS` is explicitly set. Consider giving it a non-zero default (e.g., `50` or `100`) so the test exercises the delayed code path by default, or add a comment explaining that the delay is opt-in.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
packages/core/tests/integration/iframe-ctx-addInitScript-race.spec.ts
Outdated
Show resolved
Hide resolved
a2025e9 to
a2712b1
Compare
fix race repro test init script fixes
982808d to
ee8b0d8
Compare
There was a problem hiding this comment.
1 issue found across 3 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/core/lib/v3/understudy/cdp.ts">
<violation number="1" location="packages/core/lib/v3/understudy/cdp.ts:270">
P2: Custom agent: **Exception and error message sanitization**
This uses a generic `new Error()` instead of a typed error class. The rule requires all exceptions raised to users to be individually typed, never generic `new Error()`. Use `CdpConnectionClosedError` (or introduce a new `CdpSessionDetachedError`) to match the project's error hierarchy. Note that two lines above (line 264, pre-existing) has the same issue — consider fixing both together.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
why
Init script injection was racing with Debugger.resume() sometimes, causing frames to load without init scripts running sometimes. This led to flaky init script tests, which were legitimately catching the issue.
what changed
https://deepwiki.com/search/how-does-playwright-guarantee_8cf2339b-c060-4cfc-bc62-f3baaf57b229?mode=deep
test plan
Summary by cubic
Fixes the init‑script race by guaranteeing pre‑resume setup and correcting popup attach order. Init scripts now run reliably in same‑ and cross‑process popups, OOPIF iframes, and across reloads; race tests verify addScript is sent before resume per session.
Written for commit 6f464d3. Summary will update on new commits. Review in cubic