Skip to content

Start() returns while async handler goroutines are still running; handlers share the poll context #326

Backlog

Description

@gitslim

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

Possible direction

  1. Track handler goroutines: wg.Add(1) / defer wg.Done() around go r(...), and have Start wait for handlers to drain after the workers exit (bounded by an optional WithShutdownGrace(d); unbounded waiting risks hanging on a stuck handler).
  2. 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

  1. DEMONCODEX commented on Nov 26, 2026

    @DEMONCODEX

    I'll take this.

    On current main (since #317), Start/StartWebhook already track dispatched handlers in waitUpdates and wait for them to finish, though handlers still receive the run context directly (canceling immediately on shutdown) and Start/StartWebhook wait 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions