Repository navigation
Conversation
PollIn() dereferences ep->_ub_ring in the polling bthread while Reset() frees the ring right after enqueueing a fire-and-forget REMOVE op, so the poller can read the released ring. PollerRegisterEvent() also looks up the poller group with bthread_self_tag() at REMOVE time, which may be a different group than the one that saw ADD, leaving a stale socket id in the first group's set. - Choose the poller of _poller_sid when the events are added and reuse it for MOD/REMOVE. - Resolve the endpoint from the owning Socket through a SocketUser kept in the synthetic poller Socket, and hold that Socket while polling, so the endpoint cannot be released under the poller. - Reference count the ring: DeallocateResources() stops handing it out and the last user destroys it. The trx is closed only when nobody uses the ring, and the close, which may wait for the peer, does not run in the poller when the poller is the last user. - Pass the acquired ring to PollIn()/PollOut() so they cannot observe a replaced _ub_ring, and let IsWritable()/CutFromIOBufList() borrow it the same way instead of dereferencing a null or stale pointer. Tests: ring lifetime across Reset()/release, event routing to the poller recorded at ADD time, and a stress test that churns endpoints while the poller polls them.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Several unresolved moderate and critical findings remain, including leak-prone ownership transfer, blocking cleanup on the poller path, unsafe ring retirement and acquisition races, and untracked shutdown helpers that can access torn-down global resources.
5 open issues
Inline fallback cleanup can block the UBRing poller · New Untracked destroy helpers can race global manager shutdown · New Ring acquire can race teardown and return a destroyed ring · New New allocation overwrites an unreclaimed retired ring · New PollerUser leaks when socket initialization fails before attachment · New
What changed in this PR
This PR hardens UBRing endpoint teardown against concurrent polling, sending, and handshake activity while keeping poller operations associated with the poller selected during registration.
Changes:
- Adds reference-counted ring acquisition and deferred destruction during teardown.
- Resolves poller endpoints through retained owning sockets and records stable poller ownership.
- Adds tests covering ring lifetime, poller routing, and endpoint teardown races.
| File | Description |
|---|---|
| test/brpc_ubring_unittest.cpp | Adds tests for ring lifetime bounds, poller operation routing, and endpoint teardown under concurrent polling. |
| src/brpc/ubshm/ub_endpoint.h | Declares ring reference-management APIs, poller ownership metadata, and updated polling interfaces. |
| src/brpc/ubshm/ub_endpoint.cpp | Implements ring lifetime coordination, socket-based poller endpoint resolution, stable poller routing, and safer polling and teardown paths. |
Review effort: Lite
Comment on lines
+231
to
+234
| if (bthread_start_background(&tid, &attr, DestroyRingInBthread, ring) != 0) { | ||
| PLOG(WARNING) << "Fail to start bthread to destroy the ring"; | ||
| DestroyRing(ring); | ||
| } |
Comment on lines
+228
to
+234
| bthread_t tid; | ||
| bthread_attr_t attr = BTHREAD_ATTR_NORMAL; | ||
| bthread_attr_set_name(&attr, "UBDestroyRing"); | ||
| if (bthread_start_background(&tid, &attr, DestroyRingInBthread, ring) != 0) { | ||
| PLOG(WARNING) << "Fail to start bthread to destroy the ring"; | ||
| DestroyRing(ring); | ||
| } |
Comment on lines
+237
to
+246
| bool UBShmEndpoint::AcquireRing(UBRing** ring) { | ||
| _ring_ref.fetch_add(1, butil::memory_order_seq_cst); | ||
| // The acquire load pairs with the release store in | ||
| // AllocateClientResources()/AllocateServerResources(), which publish the | ||
| // ring before they hand it out. | ||
| if (_ring_closed.load(butil::memory_order_acquire)) { | ||
| ReleaseRing(); | ||
| return false; | ||
| } | ||
| *ring = _ub_ring.load(butil::memory_order_acquire); |
Comment on lines
+984
to
+985
| _ring_retired.store(ring, butil::memory_order_seq_cst); | ||
| if (_ring_ref.load(butil::memory_order_seq_cst) != 0) { |
Comment on lines
+918
to
923
| // The poller resolves the endpoint from the owning Socket, see PollerUser. | ||
| options.user = new PollerUser(_socket_id); | ||
| options.keytable_pool = _socket->_keytable_pool; | ||
| if (Socket::Create(options, &_poller_sid) < 0) { | ||
| PLOG(WARNING) << "Fail to create socket for UBRing poller"; | ||
| return -1; |
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: N/A
Problem Summary:
The UBRing poller dereferences
ep->_ub_ringinPollIn(), butUBShmEndpoint::Reset()destroyed that ring as soon as it had enqueued a fire-and-forgetREMOVEop, without waiting for the poller to stop using it. A poller that was insidePollIn()for the same endpoint then read the releasedUBRingand its shared memory. In addition,PollerRegisterEvent()picked the poller group withbthread_self_tag()on every call, so aMOD/REMOVEissued from a different bthread than theADDcould be delivered to another group, leaving the socket id in the first poller's set forever.IsWritable()also dereferenced_ub_ringunconditionally, so a concurrent reset made it dereference a null (or stale) pointer.What is changed and the side effects?
Changed:
PollerRegisterEvent()records the poller that serves_poller_sidwhen the event is added and reuses it for every laterADD/MOD/REMOVE, so an endpoint is always added to and removed from the same op queue regardless of the bthread tag.UBShmEndpointas itsSocketUser. It stores a smallPollerUserwith the id of the owningSocket; the poller resolves the endpoint throughSocket::Address()and keeps thatSocketreferenced while polling, so the endpoint and its transport cannot be released under the poller.AcquireRing()/ReleaseRing()mark it in use (poller, sender, handshake),DeallocateResources()takes it out of the endpoint and marks it closed, and the last user destroys it. The trx is therefore closed only when nobody uses the ring any more; when the poller is the last user, the close (which may wait for the peer to acknowledge) is moved to a helper bthread so polling is never blocked on it. When no user is in flight the ring is closed inline, as before.PollIn()/PollOut()receive the ring that the caller acquired instead of followingep->_ub_ring, andIsWritable()/CutFromIOBufList()borrow the ring the same way, so they never observe a null or replaced pointer and report "not writable"/EPIPEonce the endpoint is released.UbrMapRemoteShm()/UbrUnlinkLocalShm()calls use the same acquire/release path.Side effects:
Socket::Address()calls per poll is the same as before, sincePollIn()used to resolve the endpoint's socket itself.UBShmEndpoint::IsWritable()is no longerconst(it now borrows the ring); it is internal to the ubshm transport.Check List:
cmake -B build -DBUILD_UNIT_TESTS=ON -DDOWNLOAD_GTEST=OFF -DBRPC_SYSTEM_GTEST_SOURCE_DIR=/usr/src/googletest -DWITH_UBRING=ON+make -j6 brpc_ubring_unittest brpc-static: OKbrpc_ubring_unittestbuilds and the disabled-path test passes.make -j6 brpc_ubring_unittest && test/brpc_ubring_unittest: 36/36 pass (also 36/36 with-DWITH_ASAN=ON, no ASAN report; the only leak reported is the pre-existingg_ubring_resource_mutexfromGlobalInitialize()).ring_lifetime_is_bounded_by_its_users(a ring in use is not handed out nor destroyed byReset(), and its trx is released only after the last user returns it),poller_of_add_serves_later_operations(REMOVEreaches the poller recorded atADDeven when it is not the poller derived from the current bthread tag;Reset()forgets the registration), andpoller_survives_endpoint_teardown(a bthread churns create/poll/reset/destroy of endpoints while theUBPollingbthread polls them, waiting for each trx to come back).UBRing/trx objects.🤖 This PR was automatically created by brpc-oncall