Repository navigation
Description
Library version: v1.27.0 (also present on current master, process_update.go unchanged)
Problem
In the default async mode, ProcessUpdate spawns handlers detached, with no WaitGroup tracking:
// process_update.go
func (b *Bot) ProcessUpdate(ctx context.Context, upd *models.Update) {
h := b.findHandler(upd)
r := applyMiddlewares(h, b.middlewares...)
if b.notAsyncHandlers {
r(ctx, b, upd)
return
}
go r(ctx, b, upd)
}Start waits only for the getUpdates and waitUpdates workers. On context cancellation, waitUpdates returns as soon as it finishes the current ProcessUpdate call — which returns immediately, since the actual handler runs in a detached goroutine. So Start returns while handler goroutines may still be mid-flight, and a typical caller (b.Start(ctx) followed by cleanup / deferred closes / process exit) kills them in the middle of a DB write or a SendMessage call.
There is a second half to this: handlers receive the same ctx that is passed to Start. Once cancellation propagates, every API call made by an in-flight handler fails immediately with context.Canceled — so even a caller that tracks and waits for its own handler goroutines cannot let them finish their work; the context they need is already dead.
Together this means graceful shutdown is currently not achievable: updates that are already being processed are neither waited for, nor allowed to complete their outbound calls.
Related
- Process updates before shutdown #239 proposes keeping delivery of already-received updates to handlers during shutdown (the "queued updates" half). The in-flight handlers half (wg tracking + a handler context that outlives the poll context) is not covered there.
- The Telegram bot library for Golang is not shutting down gracefully. #121 is a different problem (two bot instances conflicting after a restart).
Possible direction
- Track handler goroutines:
wg.Add(1)/defer wg.Done()aroundgo r(...), and haveStartwait for handlers to drain after the workers exit (bounded by an optionalWithShutdownGrace(d); unbounded waiting risks hanging on a stuck handler). - Give handlers a context they can still use during drain: when the poll ctx is cancelled, derive
context.WithTimeout(context.WithoutCancel(pollCtx), grace)for updates already dispatched to handlers.
WithNotAsyncHandlers is a partial workaround today: handlers become synchronous, so waitUpdates waits for the current one — but its ctx is already cancelled, so outbound calls still fail, and all update processing is serialized.
Workaround
None complete. Our current stance: accept that a single in-flight user command may be cut on SIGTERM (handlers perform single atomic statements; the user re-sends the command).
Activity
DEMONCODEX commentedon Nov 26, 2026
I'll take this.
On current
main(since #317),Start/StartWebhookalready track dispatched handlers inwaitUpdatesand wait for them to finish, though handlers still receive the run context directly (canceling immediately on shutdown) andStart/StartWebhookwait unboundedly.I'll add a
WithShutdownGrace(d time.Duration)option (with a Go 1.18-compatible detached context) so already-dispatched handlers get a bounded grace window to complete outbound work after cancellation while preserving existing default behavior when unset.