Skip to content

fix(integration): watcher ticker 接受 ctx 并接入 lifecycle#250

Open
pengpeng wants to merge 1 commit intomainfrom
cursor/fix-integration-ticker-lifecycle
Open

fix(integration): watcher ticker 接受 ctx 并接入 lifecycle#250
pengpeng wants to merge 1 commit intomainfrom
cursor/fix-integration-ticker-lifecycle

Conversation

@pengpeng
Copy link
Copy Markdown
Member

@pengpeng pengpeng commented May 5, 2026

概要

`pkg/integration/integration.go` 的 `(*integration).watch` 启动了一个永不退出的 goroutine:

```go
go func() {
for range time.NewTicker(15 * time.Second).C {
i.GetIntegrations()
}
}()
```

  • 没有 ctx;
  • 没有 `ticker.Stop()`;
  • 没有任何 done 通知。

进程进入 graceful-shutdown 后这个循环还在以 15s 一次的频率打 K8s API,与其它子系统的关闭逻辑互相干扰。

改动

  • `integration` 结构加 `cancelWatcher context.CancelFunc` + `watcherDone chan struct{}`。
  • `watch()` 改写为标准的 `for { select { case <-ctx.Done(): return; case <-tick } }` 模式,`defer ticker.Stop()` + `defer close(watcherDone)`。
  • 新增 `(*integration).Stop(ctx context.Context) error`:取消 watcher,再等 `watcherDone` 关闭(或 ctx.Err 超时)。
  • `cmd/backend/app/root.go` 注册 `coord.Add("integration-watcher", 5*time.Second, ...)`,钩子 reverse 顺序保证 watcher 早于 redis/postgres 关闭。

验证方式

  • `go build` 通过。
  • 手工:触发 SIGTERM,确认日志中看到 integration-watcher 收尾后 redis、postgres 才关闭,且 15s 内停止 GetIntegrations 调用。

Made with Cursor

(*integration).watch started a goroutine with
`for range time.NewTicker(15s).C { GetIntegrations() }` - no ctx,
no Stop, no done signal. The loop kept calling K8s after intended
shutdown, racing with the rest of the teardown.

- Add cancelWatcher / watcherDone fields, build them in watch().
- watch() loops on `select { case <-ctx.Done(): return; case <-tick }`
  with `defer ticker.Stop()` and `defer close(watcherDone)`.
- New Stop(ctx) ends the goroutine and waits for it (or returns
  ctx.Err() on shutdown deadline).
- cmd/backend/app/root.go registers a "integration-watcher" hook on
  the lifecycle coordinator with a 5s budget.

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