Skip to content

feat: graceful shutdown waits for in-flight handlers to finish (#326) - #329

Open
DEMONCODEX wants to merge 1 commit into
go-telegram:mainfrom
DEMONCODEX:fix/graceful-handler-shutdown
Open

DEMONCODEX wants to merge 1 commit into
go-telegram:mainfrom
DEMONCODEX:fix/graceful-handler-shutdown

Conversation

@DEMONCODEX

Copy link
Copy Markdown

Problem

When bot.Start or bot.StartWebhook stops upon context cancellation, in-flight update handlers are canceled immediately, which can disrupt active business operations (database transactions, external HTTP calls, etc.).

Closes #326.

Solution

  • Added WithShutdownGrace(duration) option to give handlers a bounded grace window on shutdown.
  • Handlers continue executing on a detached context during the grace period while polling is stopped immediately.
  • If handlers complete before the grace window expires, Start / StartWebhook returns cleanly.
  • If the grace period is exceeded, the handler context is canceled to prevent hanging indefinitely.

Testing

  • Unit and integration tests covering polling shutdown, timeout expiration, stuck handler bounds, and webhook graceful shutdown.
  • All tests pass: go test ./... -count=1.

…m#326)

Introduce a WaitGroup-based mechanism that tracks all active handler
goroutines. When Stop() or StopWebhook() is called, the bot waits for
all in-flight handlers to complete before returning, preventing abrupt
interruptions to running business logic.

New StopTimeout option (default: 30s) lets callers bound how long the
graceful drain takes; on timeout, the remaining handlers are abandoned
and Stop returns.

Closes go-telegram#326
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.

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

1 participant