// chrome-session.mjs — 宿主 CPU 缺陷(8173M error 25)约束下的 Playwright+系统Chrome 会话工具 // // 背景(2026-11-15 实测,详见 memory-bank/techContext): // - Playwright 自带 chromium:newPage 即 Target crashed,不可用 // - 官方 Google Chrome 157(channel:'chrome'):可用,但**同一 browser 第 2 个 newPage 必崩** // (renderer segfault error 25 @ 固定偏移 0x90b1f9,zombie 后 newPage 永久挂起) // - 稳定口径 = 每会话独立 launch + 单 page 内 goto 导航(实测 5/5 与 4/5[失败仅外网网络]) // // 用法: // node scripts/chrome-session.mjs [--shot 路径前缀] [--wait ms] // 登录参考平台等复杂场景:复制本脚本 runSession() 回调改造(env 传账号,勿硬编码) // // 环境要求:web/node_modules(playwright)、google-chrome-stable(已装 157) import { chromium } from '../web/node_modules/playwright/index.mjs'; const withTimeout = (p, ms, tag) => Promise.race([p, new Promise((_, rej) => setTimeout(() => rej(new Error(tag + ' TIMEOUT')), ms))]); const ARGS = ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage']; const launch = () => withTimeout(chromium.launch({ channel: 'chrome', args: ARGS }), 20000, 'launch'); export async function runSession({ url, onReady, shotPrefix = '/tmp/chrome-session', waitMs = 2000, retries = 2 }) { for (let attempt = 1; attempt <= retries + 1; attempt++) { let browser; try { browser = await launch(); const page = await withTimeout(browser.newPage(), 15000, 'newPage'); // 铁律:本会话唯一 page,后续一律 page.goto await withTimeout(page.goto(url, { timeout: 20000, waitUntil: 'domcontentloaded' }), 30000, 'goto'); await page.waitForTimeout(waitMs); if (onReady) await onReady(page); const title = await withTimeout(page.title(), 10000, 'title'); await withTimeout(page.screenshot({ path: `${shotPrefix}.png`, fullPage: true }), 20000, 'shot'); return { ok: true, title, url: page.url() }; } catch (e) { console.error(`[chrome-session] attempt ${attempt} FAIL: ${e.message}`); if (attempt > retries) return { ok: false, error: e.message }; await new Promise((r) => setTimeout(r, 1500)); } finally { if (browser) await browser.close().catch(() => {}); } } } // CLI 直跑:node scripts/chrome-session.mjs http://127.0.0.1:5173/ if (process.argv[1] && process.argv[1].endsWith('chrome-session.mjs')) { const url = process.argv[2] || 'http://127.0.0.1:5173/'; const r = await runSession({ url }); console.log(JSON.stringify(r)); process.exit(r.ok ? 0 : 1); }