
Graceful Shutdown in Tokio With Signals and Task Tracking
Shut down a Tokio service cleanly: catch SIGTERM and Ctrl+C, stop accepting work, wait for in flight tasks with TaskTracker, then exit within a deadline.
Tag
33 posts

Shut down a Tokio service cleanly: catch SIGTERM and Ctrl+C, stop accepting work, wait for in flight tasks with TaskTracker, then exit within a deadline.

Stop a Tokio task three ways: JoinHandle::abort, a CancellationToken for cooperative shutdown, and dropping a future. See what each does to cleanup.

select! races futures and drops the losers, which is only safe if they can be dropped mid way. Learn what cancellation safe means and which APIs are not.

Both move blocking work off the async threads, but they differ in pool, limits and fit. See which to pick for file I/O, hashing and number crunching.

JoinSet owns a group of spawned tasks and returns their results as they finish. See spawn, join_next, abort_all and how errors and panics surface.

tokio::spawn demands a future that owns its data. Learn why the compiler asks for static, what it does and does not mean, and four ways to share data with a task.

Spawn tasks with tokio::spawn, wait for them with JoinHandle and collect results with JoinSet. Includes the Send and static bounds and the panics join returns.

What the Tokio runtime is made of: worker threads, a task queue, an I/O driver and a timer. See how multi-thread and current-thread flavors differ.

A guide to async Rust and Tokio: futures, the runtime, spawning, blocking, channels and the mistakes that make a service stop answering.