← 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.

Published by sickn33 ↗sickn33/agentic-awesome-skills

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_exact on 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

  1. Inspect each composed read or write inside tokio::select! or a timeout. A dropped read_exact can have consumed bytes even though it never returned a complete frame. A dropped write_all can have written a prefix.
  2. Check the actual method's cancellation contract. Tokio documents read, read_buf, and write as cancellation-safe operations in a select branch. A wrapper with additional awaits needs to preserve any progress it makes between them.
  3. 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

  1. 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.
  2. 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.
  3. 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.
  4. For resumable writes, keep the payload and written offset and advance the offset by the completed write result. A byte count confirms local write progress. Application-level delivery depends on the protocol.

3. Verify the cancellation path without sleeps

  1. Feed a known prefix through tokio::io::duplex while leaving the write side open. Force cancellation after a read has had a chance to take that prefix.
  2. Feed the suffix and the next frame. Assert the exact bytes of both frames and the EOF behavior, including a truncated frame.
  3. 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