Skip to content

fix(backend): bound the wait for a process drain, not the drain itself - #72

Merged
AresConnor merged 1 commit into
masterfrom
fix/bound-the-instance-drain
Aug 1, 2026
Merged

AresConnor merged 1 commit into
masterfrom
fix/bound-the-instance-drain

Conversation

@AresConnor

Copy link
Copy Markdown
Contributor

Closes #70.

调查过程中 #70 原本的诊断被推翻了,更正已发在该 issue 下;这里只讲结论。

责任方不是 issue 里写的那个

#70 和代码注释都把 14 分钟归给生命周期发布尾部。但测试的 finally 在调用 ForceKillAndClearAsync 之前就已 releaseOldTimeout.TrySetResult()——屏障是放开的,执行根本走不到那一步。

KillAndDrainAsync(InstanceProcess.cs:319)里有三处外部方可致永不完成:

位置 谁能让它停住
:336-337 子孙进程退出 不死的孙进程
:344 → :602 裸 await pumps 管道 EOF。PumpAsync 的读硬编码 CancellationToken.None(:460),所以 _pumpCancellation.Cancel() 无效,:468 那个 catch 在此路径是死代码。Windows 匿名管道的 EOF 只在机器上最后一份写句柄副本关闭时到达,包括被无关进程继承的
:354 发布尾部 被阻塞的订阅者

两次独立追踪对"究竟是哪一处"给出不同倾向,都没有 dump 佐证。 所以本 PR 不猜——在调用方加一次 WaitAsync,一次覆盖三处,diff 也比分别加界更小。

冒烟证据:PTY 分支 :590 早就给同一危险加了 2 秒上界并带回退,紧邻的管道分支 :600-603 却是光秃秃的 await pumps。一半已经被人加过界了。

这是生产缺陷,测试只是信使

  • InstanceManager.KillInstanceAsync 全程持有 mutation admission 和该实例的 gate,一次卡死让该实例之后所有 start/stop/kill/remove/settings 永久排队;halt RPC 不返回;WPF 上是按钮无响应且无报错。
  • 更坏的一个:同一个排空在 StartAsync → DisposeManagedProcessAsync 路径上。第 N 代卡住的排空让该实例永远无法再启动,直到 daemon 重启。
  • halt RPC 把客户端取消令牌忠实转发进了一个不会遵守它的调用——KillAndDrainAsync 只在入口检查一次,之后每个 await 都忽略。

修法

与插件能力撤销(fix(plugins): bound the whole revocation…)同形,但更小——不需要 memoized sweep 字段,因为 ResetProcess 在超时路径上就清掉绑定。

限的是等待,不是工作:不取消任何东西,排空照常在后台跑完。

gate 不变量不被削弱:它从来不是靠"持有 gate 等待"保证的,而是靠 fencing——而 fencing 现在在超时路径上也会发生,此前那条路径是跳过它的。会因半排空实例出错的具体操作是 StartAsync:旧代 FinalizeProcessAsync 末尾的 PublishStoppedAsync() 若落在新代已提交 Running 之后会让目录回退。每条发布路径都会重新检查当前绑定,所以绑定一旦解除,被遗弃的那一代再也够不到目录,无论它还要跑多久。

排空失败(而非超时)仍保留绑定并抛出——那时无法确认 kill 是否生效,不能报告虚假的 halt 成功。

顺带把 ForceKillAndClearAsync 与 DisposeManagedProcessAsync 这对近乎重复的方法合并,上界只存在一处。

负向对照

去掉 WaitAsync(drainDeadline)、其余改动保留,新测试的 halt 不返回,在 969 行 5 秒上限处失败:

System.TimeoutException : The operation has timed out.
   at ...InstanceBase_ForceKillDetachesTheGenerationWhenTheDrainOutlastsItsDeadline() ...:line 969

加回上界后通过(2 秒)。测试用一个永不返回的订阅者作为"daemon 无法约束的一方"的替身,确定性触发,不是竞态。

验证

全新重建(先清空全部 obj/bin):整解决方案 0 warning / 0 error;ProtocolTests 1373/1373;Daemon.ApiTests 116/116;git diff --check 干净。

不在本 PR 内

InstanceProcessEventPumpTests.cs:986 的 3 秒上限。它该调,但单独一个提交,且在这个之后——而且那条测试的 Assert.False(haltTask.IsCompleted)(:982)并不能证明它读起来要证明的事:坏运行里 haltTask 早已停在排空上而非屏障上,放开屏障对它毫无作用。

KillAndDrainAsync waits on three things the daemon does not control: descendant
exits, the redirected pipes reaching EOF, and the lifecycle publication tail. A
Windows anonymous pipe reaches EOF only when the last inherited copy of its write
handle closes anywhere on the machine, and the tail is only as fast as its slowest
subscriber. Both callers awaited that with no deadline.

InstanceManager.KillInstanceAsync holds the mutation admission and the per-instance
gate across the halt, so one wedge queues every later start, stop, kill, remove and
settings call on that instance forever, and the halt RPC never returns -- with the
client's cancellation token silently ineffective, since KillAndDrainAsync checks it
once on entry and every await after that ignores it. Worse, the same drain is on
StartAsync's path through DisposeManagedProcessAsync, so a generation that never
finishes draining means the instance can never start again until the daemon
restarts.

The wait is now bounded and the work is not: nothing is cancelled, the drain runs
to completion in the background, and the generation is detached on the deadline
path as well as on the completion path. Detaching is what fences the straggler, and
it is safe there because termination is requested and the terminal status committed
synchronously before the drain's first await; every publication path re-checks the
current binding, so an abandoned generation cannot reach the catalog however long
it takes. A drain that fails rather than outruns us still keeps the binding and
rethrows, because then the kill is not known to have taken.

ForceKillAndClearAsync and DisposeManagedProcessAsync were near-duplicates of each
other; the former is now a wrapper over the latter, so the bound exists in one
place.

Which of the three waits parks on CI is not established -- two independent traces
disagreed and neither had a dump -- which is the argument for bounding the caller
rather than any one await. Note the pipe path was already half-bounded: the PTY
branch has awaited its pumps with a 2s deadline for some time, while the pipe
branch next to it had none.

Negative control: with the deadline removed and the rest of the change in place,
the new test's halt never returns and it fails on the 5s ceiling at line 969.
@AresConnor
AresConnor merged commit dbf3d80 into master Aug 1, 2026
4 checks passed
@AresConnor
AresConnor deleted the fix/bound-the-instance-drain branch August 1, 2026 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: a 3s timing blip in protocol-tests costs a 14-minute job, because the cleanup after it is unbounded

1 participant