Skip to content

1.9 Async Rust and tokio: futures, tasks, channels, cancellation, hyper

The tracer engine of Your first endpoint in AI Engineering gives each connection an OS thread. That is simple, and fine for a handful of clients, but it does not scale to what the production engine server must handle: thousands of open streams, most of them idle between tokens, plus a CPU-bound engine that must never be interrupted. From here on, every Rust network service in these books (the v1 engine server, the gRPC services, the gRPC client of the next section) is written on tokio, the async runtime, and hyper, the HTTP library on top of it.

Concepts from other sections (6)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This section teaches the four ideas they rest on: futures, tasks, channels, and cancellation. You build a small server called ticker with four routes: GET /health, GET /ticks (a Server-Sent Events stream fed by a producer task through a bounded channel), POST /jobs (two slots, else 429, through a semaphore), and GET /stats, with a server loop that shuts down gracefully. The engine server of tl-serve: OpenAI-compatible HTTP + SSE on tokio/hyper is this package grown up.

It builds on Rust: ownership, traits, Result, cargo workspaces, std TCP for ownership, traits, Result, and threads, and on HTTP/1.1, JSON, and Server-Sent Events from the wire up, whose bytes you will see again here. The solution is in course/solutions/primers/lang.09/: the library src/lib.rs (produce, Admission, ChannelBody, handle, serve) and the binary src/main.rs (main). The package depends on tokio, hyper 1, hyper-util, http-body-util, and bytes (Cargo.toml), and builds offline from a warm Cargo cache. There is no separate contract; the routes and signatures in “Designing the API” are the contract. The solution runs like this:

Terminal window
cd primers/lang.09 && cargo test && cargo run -- --port 8080
curl -N 'http://127.0.0.1:8080/ticks?n=5&interval_ms=500' # one event every half second
for i in 1 2 3; do curl -s -X POST localhost:8080/jobs -d '{"ms":2000}' & done; wait # two 200s, one 429

Learning objectives

  • Explain what a future is, why nothing runs until it is polled, and what the executor and reactor of a runtime each do.
  • Keep worker threads unblocked: async sleeps, and CPU-heavy work on its own thread.
  • Use a bounded channel for backpressure and a semaphore for bounded admission that answers 429 instead of queueing.
  • Explain cancellation as dropping, and make a producer notice a departed client even while it sleeps.
  • Write a hyper server with a streamed body and a three-step graceful shutdown.

Start with why threads are the wrong unit here. A thread is the OS’s unit of scheduling: each has its own stack (megabytes of reserved address space), and switching between them is a trip through the kernel. Ten thousand threads mostly waiting on sockets is ten thousand stacks and constant switching. An async task is a much smaller thing: a state machine in a heap allocation, run by a user-space scheduler on a few threads. While a task waits for its socket, it takes no thread at all.

The state machine inside a task is a future, a value that may produce a result later. In Rust it is any type implementing

trait Future { type Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>; }
enum Poll<T> { Ready(T), Pending }

poll either finishes (Ready) or says “not yet” (Pending) after arranging, through cx.waker(), to be woken when progress is possible: a socket became readable, a timer fired, a channel got a message. The compiler turns an async fn into such a state machine. Each .await is a state where the function may pause, keeping its local variables inside the future, and a point where the thread may go and run another task.

The most important consequence is easy to forget: nothing runs until something polls it. Calling an async fn only builds the future. Pin promises the future will not move in memory once polled, because its states may point into themselves.

Something has to do the polling. tokio provides the two halves that make futures run. The executor keeps a queue of tasks and polls them on a pool of worker threads (one per core by default; idle workers steal tasks from busy ones). The reactor asks the OS (kqueue on macOS, epoll on Linux) which sockets are ready and wakes the tasks waiting on them; the timer wheel does the same for sleep. #[tokio::main], or tokio::runtime::Builder::new_multi_thread().enable_all().build() plus block_on, starts both.

That design gives the cardinal rule: never block a worker thread. std::thread::sleep, a blocking file read, or a long computation inside a task holds the thread, and every other task on it waits. Sleep with tokio::time::sleep(d).await; move CPU-bound work to tokio::task::spawn_blocking or to a thread of its own, as the engine server does with its engine.

tokio::spawn(future) hands a future to the executor as an independent task and returns a JoinHandle. The future must be 'static (it owns everything it uses, since it may outlive the caller) and Send (it may move between worker threads), which is why shared state travels as Arc<...>. A JoinSet holds many tasks, so a server can wait for all of them at shutdown.

