From abcee83fc752ce5903279fb9c9981919771fc357 Mon Sep 17 00:00:00 2001 From: quantpoet Date: Fri, 19 Sep 2025 19:17:43 +0800 Subject: [PATCH] Fix atomicity violation in try_acquire by using compare_exchange Signed-off-by: quantpoet --- src/unix.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/unix.rs b/src/unix.rs index 2758e31..d0f6e05 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -291,9 +291,12 @@ impl Client { let mut fifo = &self.read; if let Some(is_non_blocking) = self.is_non_blocking.as_ref() { - if !is_non_blocking.load(Ordering::Relaxed) { + // Use compare_exchange to atomically check and set the flag + if is_non_blocking + .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) + .is_ok() + { set_nonblocking(fifo.as_raw_fd(), true)?; - is_non_blocking.store(true, Ordering::Relaxed); } } else { return Err(io::ErrorKind::Unsupported.into());