Home / Market / rust tokio cancellation safe io

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.

skill★ 509080 dlgithubCC-BY-4.0 content; MIT code; upstream exceptions applyInstall in CosmosUpstream Source

Documentation & Preview

--- name: rust-tokio-cancellation-safe-io description: "Use when retries after a Tokio select! or timeout corrupt framed reads or writes because an interrupted operation already consumed bytes." category: development risk: critical source: self source_type: self date_added: "2026-12-01" author: ShianMike tags: [rust, tokio, async-io, cancellation, select, framing]

---

Rust Tokio Cancellation-Safe I/O

When to Use

Prerequisites

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

Safety and Side Effects

Related Skills and Documentation