← LeaderboardThe Skill Leaderboard
rust-tokio-cancellation-safe-io
Use when retries after a Tokio select! or timeout corrupt framed reads or writes because an interrupted operation already consumed bytes.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-tokio-cancellation-safe-iodescription: "Use when retries after a Tokio select! or timeout corrupt framed reads or writes because an interrupted operation already consumed bytes."category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-01"author: ShianMiketags: [rust, tokio, async-io, cancellation, select, framing]Rust Tokio Cancellation-Safe I/O
When to Use
- A stream frame loses or duplicates bytes after a time limit, shutdown signal, or another
tokio::select!branch wins. - A reader retries
read_exacton the same stream after cancelling the previous read. - A protocol needs to pause work and resume with the same connection and parsing state.
Prerequisites
- Identify the Tokio version, transport, framing rules, and exact operation that can be dropped.
- Find every path that reuses the connection after a cancellation. Verify who owns the reader, the buffer, and the count of bytes already assembled.
- Reproduce with a local partial-frame fixture before changing a production read loop.
1. Locate progress that can be lost on cancellation
- Inspect each composed read or write inside
tokio::select!or a timeout. A droppedread_exactcan have consumed bytes even though it never returned a complete frame. A droppedwrite_allcan have written a prefix. - Check the actual method's cancellation contract. Tokio documents
read,read_buf, andwriteas cancellation-safe operations in a select branch. A wrapper with additional awaits needs to preserve any progress it makes between them. - Decide whether to keep the connection for another attempt or close it. Resuming requires enough state to know where to continue. Reconnection and resending also need the protocol's duplicate-delivery rules.
2. Persist frame state across attempts
- Let one reader own the bytes and the number of bytes already filled outside the future that can be dropped. Read into the remaining part of the buffer and update the count immediately after each successful
read. - Yield a frame when all its bytes are available. Reset the count only after producing that frame. Check length limits before allocating buffers for variable-length formats.
- Treat a zero-byte read into a nonempty slice as EOF. Report clean EOF at a frame boundary and a truncated frame when any bytes are pending.
- For resumable writes, keep the payload and written offset and advance the offset by the completed
writeresult. A byte count confirms local write progress. Application-level delivery depends on the protocol.
3. Verify the cancellation path without sleeps
- Feed a known prefix through
tokio::io::duplexwhile leaving the write side open. Force cancellation after a read has had a chance to take that prefix. - Feed the suffix and the next frame. Assert the exact bytes of both frames and the EOF behavior, including a truncated frame.
- The fixture uses
biased;to control polling order. Evaluate fairness and shutdown latency separately for the production loop.
Example
Run the partial-frame cancellation fixture. It uses two four-byte frames with the cancellation point in the middle of the first.
First write AB
Cancel The read attempt has polled and is waiting for more bytes
Second write CDEFGH
Expected frames ABCD and EFGH
Limitations
- The fixture uses in-memory Tokio I/O and fixed-size frames. Socket handling, TLS, variable-length codec limits, and writes under backpressure require their own tests.
- Frame tracking addresses local byte progress. Recovery after a dropped connection also depends on peer state and protocol acknowledgements.
Safety and Side Effects
- The example uses synthetic data in memory. Cargo builds local artifacts in the scratch project and can read a package registry when filling its cache.
- Frame recovery changes stream handling. Test truncation and retry behavior with a disposable stream before using it on a live connection.
Related Skills and Documentation
- For task and channel patterns, see Rust Async Patterns.
- For general Rust development, see Rust Pro.
- Tokio documents select! cancellation safety, AsyncReadExt, and duplex streams.