Tasks talk through channels. tokio::sync::mpsc::channel(n) is bounded: it holds at most n messages, and send(x).await pauses the sender while it is full. That pause is backpressure: a producer can never be more than n messages ahead of its consumer, so memory stays bounded however slow the reader is, and a slow reader slows the producer instead of growing memory. unbounded_channel never pauses (and never bounds memory). oneshot carries one reply; watch broadcasts the latest value (a shutdown flag). Sender::closed().await completes when the receiver is gone.

A semaphore with k permits bounds concurrency. try_acquire_owned() takes a permit or fails at once, and dropping the permit gives it back. “At most two jobs; the third gets 429” is a semaphore with 2 permits and try_acquire.

Rust has no cancel call for futures, and it does not need one. A future that is not polled does not run, and a future that is dropped is cancelled at the .await where it last paused: its destructors run, and nothing after that point happens. Cancellation comes from two everyday sources.

The first is tokio::select!, which polls several futures and drops every one that did not finish first. select!{ _ = sleep(d) => {}, _ = tx.closed() => return } sleeps unless the client leaves first.

The second is hyper, which drops a response body when the client disconnects. If the body is the receiving end of a channel, the sender learns of it at its next send (an Err) or through closed().

With those ideas, a hyper server is short. You bind a listener, accept in a loop, spawn a task per connection, and serve the connection with a service function:

let listener = tokio::net::TcpListener::bind(addr).await?;
loop {
let (stream, _) = listener.accept().await?;
tokio::spawn(async move {
let svc = hyper::service::service_fn(|req| handle(app.clone(), req)); // Request<Incoming> -> Response<Body>
hyper::server::conn::http1::Builder::new().serve_connection(hyper_util::rt::TokioIo::new(stream), svc).await
});
}

A response body is anything implementing hyper::body::Body, whose poll_frame returns the next chunk of bytes when it exists. http_body_util::Full is a whole body; a streamed body wraps a channel receiver and returns rx.poll_recv(cx). A body of unknown length goes out with chunked transfer encoding, each frame as soon as it is produced.

The last piece is stopping well. On SIGTERM (tokio::signal::unix::signal(SignalKind::terminate())), a server does three things: it stops accepting (it drops its listener), lets the connections already open finish (they run in a JoinSet; wait with a timeout), and returns from main, exit status 0. Kubernetes sends SIGTERM before it kills a pod, so this is what makes a rolling deploy cut nothing.

Trace GET /ticks?n=3&interval_ms=10 with a channel of capacity 4. Each row shows the producer task, the channel, the body hyper polls, and what the client sees:

TimeProducer taskChannelBody (hyper)Client sees
0 mssends data: {"i":0}\n\n1 queuedpoll_frame returns itevent 0
0 msselect!: sleep 10 ms or closed()emptypoll_frame is Pending
10 mssends {"i":1}1returns itevent 1
20 mssends {"i":2}1returns itevent 2
30 mssends data: [DONE]\n\n, then returns (drops tx)1, closedreturns it, then None: body ends[DONE], then the terminal chunk

The body is exactly data: {"i":0}\n\ndata: {"i":1}\n\ndata: {"i":2}\n\ndata: [DONE]\n\n. On the wire each event is one chunk, f\r\ndata: {"i":0}\n\n\r\n (0x0f = 15 bytes), and the stream ends with 0\r\n\r\n.

Now a disconnect. With n=1000, the client reads two events and closes. hyper notices the closed socket and drops the body, which drops rx. The producer is sleeping in select!; tx.closed() completes, and it returns Cancelled. /stats shows cancelled up by one and active back to 0.

Finally, admission. Send three POST /jobs {"ms": 600} at once. The first two take the two permits and sleep 600 ms; the third finds no permit and gets 429 with Retry-After: 1 at once, without waiting for a slot.

The library’s public surface is small:

