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
- Implement
tokio_util::CancellationToken propagation from the top-level runtime down to all spawned tasks
- Track
JoinHandles for spawned tasks to await completion during shutdown
- Add shutdown timeout — force-kill tasks that don't respond to cancellation within N seconds
- 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
Problem
Background tasks spawned via
tokio::spawnacross gateway, channels, and server have no cancellation mechanism:CancellationTokenorJoinHandletrackingProposed Solution
tokio_util::CancellationTokenpropagation from the top-level runtime down to all spawned tasksJoinHandles for spawned tasks to await completion during shutdownImpact