← Commit history

Fix in-window tab adoption: identify the window.open tab by a unique per-call window.name marker instead of set-difference, which under a burst of adds could adopt the wrong new target and duplicate tabs

John Lauer ·8abe88ba64 ·3mo ago ·parent 7fcb237
4 files changed +39−23
adom-bridge-puppeteer-manifest.json+4−4
@@ -1,10 +1,10 @@ {   "manifest_version": 1,   "name": "puppeteer",-  "version": "1.9.294",-  "url": "https://wiki.adom.inc/download/adom/adom-desktop-puppeteer-bridge/1.9.294/adom-bridge-puppeteer-v1.9.294.zip",-  "sha256": "d0d18f8d32b84ea3d0469172062775eaceb2eb7e9b0bc02a7443555e0f04c532",-  "size": 720138,+  "version": "1.9.295",+  "url": "https://wiki.adom.inc/download/adom/adom-desktop-puppeteer-bridge/1.9.295/adom-bridge-puppeteer-v1.9.295.zip",+  "sha256": "fc4cf70cfe17bbf2518e0a85923e584e1a1db362f5341729f1c829263ced6dd2",+  "size": 720542,   "verbPrefixes": [     "browser_",     "desktop_recorder_",
src/BRIDGE_VERSION+1−1
@@ -1 +1 @@-1.9.294+1.9.295
src/bridge.json+1−1
@@ -2,7 +2,7 @@   "manifest_version": 1,   "name": "puppeteer",   "displayName": "Puppeteer (Chrome for Testing + Native)",-  "version": "1.9.294",+  "version": "1.9.295",   "description": "Drives a browser ALREADY on the machine \u2014 installed Chrome, else Microsoft Edge (every Windows PC ships with Edge) \u2014 launched with a FRESH isolated profile, so the common case has NO ~150 MB Chrome-for-Testing download and it never touches the user's real tabs/logins. Binds its local server to LOOPBACK (127.0.0.1) only, so no Windows Firewall 'allow Node through the firewall' prompt ever pops. Chrome for Testing is fetched only as a LAST RESORT if the machine has no Chromium browser at all. SPAWN-VERIFIED fallback: pup tries each detected browser (Chrome \u2192 Edge \u2192 CfT) and actually launches it, falling through to the next if one won't start (and handling browsers like Edge that relaunch onto another PID), so a corrupt/partial install never hard-fails an open. The browser that launches is cached as the default (persisted across restarts) so it isn't re-probed each time. On spawn the bridge SELF-WARMS: detects browsers + disk, and only if the box has no Chrome/Edge and no cached CfT does it background-fetch CfT so the first open is instant. browser_use pins a specific browser (chrome | edge | cft | auto); {browser:'chrome',install:true} downloads & installs REAL Google Chrome (user-scoped, no admin) on an Edge-only box. browser_readiness (READ-ONLY) reports which browser will be used + the full candidate list + the default + free disk. Every failure returns a specific errorCode + _hint (chrome_install_no_disk, chrome_download_network, chrome_install_permission, chrome_stable_not_detected, \u2026). Launches with FULL browser permissions by default for full debug (strictPermissions:true opts out). Headful automation: open/close windows+tabs, navigate, screenshot, eval JS, record. pup is a GENERIC, non-profile browser only \u2014 every session is a fresh isolated Chrome/Edge process, never the user's real signed-in browser; profile defaults to sessionId. To drive the user's REAL Chrome/Edge (their profile, cookies, SSO), use the Adom browser EXTENSION's nbrowser_* verbs \u2014 a separate bridge, not pup's job. macOS (v1.9.74+): full parity with mac-native mechanics \u2014 background-by-default is the app-level hide (windows stay drivable/screenshottable while hidden), raise/lower map to unhide+activate/hide, browser_alert_window posts a native toast whose click focuses the window, browser_use {browser:'chrome',install:true} installs Chrome from the official dmg, and taskbar identity/badging remains Windows-only.",   "homepage": "https://wiki.adom.inc/adom/adom-desktop",   "author": "Adom Inc.",
src/server.js+33−17
@@ -311,6 +311,7 @@ const _launchInFlight = new Map(); // run in arrival order, each completing create+cleanup before the next starts. Correctness first: // a fleet of AI threads opening default windows simultaneously is the 10,000-user case. const _profileOpenChain = new Map(); // profileName -> tail promise+let _tabClaimSeq = 0; // monotonic per-call marker for in-window tab adoption (see addTabToSession) // v1.9.110 (issue #22, AdityaAngajala) — ADMISSION-TIME OWNERSHIP CLAIM. // Two defects he measured: (a) `owner` is optional, so in practice sessions end up unowned and the guard // that protects a busy window is opt-out by omission; (b) ownership was established when the BROWSER was@@ -7525,29 +7526,44 @@ async function addTabToSession(sessionId, url) {     if (anchor && anchor.page && !anchor._crashed) {       let cdp = null;       try {-        // v1.9.294 (John: "why do i keep getting so many about:blank tabs?"): adopt the tab-        // window.open() creates by TARGET SET-DIFFERENCE, not by opener match. The old opener-        // match (waitForTarget on tg.opener() === anchor) silently failed whenever the anchor was-        // mid-navigation/cross-origin at the instant window.open fired — the opener link isn't-        // observable then, waitForTarget timed out, and we fell through to newPage(). But-        // window.open had ALREADY made a blank tab, so we ended up with TWO tabs: the abandoned-        // "about:blank" (the stray John kept seeing) and the real newPage() tab. Snapshotting the-        // target set before the open and taking whatever new page target appears can't miss the-        // tab and can't orphan it — we always adopt the exact tab we just made.+        // v1.9.295 (John: "why do i keep getting so many about:blank tabs?"): adopt EXACTLY the tab+        // window.open() creates, identified by a UNIQUE per-call marker written into window.name.+        //+        // History of why both simpler schemes failed:+        //  - Opener match (waitForTarget on tg.opener() === anchor): window.open() blanks expose NO+        //    observable opener here (confirmed in the bridge log: "new tab ... with no opener"), so+        //    the match ALWAYS timed out and fell through to newPage() — orphaning the blank+        //    window.open had already made. That stray "about:blank" beside the real tab is what John+        //    kept seeing.+        //  - Plain set-difference (take any new page target): correct for a single add, but under a+        //    BURST several sessions' blanks are new at once, so calls raced to grab the first new+        //    target and duplicated tabs (a burst of 3 adds tripled the middle one in testing).+        //+        // The marker is unique per call, so it can only ever match this call's own tab — burst-safe.+        // window.name survives navigation (even cross-origin), and about:blank is same-origin to the+        // opener so the write always lands. We adopt while still about:blank, before goto().+        const tok = 'pupclaim_' + (_tabClaimSeq = (_tabClaimSeq + 1) % 1e9) + '_' + Date.now().toString(36);         const before = new Set(be.browser.targets());         cdp = await anchor.page.target().createCDPSession();-        await cdp.send('Runtime.evaluate', { expression: 'window.open("about:blank")', userGesture: true });+        await cdp.send('Runtime.evaluate', {+          expression: '(function(){var w=window.open("about:blank");if(w){try{w.name=' + JSON.stringify(tok) + ';}catch(e){}}return !!w;})()',+          userGesture: true });         const start = Date.now();         let pg = null;-        while (Date.now() - start < 5000) {-          const fresh = be.browser.targets().find(t => { try { return !before.has(t) && t.type() === 'page'; } catch (e) { return false; } });-          if (fresh) { try { pg = await fresh.page(); } catch (e) {} if (pg) break; }-          await new Promise(r => setTimeout(r, 50));+        while (Date.now() - start < 5000 && !pg) {+          // Only inspect NEW page targets (set-diff narrows the field), then CONFIRM ours by marker.+          const fresh = be.browser.targets().filter(t => { try { return !before.has(t) && t.type() === 'page'; } catch (e) { return false; } });+          for (const t of fresh) {+            let p = null; try { p = await t.page(); } catch (e) {}+            if (!p) continue;+            let nm = null; try { nm = await p.evaluate(() => window.name); } catch (e) { /* mid-nav context torn down; retry next poll */ }+            if (nm === tok) { pg = p; break; }+          }+          if (!pg) await new Promise(r => setTimeout(r, 60));         }         if (pg) return pg;-        // No new target appeared → window.open was blocked/failed and created NOTHING, so the-        // newPage() fallback below can't orphan a blank. (If it had created one, set-difference-        // above would have adopted it.)+        // No marked tab appeared → window.open was blocked/failed and created NOTHING, so the+        // newPage() fallback below can't orphan a blank.       } catch (e) { console.log(`[tab] in-window open failed (${e.message}) — falling back to newPage`); }       finally { try { if (cdp) await cdp.detach(); } catch (e) {} }     }