From 1bdf2399701ddea298adac8d0fad52314251ca7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AB=E6=88=91=E5=B0=8F=E6=9D=A8=E5=90=8C=E5=AD=A6?= <188421097+YuJunZhiXue@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:33:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9B=9E=E6=94=B6=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=E9=98=B2=20PID=20=E5=A4=8D=E7=94=A8=E8=AF=AF=E6=9D=80=E5=B9=B6?= =?UTF-8?q?=E7=AD=89=E5=BE=85=E7=AB=AF=E5=8F=A3=E9=87=8A=E6=94=BE=EF=BC=8C?= =?UTF-8?q?=E7=89=88=E6=9C=AC=201.1.86=EF=BC=88#197/#198=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 合并 #197/#198 后加固:记录 pid 命令行不可读时必须在监听端口上才杀;杀不掉保留记录下次重试;taskkill 后等待进程退出再绑端口。桌面换包顺序与失败兜底随 PR #198 一并发布。 --- lib/web-preflight-entry.js | 46 ++++++++++++++++++++++++++++++++------ lib/web-reclaim.js | 39 +++++++++++++++++++++++++++----- package.json | 2 +- release-notes.md | 28 +++++++++++++++++++++++ test/web-reclaim.test.js | 16 +++++++++++++ 5 files changed, 117 insertions(+), 14 deletions(-) diff --git a/lib/web-preflight-entry.js b/lib/web-preflight-entry.js index 4852392..d518a26 100644 --- a/lib/web-preflight-entry.js +++ b/lib/web-preflight-entry.js @@ -85,19 +85,31 @@ function main() { try { let killed = 0; + const killedPids = []; let rec = null; try { rec = JSON.parse(fs.readFileSync(record, "utf8")); } catch { rec = null; } - if (rec && rec.pid && alive(rec.pid)) { - const cmd = cmdlineOf(rec.pid); - if (!cmd || isDshWeb(cmd)) { - if (kill(rec.pid)) killed += 1; + const listeners = listeningPids(port); + const recPid = Number(rec && rec.pid) || 0; + let recStillAlive = false; + if (recPid && alive(recPid)) { + const cmd = cmdlineOf(recPid); + // 命令行拿不到时,必须证明这个 pid 仍占着端口才杀:记录可能早于进程退出, + // pid 被系统复用后盲目 taskkill 会杀掉无关进程。 + const ownsPort = listeners.includes(recPid); + if (isDshWeb(cmd) || (!cmd && ownsPort)) { + if (kill(recPid)) { + killed += 1; + killedPids.push(recPid); + } } + recStillAlive = alive(recPid); } - if (rec) { + // 杀不掉就留着记录,下一次启动再试;只有确认进程已经不在才清记录。 + if (rec && !recStillAlive) { try { fs.unlinkSync(record); } catch { @@ -106,13 +118,20 @@ function main() { } if (!killed) { - for (const pid of listeningPids(port)) { + for (const pid of listeners) { if (pid === process.pid) continue; if (!isDshWeb(cmdlineOf(pid))) continue; - if (kill(pid)) killed += 1; + if (kill(pid)) { + killed += 1; + killedPids.push(pid); + } } } + // taskkill 返回不代表端口已经释放。等被杀进程真正退出再让 Cordis 绑端口, + // 否则还是会撞 EADDRINUSE。 + waitGone(killedPids); + if (killed) { process.stderr.write(`[dsh-purge] reclaimed stale dsh web on :${port}, retrying boot\n`); } @@ -121,4 +140,17 @@ function main() { } } +function waitGone(pids, timeoutMs = 1500) { + if (!pids.length) return; + const deadline = Date.now() + timeoutMs; + const sleeper = new Int32Array(new SharedArrayBuffer(4)); + while (Date.now() < deadline && pids.some((pid) => alive(pid))) { + try { + Atomics.wait(sleeper, 0, 0, 80); + } catch { + break; + } + } +} + main(); diff --git a/lib/web-reclaim.js b/lib/web-reclaim.js index e0c305d..c630e79 100644 --- a/lib/web-reclaim.js +++ b/lib/web-reclaim.js @@ -167,22 +167,49 @@ export function reclaimStaleWebServer({ port, dshHome, logger } = {}) { const killed = []; const rec = readRecord(home, p); + const listeners = listenerPids(p); + let recStillAlive = false; if (rec && pidAlive(rec.pid)) { const cmd = cmdlineOf(rec.pid); - // 拿不到命令行时按记录处理(pid 可能被系统复用,能拿到就必须校验) - if (!cmd || looksLikeDshWeb(cmd)) { + // 命令行拿不到时,必须证明这个 pid 仍占着端口才杀:记录早于进程退出时, + // pid 被系统复用后盲目 kill 会打到无关进程。 + const ownsPort = listeners.includes(rec.pid); + if (looksLikeDshWeb(cmd) || (!cmd && ownsPort)) { if (killPid(rec.pid)) killed.push(rec.pid); } + recStillAlive = pidAlive(rec.pid); + } + // 杀不掉就保留记录,下一次启动再试。 + if (rec && !recStillAlive) clearRecord(home, p); + if (killed.length) { + waitGone(killed); + return { killed, source: "record" }; } - if (rec) clearRecord(home, p); - if (killed.length) return { killed, source: "record" }; - for (const pid of listenerPids(p)) { + for (const pid of listeners) { if (pid === process.pid) continue; const cmd = cmdlineOf(pid); if (!looksLikeDshWeb(cmd)) continue; if (killPid(pid)) killed.push(pid); } - if (killed.length) log(`[dsh-purge] reclaimed stale web server on :${p} (pid ${killed.join(",")})`); + if (killed.length) { + waitGone(killed); + log(`[dsh-purge] reclaimed stale web server on :${p} (pid ${killed.join(",")})`); + } return { killed, source: killed.length ? "port" : "" }; } + +/** taskkill / SIGKILL 返回不代表端口已释放;绑端口前等一下进程真正退出。 */ +export function waitGone(pids, timeoutMs = 1500) { + const list = Array.isArray(pids) ? pids.filter((pid) => Number(pid) > 0) : []; + if (!list.length) return; + const deadline = Date.now() + timeoutMs; + const sleeper = new Int32Array(new SharedArrayBuffer(4)); + while (Date.now() < deadline && list.some((pid) => pidAlive(pid))) { + try { + Atomics.wait(sleeper, 0, 0, 80); + } catch { + break; + } + } +} diff --git a/package.json b/package.json index deb88e1..37ac0b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-purge", - "version": "1.1.85", + "version": "1.1.86", "description": "DeepSeek Harness 破甲:让所有模型都能破甲,不同模型可换不同提示词;默认提示词面向国模「小码酱」。Jailbreak for every model — swap prompts per model. 求 Star 收藏 ⭐ 如要二开,请查看署名要求部分", "type": "module", "main": "lib/index.js", diff --git a/release-notes.md b/release-notes.md index 3b6255e..0461b01 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,3 +1,31 @@ +# 1.1.86 + +## 中文 + +- 版本 **1.1.86**。 +- **Web「应用 → 自动重启」后残留 server 导致 EADDRINUSE 起不来(#197,合并 PR #197)**: + - `restart-web.js` 把后台 server 的 pid/url 记到 `$DSH_HOME/dsh-purge/web-server-.json`;`killOldServer` 增加按端口兜底,清掉旧版本留下、没有记录的孤儿。 + - 新增 `lib/bin-preflight.js`:把回收钩子注入宿主 `bin.js` 顶部(跨平台),在 Cordis 激活、webserver 绑端口之前先回收残留;只杀命令行命中 `@deepseek-ai/dsh/lib/bin.js … web` 的进程。 + - 重启时打印「后台运行、关窗不会停」,避免把提示符返回误判成程序已退出。 + - 加固:记录里的 pid 拿不到命令行时,必须仍占着该端口才允许杀,防 PID 复用误杀;杀不掉就保留记录下次再试;杀完等待进程真正退出再绑端口。 +- **桌面端点「应用」换包顺序(#198,合并 PR #198)**: + - 先按安装根路径且不含 `--type=` 识别并杀掉 Electron main,再扫安装目录其余进程,避免 host 先死、main 还活着弹「host stopped」。 + - 挪包失败(asar 仍被占用)时补上 `Start-Process`,把原版应用拉起来并写入 `%TEMP%\dsh-purge-desktop.log`,不再留空桌面。 +- 测试:96/96 通过。 + +## English + +- **1.1.86**. +- **Stale web server after "Apply → auto restart" caused EADDRINUSE on boot (#197, merged PR #197)**: + - `restart-web.js` records the background server pid/url in `$DSH_HOME/dsh-purge/web-server-.json`; `killOldServer` now also reclaims by port, catching orphans left by older versions with no record. + - New `lib/bin-preflight.js` injects a reclaim hook at the top of the host `bin.js` (cross-platform) so it runs before Cordis activates and before the webserver binds; only processes whose command line matches `@deepseek-ai/dsh/lib/bin.js … web` are killed. + - The restart prints "running in the background; closing this window will NOT stop it" so the returned prompt is not mistaken for an exit. + - Hardening: when the recorded pid's command line cannot be read, it is killed only if it still listens on that port (guards against PID reuse); a failed kill keeps the record for the next boot; boot waits for the killed process to actually exit before binding. +- **Desktop asar swap order (#198, merged PR #198)**: + - Electron main is identified by install root plus "no `--type=`" and killed first, then the rest of the install tree is swept, so the host no longer dies while main stays alive showing a "host stopped" dialog. + - If the asar move fails (still locked), the original app is relaunched and the reason is appended to `%TEMP%\dsh-purge-desktop.log` instead of leaving an empty desktop. +- Tests: 96/96 pass. + # 1.1.85 ## 中文 diff --git a/test/web-reclaim.test.js b/test/web-reclaim.test.js index 74b5d2b..37a096b 100644 --- a/test/web-reclaim.test.js +++ b/test/web-reclaim.test.js @@ -11,6 +11,7 @@ import { parseNetstatListeners, readRecord, recordPath, + waitGone, writeRecord, } from "../lib/web-reclaim.js"; @@ -82,3 +83,18 @@ test("parseLsofPids 处理多行/空输出", () => { assert.deepEqual(parseLsofPids(""), []); assert.deepEqual(parseLsofPids(" \n"), []); }); + +test("waitGone 对空列表/已退出 pid 立即返回", () => { + const t0 = Date.now(); + waitGone([]); + waitGone([0, -1, 999999]); + assert.ok(Date.now() - t0 < 1000, "不应为已退出的进程等到超时"); +}); + +test("preflight 侧车:记录 pid 只有仍在监听端口时才允许盲杀", () => { + const side = fs.readFileSync(new URL("../lib/web-preflight-entry.js", import.meta.url), "utf8"); + assert.match(side, /const ownsPort = listeners\.includes\(recPid\)/); + assert.match(side, /isDshWeb\(cmd\) \|\| \(!cmd && ownsPort\)/); + assert.match(side, /if \(rec && !recStillAlive\)/); + assert.match(side, /waitGone\(killedPids\)/); +});