gui: TestSetPortServesWhenTheOldOneWasTaken's TempDir cleanup finds .config/magpie not empty (full suite on mote)
Seen in a full go test ./... -timeout 45m -count=1 on mote at 05d2d852e (base 4d79bebfa; the change touches internal/gateway only):
--- FAIL: TestSetPortServesWhenTheOldOneWasTaken (0.23s)
testing.go:1464: TempDir RemoveAll cleanup: unlinkat /var/folders/.../TestSetPortServesWhenTheOldOneWasTaken2037326217/001/.config/magpie: directory not empty
Passed alone at base (1x) and at head (3x). So something the test starts (the moved gateway, or a settings save) still writes into the sandbox HOME's .config/magpie after the test returns. Whatever a test starts has to end when the test does (code standards). It's not fixed here; nobody owns it yet.
这里有一处确定的生命周期遗漏,提供一个仅修复测试主网关服务清理的部分候选,基于 1bd1a08a3a5b70934eb12519574a087a98cbac11。
setPort 在旧端口被占用时调用 serveGateway 启动实际网关,原测试 cleanup 却只 served.Store(nil),没有取消或等待服务。改为在 gatewayMu 下复用已有 stopServedLocked(),在 sandbox HOME 恢复/删除之前取消服务并等 servedRun.done 关闭。另加先注册、后执行的 cleanup 断言检查捕获的 done,不靠 sleep、重试或放宽阈值。
验证
Linux amd64、Go 1.26.8,临时 HOME 和隔离缓存,仅本地测试服务。
- 先只加生命周期断言,保留原 cleanup:
go test -p 1 -tags nogui ./internal/gui -run '^TestSetPortServesWhenTheOldOneWasTaken$' -count=1真实失败(0.148s):--- FAIL: TestSetPortServesWhenTheOldOneWasTaken the moved gateway outlived its test cleanup - 修复后
go test -p 1 -tags nogui ./internal/gui -run '^TestSetPort' -count=20通过(1.827s),包含相邻移动端口测试。 go vet -p 1 -tags nogui ./internal/gui通过;gofmt、git diff --check 干净。独立只读复核无阻断。- 发布前再次核对到
2ab2f69,相关 port_test/backend/gatewaypause 文件未出现上游变化,未发现重复评论或 PR。
**边界:**这是确定性复现“主服务未结束”,不是复现原 macOS 全套的 directory-not-empty 报错。主服务的 done 不等于全部 provider/plugin/filememo 后台循环已 join;取消通知它们退出,但本补丁不能保证所有异步写入均已结束。因此不宣称 #7112 整体解决。未跑完整包/仓库 suite、race、跨平台 build/vet 或原始 macOS 场景。
仅测试文件变化,无生产语义、依赖或文档契约改变。未创建 PR、未合入。AI 辅助实现和验证,基于 @yetone 的失败报告;采用时请保留报告者与补丁作者 credit:
Co-authored-by: yetone <[email protected]>
diff --git a/internal/gui/port_test.go b/internal/gui/port_test.go
index bd263fa..024c897 100644
--- a/internal/gui/port_test.go
+++ b/internal/gui/port_test.go
@@ -156,8 +156,22 @@ func TestSetPortServesWhenTheOldOneWasTaken(t *testing.T) {
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
+ var gatewayDone <-chan struct{}
+ t.Cleanup(func() {
+ if gatewayDone != nil {
+ select {
+ case <-gatewayDone:
+ default:
+ t.Error("the moved gateway outlived its test cleanup")
+ }
+ }
+ })
served.Store(nil)
- t.Cleanup(func() { served.Store(nil) })
+ t.Cleanup(func() {
+ gatewayMu.Lock()
+ defer gatewayMu.Unlock()
+ stopServedLocked()
+ })
if gateway.Running() {
t.Fatal("another program taken for magpie")
}
@@ -165,6 +179,12 @@ func TestSetPortServesWhenTheOldOneWasTaken(t *testing.T) {
if _, err := setPort(to); err != nil {
t.Fatal(err)
}
+ gatewayMu.Lock()
+ gatewayDone = servedRun.done
+ gatewayMu.Unlock()
+ if gatewayDone == nil {
+ t.Fatal("the moved gateway has no completion signal")
+ }
for deadline := time.Now().Add(5 * time.Second); !answers(to); time.Sleep(20 * time.Millisecond) {
if time.Now().After(deadline) {
t.Fatal("the gateway didn't start on the new port")
Seen again in a go test -count=1 ./internal/gui package run on mote (load ~40 on 18 cores), at ecf116775. That is origin/main 69dce314b plus a stack that doesn't touch the port code or SetPort:
--- FAIL: TestSetPortServesWhenTheOldOneWasTaken (0.13s)
testing.go:1464: TempDir RemoveAll cleanup: unlinkat /var/folders/.../TestSetPortServesWhenTheOldOneWasTaken4230922142/001/.config/magpie: directory not empty
The log lines just before it were gateway: listening on 127.0.0.1:3426, as settings say now and then the same for 127.0.0.1:3425. That is consistent with a gateway restarted by the port change still writing under the test's HOME after the test returns. I am not working on this. Every other test in the package passed in the same run.
Seen again on mote, go test -tags nogui -count=1 -timeout 45m of library, agent, provider, cli, gui and usage at e54249dbc (the qq01-hub port stack on origin/main ba2f26b98, which doesn't touch the port or settings code). The same cleanup error: TempDir RemoveAll cleanup: unlinkat …/TestSetPortServesWhenTheOldOneWasTaken…/001/.config/magpie: directory not empty (0.13s), the only red of the six packages. Not taking it.
Taking it: the cleanup only cleared served, so the gateway setPort started kept serving and wrote into the sandbox HOME's .config/magpie after the test (yhdhaidong's reading above). Fix coming with a check that fails on the old cleanup. Also red on ubuntu CI at 6d6798ba8 (run 46581929882).
Fixed in bacdd2e50: the test stops the gateway it started before its HOME goes, and fails if it hasn't ended (red 3 of 3 on the old cleanup).