Socket errors and timeouts
Quick answer
Socket operations return I/O errors; a timeout limits how long an operation may block, not necessarily the total duration of a request.
Classify failures and bound the deadline
A blocking read timeout may surface as WouldBlock or TimedOut depending on the OS. Interrupted can be retried, but a permanent error needs reporting. EOF in the middle of a frame is a protocol failure. Use an overall deadline as well as per-operation timeouts; an adversary could otherwise send small pieces indefinitely. A failed write may have sent some bytes, so retries must account for partial progress.
use std::io::ErrorKind::*;fn main() { for k in [TimedOut, WouldBlock, Interrupted, ConnectionReset] { println!("{}", match k { TimedOut | WouldBlock => "wait expired", Interrupted => "interrupted", _ => "connection failed" }); }}Expected stdout (not executed):
wait expiredwait expiredinterruptedconnection failedTreat WouldBlock as a timeout only in the appropriate blocking-timeout context.
use std::time::Duration;fn main() { let budget = Duration::from_secs(5); for spent in [2, 5, 6] { println!("{}", budget.saturating_sub(Duration::from_secs(spent)).as_secs()); }}Expected stdout (not executed):
300A real implementation measures elapsed time using Instant.
Common mistake
Do not retry every failure forever or restart the entire deadline on each partial read.
Try it yourself
Model a five-second deadline with three attempts and refuse a fourth after exhaustion.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Socket errors and timeouts — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A blocking read returns Interrupted. Which policy is reasonable?
Socket errors and timeouts — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::io::ErrorKind::*; fn main() { for k in [TimedOut, WouldBlock, Interrupted, ConnectionReset] { println!("{}", match k { TimedOut | WouldBlock => "wait expired", Interrupted => "interrupted", _ => "connection failed" }); } } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Socket errors and timeouts — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::time::Duration; fn main() { let budget = Duration::from_secs(5); for spent in [2, 5, 6] { println!("{}", budget.saturating_sub(Duration::from_secs(spent)).as_secs()); } } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.