Repository navigation
Conversation
…vances (#1273) A replaced container leaves a state.lock whose heartbeat is fresh but whose holder is dead. The pid check cannot cross containers, so the foreign-host branch judged liveness by heartbeat age alone and every new instance sat read-only for the whole 60 s stale window — and with no traffic to drive refresh(), even longer. The heartbeat itself is the only cross-host liveness proof available: a live holder bumps the lock file's mtime every beat, so a foreign lock now stays live only while that mtime actually advances. One beat window (beatMs plus margin) without movement can only mean a dead holder, and the lock is reclaimed. A live foreign instance can never lose its lock this way — its beat always moves the mtime inside the window — and a successor that replaces the lock mid-probe restarts the observation, so the probe can never complete against it. The death verdict is sticky per holder so the reclaim path's critical-section re-reads can finish. This trades a bounded amount of stall tolerance for availability: a foreign holder whose heartbeat freezes (event-loop stall, machine asleep) is now overtaken after ~one beat window instead of 60 s. The cost of a false takeover is small by design: every save re-reads the lock file and re-checks the token, so the woken holder turns read-only at its next write instead of corrupting the team — and stalls beyond the old 60 s window were already fatal before this change.
|
Review: approved Note: this is a lock/storage-semantics change; the heterogeneous review (Reviewr) was unavailable, so this is the single review. Scope reviewed: Verified on this head:
Answers to the review questions1. False-takeover risk — quantified, and accepted as designed, with a caveat worth stating plainly. The PR's own framing of "~16–31 s of freeze needed" understates the worst phase: the observation clock starts at the watcher's first sighting, which is unrelated to the holder's beat phase. A beat that lands just before first sighting leaves the holder only the 1100 ms margin before its next beat is overdue. So one single heartbeat delayed by more than ~1.1 s beyond its schedule — a multi-second event-loop stall from GC, a sync DNS resolution, heavy I/O — can produce a false takeover if the watcher's 2. Sticky verdict and the critical section — correct and necessary. Stickiness is required, because the reclaim critical section re-reads the lock and must see a consistent verdict; time alone can never lift it, only a moved mtime or a changed holder key. The critical section does perform a second validation: 3. 4. Interaction with the 60 s reclaim path — convergent, no double takeover. The probe and the old path funnel into the same guarded critical section; the probe merely lets the watcher arrive at ~16 s instead of ~60 s. After a probe takeover the file carries the new holder with a fresh mtime, so the old age-based path sees a live holder and never fires. The reverse ordering (old path fires first at 60 s in a watcher that never refreshed earlier) is equally consistent. 5. Tests — honest, not mock-theater. Real files, real 6. Scope — clean; P2 (suggestions, not blocking)
Not verifiedReal docker/podman replacement cycle (no container runtime here either), Windows, and the DSH smoke test — all as declared in the PR. The lock-protocol analysis above is from source reading plus the injected-clock tests. |
klarkxy
left a comment
There was a problem hiding this comment.
Reviewed via Gemini App · Model: Gemini · Commit:
3523f870a202403028623d76c9d231d969eefe41
整体评估与已核验边界
核心跨主机接管协议与防脑裂边界设计成立:
- 首次建锁宽限期与临界区一致性:
ds-bot/src/host/home-lock.js中foreignAliveAt在首次观测新token:host时授予foreignProbeMs(beatMs + 1100,默认16.1s)初始观察窗口,避免新实例刚抢到锁尚未发出首次心跳(15s)即被误抢;dead: true按token:host粘滞保持以支撑抢占临界区重读,且迟到心跳或中途换主均可即时解除。 - 探活分流与防脑裂兜底:同主机
holder.host === host的 PID 探活不变(注:若容器显式固定相同hostname且旧 PID 被新容器非自身进程复用,仍退回60s窗口);异主机即使发生极端误接管,ds-bot/src/host/store.js保存前后重验owns()也会 fail-closed 降级只读,不会产生双写数据损坏。
分级改进建议
-
[建议修复 / P2] 最坏相位仅
1100ms裕量,在冷启动阻塞、NFS 属性缓存或粗粒度时间戳下易误抢活跃锁- 位置:
ds-bot/src/host/home-lock.js(foreignAliveAt) - 触发条件与建议:当观察者在持有者刚建锁或刚落盘心跳后立即建立/重置观测(
since: now()),距下一次15s心跳仅余1.1s容差。容器冷启动 I/O 抖动超1.1s、Linux NFS 默认属性缓存(acregmin=3s)或2s精度文件系统均会导致stat.mtimeMs未及时刷新而误判dead: true。建议将容差放宽至beatMs + 5000或2 * beatMs(注:切勿按 PR 描述 Known limits 所提改为从stat.mtimeMs起算窗口,否则既无法增大该1.1s相位裕量,还会在跨机时钟偏差下导致首轮瞬时误抢)。
- 位置:
-
[建议改进 / P2] 墙钟跳变与 NTP 回拨敏感性
- 位置:
ds-bot/src/host/home-lock.js(foreignAliveAt) - 触发条件与建议:①
now() - seen.since依赖墙钟,观察端发生 NTP 前跳或宿主机休眠唤醒超16.1s时会直接跨越真实等待期误抢锁,建议生产默认使用单调时钟计时(保留测试时钟注入);②stat.mtimeMs > seen.mtimeMs在持有端发生小幅 NTP 时钟回拨时,即便心跳持续改写mtime(stat.mtimeMs !== seen.mtimeMs)也会因不满足严格大于而误判死亡,建议在ageAt(filePath) < staleMs前提下将stat.mtimeMs !== seen.mtimeMs视为有效心跳并同步更新seen.mtimeMs。
- 位置:
-
[小细节 / Nit]
foreignObservations未回收失效与守卫文件记录- 位置:
ds-bot/src/host/home-lock.js(foreignAliveAt、liveAt) - 触发条件与建议:异机抢占守卫文件(
state.lock.reclaim.*)同样途经foreignAliveAt且从不清理。建议在ageAt(filePath) >= staleMs与stat === undefined分支调用foreignObservations.delete(filePath)并补充容量上限。
- 位置:
单测与 CI 状态
ds-bot/test/home-lock.test.js新增 4 组真实文件与注入时钟测试,覆盖冻结接管、持续心跳保活、迟到心跳重置与中途换主保护。- CI 检查中
Test result(ubuntu-latest · Node 24三分片)、Leak scan与Package result均通过;Evaluate merge guard仅因待人工确认标签needs-product-ok处于预期门禁拦截状态。
Issue
Refs #1273
Agent
Agent: DS Bot (SWE-2.1)
What changed / 改了什么
Root cause: a replaced container leaves
state.lockon the shared volume with a heartbeat younger than 60 s, and the foreign-host liveness branch (home-lock.js) cannot compare pids across containers — so it judged the lock live purely by heartbeat age. Every new instance then sat read-only for the full 60 s stale window (the measured takeover in the issue: ~56–62 s), longer when nothing droverefresh().Fix — the bounded probe from the issue discussion (the option inchTodd proposed and the reporter endorsed after measuring): the heartbeat is the only cross-host liveness proof available, and a live holder bumps the lock file's mtime every
beatMs. A foreign-host lock therefore stays live only while its mtime actually advances:beatMs+ 1100 ms margin) without a moved mtime can only mean a dead holder: the lock is reclaimed through the existing guarded reclaim path.Takeover after a container replacement now lands at about one beat window (~16 s worst case, plus the time until the next
refresh()) instead of ~60 s. Same-host behaviour is completely unchanged.Safety analysis (this is a lock-semantics change, so spelled out):
app/src/import.mjs'steamInUseis deliberately unchanged: it is a one-shot judgement (import UX) that cannot observe over time without blocking; its conservative stale rule stays as is. Noted in the issue thread as a beneficiary, not a requirement.Tests (red before the fix, green after):
Quantifying a false takeover of a live lock (for review):
The window is
beatMs + 1100 ms≈ 16.1 s from the last observed heartbeat movement, not from the last beat. Breakdown of what must all happen for a live foreign holder to lose its lock:utimeson a timer that normally fires every 15 s) for ~16–31 s continuously. In the worst phase the deadline is ~16.1 s after the last landed beat; typically it is up to ~31 s (one full beat period plus the window).claim()at least twice — once to open the observation and once after the window closed.claim()runs fromrefresh(), which fires on channel/mail/IM traffic, not on a timer, so a quiet watcher may not even complete the probe quickly.What a false takeover costs, if it happens anyway:
writable()instore.js→owns()); the token no longer matches, so the very next save attempt fails closed and the instance turns read-only. There is no window in which both processes write the team: the takeover itself is a single guarded rename, and the stale holder's next write is already rejected.The narrowing is intentional, not accidental: the old rule's 60 s tolerance was never a real guarantee either — any freeze longer than 60 s was already fatal under the old rule, and heartbeat liveness cannot tell "frozen" from "dead" at any fixed window. What changes is where the line sits: ~60 s → ~16–31 s. The probe was the option proposed in the issue discussion and endorsed by the reporter after measuring; a more conservative
2 × beatMs + margin(~31–46 s) is a legitimate alternative and easy to adopt if review prefers it — it halves the availability win (60 s → ~31–46 s instead of ~16 s) while doubling the stall tolerance. This PR keeps the one-beat window because the dominant real-world case (container replacement, the issue's own scenario) gains the most and the false-takeover cost is bounded to read-only, never corruption.Scope / 范围: this PR is the lock-adoption half of the issue only — takeover after a container replacement drops from ~60 s to ~16 s. The other half the alignment review asked to keep open, a sidebar hint that a previous instance left a lock instead of a bare read-only message, is not done here and stays for follow-up, hence
Refsrather thanFixes.Changelog
A restarted or replaced container now picks up the team in seconds instead of sitting read-only for a minute.
How I checked it / 怎么验证的
npm test(inds-bot/) — fullnpm run test:windows: see numbers belownode scripts/build-client.mjs --check— clean (no client changes)npm run lint— 0 errorsindex.jsorsrc/host// 改了index.js或src/host/时跑冒烟测试 — cannot run locally (no deepseek-harness source on this machine); see Not verifiedTargeted:
test/home-lock.test.js41/41,test/store.test.jsincluded in 82/82 combined pass; desktop import regressionapp/test/import*.mjs75 pass / 0 fail / 1 skipped. Leak scan: must fix 0. Verified on Mac.Known limits confirmed in review (P2, no code change here):
owns()before and after — no window ever has two writers — and the falsely overtaken instance turns read-only at its next save and stops itself within a beat, recovering without a restart. Starting the window from the last observed heartbeat mtime instead of the observation time is a zero-cost improvement, left for follow-up.foreignObservationsentries for never-beating guard files are never evicted (bounded per path but permanent for the process lifetime). Left for follow-up.Not verified: behaviour inside a real Docker/podman replacement cycle (no container runtime used in this verification; the probe logic is exercised through injected clocks and real lock files);
src/hostsmoke tests (no deepseek-harness source locally). Multi-machine shared-volume setups keep their pre-existing semantics limits: heartbeat liveness can never survive a foreign machine asleep longer than the takeover window — now ~16 s + refresh cadence instead of 60 s (see Safety analysis).Checklist / 检查
src/client/changed →npm run buildran andclient.jsis committed. / 改了src/client/就已重新 build 并提交client.js。(N/A)Co-authored-by. / 没有把 AI 工具写成作者或Co-authored-by。Co-authored-by(GitHub noreply, unless opted out), or N/A. / 促成改动的高质量外部 Issue/想法已按规范加Co-authored-by(GitHub noreply 邮箱,对方介意除外),或不适用。(N/A)