Prompt
Avoid common concurrency anti-patterns that lead to race conditions, deadlocks, and unreliable behavior. Use structured approaches instead of ad-hoc solutions.
Key anti-patterns to avoid:
- Manual buffer management with select! - Can cause race conditions where data is lost
- Sleep-based synchronization - Creates timing dependencies and potential bugs
- Wrong synchronization primitives - Using Mutex when RwLock is needed blocks concurrent readers
- Improper lock lifetime management - Forgetting to release locks can cause deadlocks
Preferred approaches:
- Use streams for async data processing instead of manual buffer handling
- Choose RwLock over Mutex when multiple concurrent readers are expected
- Explicitly manage lock lifetimes with
drop()when needed - Avoid timing-based solutions like sleep delays for synchronization
Example:
// Anti-pattern: Manual buffer management with select!
select! {
n = stdout_reader.read_until(b'\n', &mut stdout_buf) => {
// Risk of losing last chunk of data
}
}
// Better: Use streams
let stdout_stream = LinesStream::new(BufReader::new(stdout).lines());
let stderr_stream = LinesStream::new(BufReader::new(stderr).lines());
// Drive streams to completion without manual buffer management
// Anti-pattern: Wrong lock type
pub(super) extension_manager: Mutex<ExtensionManager>, // Blocks concurrent readers
// Better: Allow concurrent reads
pub(super) extension_manager: RwLock<ExtensionManager>, // Multiple readers, single writer