Skip to content

fix(samba): deleteExpiredShares ticker 接受 ctx 并接入 lifecycle#253

Open
pengpeng wants to merge 1 commit intomainfrom
cursor/fix-samba-expiredshares-lifecycle
Open

fix(samba): deleteExpiredShares ticker 接受 ctx 并接入 lifecycle#253
pengpeng wants to merge 1 commit intomainfrom
cursor/fix-samba-expiredshares-lifecycle

Conversation

@pengpeng
Copy link
Copy Markdown
Member

@pengpeng pengpeng commented May 5, 2026

概要

`pkg/samba/samba.go` 的 `(*samba).deleteExpiredShares`:

```go
go func() {
for range time.NewTicker(5 * time.Minute).C {
// ... List + Delete 都用 context.Background()
}
}()
```

  • goroutine 永不退出;
  • 内部 K8s List / Delete 用 `context.Background()`,graceful-shutdown 也无法打断在途请求;
  • `cmd/samba/main.go` 的 lifecycle 协调器完全不知道这个 goroutine 存在。

SIGTERM 之后 5 分钟内还可能发出 Delete 请求,与其它子系统的关闭交叉。

改动

  • `samba` 结构加 `cancelCleanup context.CancelFunc` + `cleanupDone chan struct{}`。
  • goroutine 改为 `select { case <-ctx.Done(): return; case <-tick }` 模式,K8s 调用也改用循环 ctx。
  • 新增 `(*samba).Stop(ctx)`:取消 + 等 cleanupDone(或 ctx 超时)。
  • `cmd/samba/main.go` 注册 `coord.Add("samba-cleanup", 5*time.Second, ...)`。
  • `cmd/backend/app/root.go` 不调用 `Start()`,因此不需要 hook。

验证方式

  • `go build ./pkg/samba/ ./cmd/samba/` 通过。
  • 手工:在 samba 二进制中触发 SIGTERM,确认 `samba-cleanup` 钩子完成;之后没有任何 "samba delete crds with ticker" 日志,也没有新的 Delete API 调用。

Made with Cursor

(*samba).deleteExpiredShares started a goroutine that ranged
forever over a 5-minute ticker, calling K8s List/Delete with
context.Background() each tick. The loop wasn't tied to any
shutdown signal:

- After SIGTERM the goroutine kept polling K8s and issuing
  Delete calls during the same window other subsystems were
  tearing down.
- The cleanup binary's lifecycle coordinator never knew about it.

Add cancelCleanup / cleanupDone fields and turn the loop into a
standard select { case <-ctx.Done(): return; case <-tick }
shape. Replace the inner context.Background() calls with the
loop's ctx so the in-flight K8s call also gets cancelled.

Expose samba.Stop(ctx) and register a "samba-cleanup" lifecycle
hook in cmd/samba/main.go (the only binary that calls
SambaService.Start() and therefore deleteExpiredShares).

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

1 participant