Make _state atomic to prevent concurrent _read_buf mutation on TCP fa…#3347
Open
chenBright wants to merge 1 commit into
Open
Make _state atomic to prevent concurrent _read_buf mutation on TCP fa…#3347chenBright wants to merge 1 commit into
chenBright wants to merge 1 commit into
Conversation
…llback RdmaEndpoint::_state was a plain enum, written by the handshake bthread and read concurrently by the event-dispatching thread (OnNewDataFromTcp). This is a data race, and on a weak memory model it can let the two threads concurrently mutate _socket->_read_buf. Make _state a butil::atomic<State>: - Terminal-state stores use release and the matching loads use acquire, so data published before a terminal state (the magic bytes put back into _read_buf, and the RDMA window/resource setup before ESTABLISHED) is visible to the reader. - Non-terminal handshake transitions use relaxed.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a concurrency data race in the RDMA/TCP handshake path by making RdmaEndpoint::_state atomic and using acquire/release semantics for terminal handshake transitions, preventing concurrent mutation of Socket::_read_buf during fallback/establishment.
Changes:
- Changed
RdmaEndpoint::_statefrom a plainStateenum tobutil::atomic<State>. - Updated all reads/writes of
_stateto useload()/store()with appropriate memory orders (release for terminal publishes, acquire for corresponding reads). - Minor refactors/formatting adjustments around handshake logic (e.g.,
rdma_onflag computation, line wrapping).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/brpc/rdma/rdma_endpoint.h | Makes _state an atomic and adjusts TryReadOnTcp declaration to match new usage patterns. |
| src/brpc/rdma/rdma_endpoint.cpp | Converts _state accesses to atomic load/store and adds acquire/release ordering to prevent handshake/TCP read buffer races. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
RdmaEndpoint::_stateis a plain (non-atomic) enum, but it is writtenby the RDMA handshake bthread and read concurrently by the
event dispatcher bthread in
OnNewDataFromTcp. This is a data race,and on a weak memory model (ARM / POWER / RISC-V) it can let the two
threads concurrently mutate
_socket->_read_bufand corrupt anIOBuf.What is changed and the side effects?
Changed:
Make
_stateabutil::atomic<State>.memory_order_releaseand the corresponding loads usememory_order_acquire, so that everything published before a terminal state becomesvisible to the reader:
memory_order_relaxed.Side effects:
Performance effects:
Breaking backward compatibility:
Check List: