wwbmmm opened a new pull request, #3780:
URL: https://github.com/apache/brpc/pull/3780

   ### What problem does this PR solve?
   
   Issue Number: N/A
   
   Problem Summary:
   
   The UBRing poller dereferences `ep->_ub_ring` in `PollIn()`, but 
`UBShmEndpoint::Reset()` destroyed that ring as soon as it had enqueued a 
fire-and-forget `REMOVE` op, without waiting for the poller to stop using it. A 
poller that was inside `PollIn()` for the same endpoint then read the released 
`UBRing` and its shared memory. In addition, `PollerRegisterEvent()` picked the 
poller group with `bthread_self_tag()` on every call, so a `MOD`/`REMOVE` 
issued from a different bthread than the `ADD` could be delivered to another 
group, leaving the socket id in the first poller's set forever. `IsWritable()` 
also dereferenced `_ub_ring` unconditionally, 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_sid` when 
the event is added and reuses it for every later `ADD`/`MOD`/`REMOVE`, so an 
endpoint is always added to and removed from the same op queue regardless of 
the bthread tag.
   - The synthetic poller socket no longer stores the `UBShmEndpoint` as its 
`SocketUser`. It stores a small `PollerUser` with the id of the owning 
`Socket`; the poller resolves the endpoint through `Socket::Address()` and 
keeps that `Socket` referenced while polling, so the endpoint and its transport 
cannot be released under the poller.
   - The ring is reference counted: `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 following `ep->_ub_ring`, and `IsWritable()`/`CutFromIOBufList()` borrow the 
ring the same way, so they never observe a null or replaced pointer and report 
"not writable"/`EPIPE` once the endpoint is released.
   - The handshake's `UbrMapRemoteShm()`/`UbrUnlinkLocalShm()` calls use the 
same acquire/release path.
   
   Side effects:
   - Performance effects: the poller takes one additional reference count per 
poll of an endpoint (a pair of atomics on a cacheline that only the teardown 
path writes); the number of `Socket::Address()` calls per poll is the same as 
before, since `PollIn()` used to resolve the endpoint's socket itself.
   - Breaking backward compatibility: none. `UBShmEndpoint::IsWritable()` is no 
longer `const` (it now borrows the ring); it is internal to the ubshm transport.
   
   ---
   ### Check List:
   - Please make sure your changes are compilable.
     - `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` : OK
     - default configuration (UBRING off): `brpc_ubring_unittest` builds and 
the disabled-path test passes.
   - Tests:
     - `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-existing `g_ubring_resource_mutex` from `GlobalInitialize()`).
     - New tests: `ring_lifetime_is_bounded_by_its_users` (a ring in use is not 
handed out nor destroyed by `Reset()`, and its trx is released only after the 
last user returns it), `poller_of_add_serves_later_operations` (`REMOVE` 
reaches the poller recorded at `ADD` even when it is not the poller derived 
from the current bthread tag; `Reset()` forgets the registration), and 
`poller_survives_endpoint_teardown` (a bthread churns create/poll/reset/destroy 
of endpoints while the `UBPolling` bthread polls them, waiting for each trx to 
come back).
     - Not covered by the container: there is no UB hardware/UDK here, so 
UBRing runs on the built-in IPC shared-memory backend. An end-to-end 
reproduction with a real ubring session (a client that drops the TCP fd and 
stops acknowledging the close) still has to be checked manually on UB hardware; 
the unit tests exercise the same poller/teardown paths with real `UBRing`/trx 
objects.
   
   ---
   🤖 This PR was automatically created by brpc-oncall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to