primers/lang.09/src/lib.rs
pub const CAPACITY: usize = 4; pub const JOB_SLOTS: usize = 2;
pub fn sse_event(payload: &str) -> String; // "data: <payload>\n\n"
pub fn query_param<'a>(query: &'a str, key: &str) -> Option<&'a str>;
pub fn json_int(body: &str, key: &str) -> Option<u64>; // {"ms": 200} -> 200
pub struct Stats { pub started, active, completed, cancelled: AtomicUsize } // to_json()
pub enum Outcome { Completed, Cancelled }
pub async fn produce(n: usize, interval: Duration, tx: mpsc::Sender<Bytes>, stats: Arc<Stats>) -> Outcome;
pub struct Admission { /* Arc<Semaphore> */ } impl Admission { pub fn new(slots: usize) -> Self; pub fn try_admit(&self) -> Option<OwnedSemaphorePermit>; }
pub struct App { pub stats: Arc<Stats>, pub jobs: Admission }
pub struct ChannelBody { /* mpsc::Receiver<Bytes> */ } // impl hyper::body::Body
pub async fn handle(app: Arc<App>, req: Request<Incoming>) -> Result<Response<Out>, Infallible>;
pub async fn serve(listener: tokio::net::TcpListener, app: Arc<App>, shutdown: impl Future<Output = ()>);

The routes answer as follows:

RouteAnswer
GET /health200 {"ok":true}
GET /ticks?n=5&interval_ms=100200 text/event-stream: data: {"i":k} for k in 0..n, then data: [DONE]
POST /jobs {"ms": 200}200 {"ok":true,"slept_ms":200} after sleeping, or 429 Retry-After: 1 when both slots are taken
GET /stats200 {"started","active","completed","cancelled"}

ticker --port 0 prints listening on 127.0.0.1:<port> first. SIGTERM stops accepting, waits for open requests, and exits 0.

Walk through the reasoning before the code, and try to predict each function from it.

  1. A stream is a producer and a consumer at different speeds. The producer makes events on a timer; the consumer is a socket that drains as fast as the client reads. Putting a bounded channel between them (capacity CAPACITY = 4) means the producer can be at most four events ahead: send().await pauses it when the client is slow.
  2. The response body must be the channel’s receiver. Then hyper pulls each event out of the channel as soon as it exists (streaming, not buffering), and when the client disconnects, hyper drops the body, which drops the receiver, which is how the producer learns the client is gone.
  3. The producer must notice a gone client even while it sleeps. A send fails only when it is attempted; between events the producer is in sleep. So it sleeps inside select! against tx.closed(), which completes the moment the receiver is dropped.
  4. The producer is its own task. The handler must return the response (headers first) immediately and let the body fill later, so the producer is tokio::spawned and owns its Sender and an Arc<Stats>.
  5. Admission must refuse, not queue. “At most two jobs, the third gets 429” is a semaphore with two permits and a non-waiting try_acquire. The permit is a value: holding it is holding the slot, and dropping it frees the slot on every path out, including a cancelled handler.
  6. Never block a worker. The job sleeps with tokio::time::sleep(..).await, so a sleeping job takes no thread and the 429 for the third job is answered at once.
  7. Shutdown is a select! between accepting and a signal. On the signal, the loop breaks, the listener is dropped (no new connections), and the server waits, with a bound, for the connection tasks it spawned into a JoinSet.

The producer counts itself in Stats, sends n events plus [DONE], and stops at the first sign the receiver is gone:

pub async fn produce(n: usize, interval: Duration, tx: mpsc::Sender<Bytes>, stats: Arc<Stats>) -> Outcome {
stats.started.fetch_add(1, Ordering::SeqCst);
stats.active.fetch_add(1, Ordering::SeqCst);
let mut outcome = Outcome::Completed;
for i in 0..=n {
// i == n is the terminal [DONE] event, so n events plus one.
let ev = if i == n { sse_event("[DONE]") } else { sse_event(&format!("{{\"i\":{i}}}")) };
// Backpressure: waits while 4 events are queued. Err means the receiver
// (the response body) was dropped: the client is gone.
if tx.send(Bytes::from(ev)).await.is_err() {
outcome = Outcome::Cancelled;
break;
}
if i < n && !interval.is_zero() { // no sleep after the last event
tokio::select! {
_ = tokio::time::sleep(interval) => {}
// Completes as soon as the receiver is dropped: cancel mid-sleep.
_ = tx.closed() => {
outcome = Outcome::Cancelled;
break;
}
}
}
}
// One exit path, so active always goes back down and exactly one of
// completed/cancelled goes up.
stats.active.fetch_sub(1, Ordering::SeqCst);
match outcome {
Outcome::Completed => stats.completed.fetch_add(1, Ordering::SeqCst),
Outcome::Cancelled => stats.cancelled.fetch_add(1, Ordering::SeqCst),
};
outcome
}

Returning drops tx. With no sender left, the receiver’s next poll_recv returns None, which is the end of the body: the last row of the hand trace.

The whole Body implementation is one line, because mpsc::Receiver::poll_recv already has the right shape: Ready(Some(x)), Ready(None) at the end, or Pending with the waker registered.

pub struct ChannelBody {
rx: mpsc::Receiver<Bytes>,
}
impl Body for ChannelBody {
type Data = Bytes;
type Error = Infallible;
fn poll_frame(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Result<Frame<Bytes>, Infallible>>> {
// Some(bytes) -> one data frame (one chunk on the wire); None -> end of body.
self.rx.poll_recv(cx).map(|o| o.map(|b| Ok(Frame::data(b))))
}
}

Admission is a cloneable wrapper around an Arc<Semaphore>. try_acquire_owned returns a permit that is not tied to a borrow of the semaphore, so it can be held across .awaits in the handler.

impl Admission {
pub fn new(slots: usize) -> Admission {
Admission { sem: Arc::new(Semaphore::new(slots)) }
}
/// A permit (released when dropped), or `None` when every slot is taken.
pub fn try_admit(&self) -> Option<OwnedSemaphorePermit> {
Arc::clone(&self.sem).try_acquire_owned().ok() // never waits
}
}

The handler is one match on method and path. All responses share the boxed body type Out, so a Full JSON body and a ChannelBody stream can come from the same function.

pub async fn handle(app: Arc<App>, req: Request<Incoming>) -> Result<Response<Out>, Infallible> {
let query = req.uri().query().unwrap_or("").to_string();
Ok(match (req.method(), req.uri().path()) {
(&Method::GET, "/health") => reply(StatusCode::OK, "{\"ok\":true}".to_string()),
(&Method::GET, "/stats") => reply(StatusCode::OK, app.stats.to_json()),
(&Method::GET, "/ticks") => {
let n = query_param(&query, "n").and_then(|v| v.parse().ok()).unwrap_or(5usize);
let ms = query_param(&query, "interval_ms").and_then(|v| v.parse().ok()).unwrap_or(100u64);
let (tx, rx) = mpsc::channel(CAPACITY); // bounded: the backpressure
tokio::spawn(produce(n, Duration::from_millis(ms), tx, Arc::clone(&app.stats)));
// Return now: headers go out at once, events follow as produced.
let mut r = Response::new(ChannelBody { rx }.boxed());
r.headers_mut().insert("content-type", "text/event-stream".parse().expect("static"));
r.headers_mut().insert("cache-control", "no-cache".parse().expect("static"));
r
}
(&Method::POST, "/jobs") => {
// Admission before reading the body: a refused job costs nothing.
let Some(permit) = app.jobs.try_admit() else {
let mut r = reply(StatusCode::TOO_MANY_REQUESTS, "{\"error\":\"busy\"}".to_string());
r.headers_mut().insert("retry-after", "1".parse().expect("static"));
return Ok(r);
};
let body = match req.into_body().collect().await {
Ok(b) => String::from_utf8_lossy(&b.to_bytes()).into_owned(),
Err(_) => String::new(),
};
let Some(ms) = json_int(&body, "ms") else {
return Ok(reply(StatusCode::BAD_REQUEST, "{\"error\":\"ms must be a non-negative integer\"}".to_string()));
};
tokio::time::sleep(Duration::from_millis(ms)).await; // never thread::sleep
drop(permit); // the slot is free before the reply is written
reply(StatusCode::OK, format!("{{\"ok\":true,\"slept_ms\":{ms}}}"))
}
_ => reply(StatusCode::NOT_FOUND, "{\"error\":\"not found\"}".to_string()),
})
}

The early return Ok(r) after a bad body also drops permit, so the slot comes back on every path. json_int reads only digits after the colon, so {"ms": -1} is None and answers 400.

pub async fn serve(listener: tokio::net::TcpListener, app: Arc<App>, shutdown: impl Future<Output = ()>) {
let mut conns = tokio::task::JoinSet::new(); // every connection task, so we can wait for them
tokio::pin!(shutdown); // polled repeatedly by select!, so it must not move
loop {
tokio::select! {
_ = &mut shutdown => break, // SIGTERM or Ctrl-C
acc = listener.accept() => {
let Ok((stream, _)) = acc else { continue }; // one bad accept is not fatal
let _ = stream.set_nodelay(true); // send each small SSE chunk now
let app = Arc::clone(&app);
conns.spawn(async move {
let svc = service_fn(move |req| handle(Arc::clone(&app), req));
let _ = hyper::server::conn::http1::Builder::new().serve_connection(TokioIo::new(stream), svc).await;
});
}
}
}
drop(listener); // step 1: stop accepting; new connections are refused
// Step 2: let open connections finish, but never wait forever.
let _ = tokio::time::timeout(Duration::from_secs(10), async { while conns.join_next().await.is_some() {} }).await;
} // step 3: return; main returns; exit 0

main builds a multi-thread runtime, binds 127.0.0.1:<port>, prints listening on ... and flushes stdout (so a parent process reading the port is not stuck behind a buffer), and passes a shutdown future that completes on SIGTERM or Ctrl-C.

The full solution is in src/lib.rs and src/main.rs.

Memory per stream is bounded by CAPACITY events plus one small task, however slow the client. A thread per stream would cost a stack each, and an unbounded channel would let a stalled client grow the queue to all nn events (200,000 in the slow-reader case).

Cancellation is free because it is dropping: there is no flag to check and no cancel message to route. The only extra code is the closed() arm, which turns “noticed at the next send” into “noticed now”.

The design answers 429 instead of queueing. acquire().await would also bound concurrency, but excess jobs would wait invisibly, holding connections, and the client could not tell overload from slowness. try_acquire plus Retry-After pushes the decision back to the client, which is what the engine server does under load.

A hand-written shutdown costs a JoinSet and a select!; hyper-util’s graceful helper (see the production equivalents below) also tells idle keep-alive connections to close. The 10 s cap trades a possibly cut long stream for a bounded shutdown, inside Kubernetes’ 30 s grace period.

Finally, SeqCst everywhere on the counters is stronger than needed (Relaxed would do for independent counters), but it costs nothing measurable here and removes a question.

A correct solution passes these checks:

CaseKind of checkChecksWhy it matters downstream
The crate buildsconformancethe package builds offline with a ticker binarythe crates the engine server uses
The package’s own tests passunitthe #[tokio::test]s in lib.rs passtesting async code
The hand exampleunitthe hand-traced body, byte for byteSSE over hyper
Healthunita plain JSON routethe handler shape
Events leave as producedunitthe first event arrives long before the last is producedstreaming, not buffering
A disconnect cancels the producerfaulta disconnect cancels the producer within 3 sthe engine server aborts requests the same way
A slow reader bounds the producerunita reader that reads nothing stops a 200,000-event producerbackpressure through a bounded channel
Bounded admission answers 429faulttwo slots: the third concurrent job gets 429 and Retry-After at once; slots come backthe engine server’s 429
SIGTERM drains and exits 0faultSIGTERM during a job: the job finishes, exit 0rolling deploys

The mistakes that most often break these cases are listed here with their symptoms and the line of the solution that prevents each:

PitfallSymptomHow the solution avoids it
std::thread::sleep in a handlerother requests on that worker stall; the 429 arrives latetokio::time::sleep(..).await in /jobs (step 6)
Collecting the whole stream before respondingthe first event arrives with the lastthe body is a ChannelBody; the producer is spawned and the response returned at once
An unbounded channel for a streama slow client makes the producer run to the end, memory growingmpsc::channel(CAPACITY) with CAPACITY = 4
Sleeping without watching closed()a gone client’s producer keeps ticking until its next sendselect! between sleep and tx.closed()
A semaphore acquire().await instead of try_acquireexcess jobs queue silently instead of 429try_acquire_owned in Admission::try_admit, 429 with Retry-After: 1
Exiting on SIGTERM without waiting for connectionsin-flight requests are cut; the client sees a resetserve drops the listener, then joins the JoinSet with a 10 s timeout
Holding a std::sync::Mutex guard across .awaitthe future is not Send, or a deadlock under loadno mutex at all: shared state is atomics and a semaphore

Warning: Awaiting the producer inside the handler instead of spawning it looks harmless and deadlocks. The body is not polled until the handler returns, so the producer fills the 4-slot channel and waits forever on a reader that never starts.

This section leaned on Rust: ownership, traits, Result, cargo workspaces, std TCP for ownership, Arc, Send, and threads, and on HTTP/1.1, JSON, and Server-Sent Events from the wire up for HTTP/1.1 framing and SSE by hand: the same bytes, now produced by hyper.

In AI Engineering, tl-serve: OpenAI-compatible HTTP + SSE on tokio/hyper is the v1 server: hyper, one engine thread, an SSE task per stream fed by a channel, bounded admission, abort on disconnect, and drain on SIGTERM. Disaggregated prefill and decode and Serving metrics and tracing use the same tools for async KV transfer and telemetry exporters. Next in this chapter, Protocol Buffers and gRPC uses tonic, the gRPC library, which is hyper and tokio underneath.

Each hand-built piece has a production equivalent:

This pieceProduction equivalentWhat it addsWhere to look
hyper by handaxumrouting, extractors, middleware on the same hyperaxum::Router
a semaphore per routetower layersrate limits, timeouts, load shedding as composable servicestower::limit, tower::load_shed
select! and closed()tokio_util::sync::CancellationTokenone token cancels a tree of tasksthe tokio-util docs
a hand-written shutdownhyper-util’s graceful shutdowntracks connections and their in-flight requests for youhyper_util::server::graceful
  • future: a value implementing Future, which does nothing until polled and then returns Ready or Pending.
  • task: a future handed to the executor with tokio::spawn, scheduled independently on the worker threads.
  • executor: the part of the runtime that polls ready tasks on a pool of worker threads.
  • reactor: the part of the runtime that asks the OS which sockets are ready and wakes the waiting tasks.
  • bounded channel: a channel with a fixed capacity whose send waits while it is full.
  • backpressure: a slow consumer slowing its producer, so memory between them stays bounded.
  • semaphore permit: a value representing one slot; holding it holds the slot, dropping it frees it.
  • cancellation by drop: a dropped future stops at its last .await, running destructors and nothing after.
  • graceful shutdown: stop accepting, let in-flight work finish within a bound, then exit 0.
  • An async fn returns a future, a value that does nothing until an executor polls it; .await is a point where the task may pause and its thread runs another task.
  • A task never blocks its thread: a sleep is tokio::time::sleep(..).await, and CPU-heavy work goes to its own thread.
  • A bounded channel gives backpressure: send().await waits while the channel is full, so a slow reader slows the producer instead of growing memory.
  • Dropping is cancelling: when the client goes away, hyper drops the body, the receiver goes with it, and the producer sees send fail or closed() fire.
  • Bounded admission with try_acquire refuses with 429 instead of queueing, and the permit frees its slot on every path out.
  • Graceful shutdown is three steps: stop accepting, let in-flight work finish, exit 0.
  1. Why does the producer need tx.closed() in its select! if send already fails when the client is gone?
    Answer `send` fails only when it is called. With `interval_ms=60000` the producer would sleep a minute after the client left before trying to send and finding out. `closed()` completes when the receiver is dropped, so `select!` cancels the sleep immediately.
  2. Where exactly is the backpressure, and what would mpsc::unbounded_channel change?
    Answer In `tx.send(..).await`: with 4 events queued it returns `Pending` until hyper's `poll_frame` takes one, which happens only as the socket drains. An unbounded channel never pauses, so the producer runs to the end and every unsent event sits in memory.
  3. Why is the job’s slot released on every path out of the /jobs handler, including when the client disconnects mid-sleep?
    Answer The slot is the `OwnedSemaphorePermit` value. If the handler returns early, its local `permit` is dropped; if the client disconnects, hyper drops the handler's future at its `.await`, which drops its locals, including the permit. Dropping a permit returns it to the semaphore.
  4. Why does the solution tokio::spawn the producer instead of awaiting it in the handler?
    Answer The handler must return the `Response` so hyper can send headers and start polling the body. If it awaited the producer first, the producer would block on a full channel that nobody reads (the body is not being polled yet): with more than 4 events (counting `[DONE]`) it would deadlock, and with fewer the client would get everything at the end.
  5. After SIGTERM, what happens to a connection that is in the middle of a 600 ms job, and to a new connection attempt?
    Answer The loop breaks and drops the listener, so the new connection is refused. The running connection task is in the `JoinSet`; the server waits for it (up to 10 s), the job finishes and is answered, then `serve` returns and `main` exits 0.
  6. What exact bytes does GET /ticks?n=3&interval_ms=10 produce as the body, and how is the first event framed on the wire?
    Answer The body is `data: {"i":0}\n\ndata: {"i":1}\n\ndata: {"i":2}\n\ndata: [DONE]\n\n`. Each event is one chunk; the first is `f\r\ndata: {"i":0}\n\n\r\n`, where 0x0f = 15 is its byte length, and the stream ends with the terminal chunk `0\r\n\r\n`.