Skip to content

Graceful shutdown for background tasks #21

Description

@longzhi

Problem

Background tasks spawned via tokio::spawn across gateway, channels, and server have no cancellation mechanism:

  • Delivery listeners, approval listeners, channel bot loops all run indefinitely
  • No CancellationToken or JoinHandle tracking
  • Server restart may lose in-flight messages
  • No way to know when all background tasks have completed during shutdown

Proposed Solution

  1. Implement tokio_util::CancellationToken propagation from the top-level runtime down to all spawned tasks
  2. Track JoinHandles for spawned tasks to await completion during shutdown
  3. Add shutdown timeout — force-kill tasks that don't respond to cancellation within N seconds
  4. Log in-flight work status during shutdown sequence

Impact

  • Data safety — in-flight messages are processed before shutdown
  • Clean restarts — no orphaned tasks or leaked resources
  • Observability — shutdown logs show what was still running

Activity

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

    P2: mediumImportant but not urgent, schedule itenhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions