From ff41d10ff0fe8611ecda5e81fa0bb4058b830e0b Mon Sep 17 00:00:00 2001 From: Qinyu Tong Date: Thu, 27 Aug 2026 10:46:44 -0700 Subject: [PATCH] clifecycle: add SetStopTimeout to make the shutdown grace period configurable Co-authored-by: Cursor --- clifecycle/lifecycle.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/clifecycle/lifecycle.go b/clifecycle/lifecycle.go index 7b35086..ce56e4c 100644 --- a/clifecycle/lifecycle.go +++ b/clifecycle/lifecycle.go @@ -52,6 +52,18 @@ func (lc *Lifecycle) OnStop(fn func(ctx context.Context) error) { lc.onStop = append(lc.onStop, fn) } +// SetStopTimeout overrides how long Stop waits for background goroutines to +// complete before running cleanup funcs and canceling the lifecycle context. +// Defaults to 30 seconds. Apps whose background work is bounded by a longer +// deadline should set this above that deadline so graceful shutdown does not +// force-quit work that is about to finish. +// +// Call this during app initialization, before Stop may run; it must not be +// called concurrently with Stop. +func (lc *Lifecycle) SetStopTimeout(d time.Duration) { + lc.stopTimeout = d +} + // Go starts a background goroutine that will be waited for during shutdown. // The goroutine should return when the context is done or when its work is complete. //