From 11a0804a0f33de28cbdf63341ac3e1580f91de94 Mon Sep 17 00:00:00 2001 From: WJZ_P <110795301+WJZ-P@users.noreply.github.com> Date: Mon, 16 Mar 2026 02:16:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=8F=8D=E6=A3=80=E6=B5=8B=E5=8F=82=E6=95=B0=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20Windows=20=E4=B8=8B=E7=9A=84=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E8=BF=9B=E7=A8=8B=E5=88=86=E7=A6=BB=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/browser.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/browser.js b/src/browser.js index ee96113..2b820d9 100644 --- a/src/browser.js +++ b/src/browser.js @@ -255,7 +255,7 @@ const BROWSER_ARGS = [ : []), // ── 反检测(配合 stealth 插件 + ignoreDefaultArgs) ── - '--disable-blink-features=AutomationControlled', // 移除 navigator.webdriver 标记,降低被检测为自动化的风险 + //'--disable-blink-features=AutomationControlled', // 移除 navigator.webdriver 标记,降低被检测为自动化的风险。stealth已经带上了,这里额外写会造成参数错误。 // ── 网络 / 性能 ── '--disable-background-networking', // 禁止后台网络请求(更新检查、遥测等) @@ -439,12 +439,26 @@ export async function ensureBrowser(opts = {}) { /** * 断开浏览器连接(不杀进程,方便下次复用) + * + * 在 Windows 上,Node 退出时默认会终止所有子进程。 + * 因此 disconnect 前先对浏览器子进程做 unref + stdio detach, + * 使浏览器进程脱离 Node 进程树,独立存活。 */ export function disconnect() { if (_browser) { + // 解除 Node 对浏览器子进程的引用,防止 Node 退出时杀掉它 + const proc = _browser.process(); + if (proc) { + proc.unref(); + // 同时 unref 所有 stdio 流,避免 Node 因为管道未关闭而挂住 + if (proc.stdin) proc.stdin.unref(); + if (proc.stdout) proc.stdout.unref(); + if (proc.stderr) proc.stderr.unref(); + } + _browser.disconnect(); _browser = null; - console.log('[browser] disconnected'); + console.log('[browser] disconnected (browser process kept alive)'); } }