Skip to content

Conversation

@quantpoet
Copy link

Replaced the racy load-then-store pattern with atomic compare_exchange to eliminate the potential race condition.

This ensures the non-blocking flag is set atomically, preventing multiple threads from simultaneously calling set_nonblocking on the same file descriptor.

Signed-off-by: quantpoet <quantway@outlook.com>
@tgross35
Copy link

This ensures the non-blocking flag is set atomically, preventing multiple threads from simultaneously calling set_nonblocking on the same file descriptor.

Why is this a problem? It looks like an idempotent fcntl call. Also, wouldn't swap be sufficient?

@tgross35
Copy link

Worse actually: with this PR, thread A can swap is_non_blocking from false to true and start fnctl. Thread B may observe is_non_blocking = true before thread A's syscall completes and assume the FIFO is O_NONBLOCK, even though that isn't correct.

The current implementation ensures O_NONBLOCK is set before continuing.

@quantpoet
Copy link
Author

Worse actually: with this PR, thread A can swap is_non_blocking from false to true and start fnctl. Thread B may observe is_non_blocking = true before thread A's syscall completes and assume the FIFO is O_NONBLOCK, even though that isn't correct.

The current implementation ensures O_NONBLOCK is set before continuing.

Oh indeed. Thanks for your reply.

@quantpoet quantpoet closed this Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants