Copilot commented on code in PR #3780:
URL: https://github.com/apache/brpc/pull/3780#discussion_r4717670210


##########
src/brpc/ubshm/ub_endpoint.cpp:
##########
@@ -844,53 +917,70 @@ int UBShmEndpoint::AllocateServerResources(ubring::SHM* 
remote_trx_shm,
         return 0;
     }
 
-    CHECK(_ub_ring == nullptr);
+    CHECK(_ub_ring.load(butil::memory_order_relaxed) == nullptr);
     // TODO: Pooling management
-    _ub_ring = new UBRing();
+    UBRing* ring = new UBRing();
+    _ub_ring.store(ring, butil::memory_order_seq_cst);
 
     SocketOptions options;
-    options.user = this;
     options.keytable_pool = _socket->_keytable_pool;
     if (Socket::Create(options, &_poller_sid) < 0) {
         PLOG(WARNING) << "Fail to create socket for UBRing poller";
         return -1;
     }
-    int ret = _ub_ring->UbrAllocateServerShm(
+    // The poller resolves the endpoint from the owning Socket, see PollerUser.
+    // Attach the user only now: Socket::Create() may fail before it takes
+    // ownership of the user, which would leak it.
+    SocketUniquePtr poller_socket;
+    CHECK_EQ(0, Socket::Address(_poller_sid, &poller_socket));
+    poller_socket->set_user(new PollerUser(_socket_id));
+    int ret = ring->UbrAllocateServerShm(
         remote_trx_shm, local_trx_shm, format);
     if (ret != 0) {
         return ret;
     }
+    // Publish the ring to the poller before the ADD below reaches it.
+    _ring_closed.store(false, butil::memory_order_seq_cst);
     PollerRegisterEvent(PollerSidOp::ADD, EPOLLIN);
     return ret;
 }
 
 void UBShmEndpoint::DeallocateResources() {
-    if (!_ub_ring) {
+    // Take the ring out first so that nobody starts to use it any more.
+    UBRing* ring = _ub_ring.exchange(nullptr, butil::memory_order_seq_cst);
+    if (ring == nullptr) {
         return;
     }
+    // Stop handing the ring out and remove _poller_sid from the poller set, so
+    // that the poller stops polling this endpoint.
+    _ring_closed.store(true, butil::memory_order_seq_cst);
     PollerRegisterEvent(PollerSidOp::REMOVE);
-    _ub_ring->UbrTrxClose();
     if (INVALID_SOCKET_ID != _poller_sid) {
         SocketUniquePtr s;
         if (Socket::Address(_poller_sid, &s) == 0) {
-            s->_user = nullptr;
             s->_fd = -1;
             s->SetFailed();
         }
     }
-}
-
-void UBShmEndpoint::PollIn(UBShmEndpoint* ep, uint32_t ep_event) {
-    SocketUniquePtr s;
-    if (Socket::Address(ep->_socket_id, &s) < 0) {
-        return;
+    // A poller or a sender may be inside the ring right now: closing the trx
+    // invalidates the shared memory it reads, so wait for it to leave. Such a
+    // user never releases the endpoint by itself (the poller keeps the owning
+    // Socket referenced and the other users do not touch it), so the wait
+    // ends.
+    int ref = _ring_ref->load(butil::memory_order_seq_cst);
+    while (ref != 0) {
+        // Returns early when the counter changed or on a signal: re-check.
+        bthread::butex_wait(_ring_ref, ref, nullptr);
+        ref = _ring_ref->load(butil::memory_order_seq_cst);

Review Comment:
   `DeallocateResources()` now waits here until every poller reference is 
released, but a poller holds that reference across the entire `PollIn()` drain. 
`PollIn()` has an unbounded readable loop, so a continuously busy ring can keep 
returning data without reaching `EAGAIN`; in that case the poller never 
releases the reference, `REMOVE` is never able to stop it, and the 
health-check/reset thread can block indefinitely. Bound each poller's drain (or 
otherwise make teardown able to interrupt/release the poll operation) before 
waiting synchronously for the last reference.



-- 
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