wwbmmm commented on code in PR #3780:
URL: https://github.com/apache/brpc/pull/3780#discussion_r4717613310
##########
src/brpc/ubshm/ub_endpoint.cpp:
##########
@@ -193,15 +193,80 @@ UBShmEndpoint::~UBShmEndpoint() {
}
void UBShmEndpoint::Reset() {
+ // The ring is destroyed by DeallocateResources() or by the poller that
+ // still uses it.
DeallocateResources();
- delete _ub_ring;
- _ub_ring = nullptr;
_poller_sid = INVALID_SOCKET_ID;
+ _poller = nullptr;
_negotiated_data_format = UBR_DATA_FORMAT_NONE;
_state = UNINIT;
}
+// `ring' is not used any more, either because the endpoint never handed it to
+// a poller or because its last user just returned it.
+static void DestroyRing(UBRing* ring) {
+ if (ring == nullptr) {
+ return;
+ }
+ ring->UbrTrxClose();
+ delete ring;
+}
+
+static void* DestroyRingInBthread(void* arg) {
+ DestroyRing(static_cast<UBRing*>(arg));
+ return nullptr;
+}
+
+// Closing the trx may wait for the peer to acknowledge the close, which must
+// not happen in the poller or in a callback of the ring: hand the ring to a
+// bthread instead.
+static void DestroyRingLater(UBRing* ring) {
+ if (ring == nullptr) {
+ return;
+ }
+ 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);
+ }
+}
+
+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);
Review Comment:
[replied by brpc-oncall robot] Correct, that ordering was too weak. The
counter and the closed flag are now one protocol with sequential consistency on
both sides: `AcquireRing()` does `_ring_ref++` and then loads `_ring_closed`,
`DeallocateResources()` stores `_ring_closed = true` and then loads
`_ring_ref`. A concurrent acquire is therefore either counted before the
teardown reads the counter (and the teardown waits for it), or it observes the
closed flag and bails out. The reasoning is documented next to `AcquireRing()`.
##########
src/brpc/ubshm/ub_endpoint.cpp:
##########
@@ -844,53 +940,62 @@ 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_relaxed);
SocketOptions options;
- options.user = this;
+ // 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;
}
- int ret = _ub_ring->UbrAllocateServerShm(
+ 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_release);
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;
}
+ // The poller may be inside PollIn() of this endpoint right now, so the
+ // ring is not destroyed here but by its last user.
+ _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) {
+ _ring_retired.store(ring, butil::memory_order_seq_cst);
+ if (_ring_ref.load(butil::memory_order_seq_cst) != 0) {
Review Comment:
[replied by brpc-oncall robot] `_ring_retired` is gone.
`DeallocateResources()` waits until no one uses the ring and then destroys it
itself, so there is no teardown state that a new allocation could overwrite
while the previous ring is still in flight.
##########
src/brpc/ubshm/ub_endpoint.cpp:
##########
@@ -817,21 +909,25 @@ int UBShmEndpoint::AllocateClientResources(ubring::SHM*
local_trx_shm, const cha
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_relaxed);
SocketOptions options;
- options.user = this;
+ // 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;
Review Comment:
[replied by brpc-oncall robot] Fixed: the synthetic poller socket is created
without a user, and `PollerUser` is attached with `Socket::set_user()` only
after `Socket::Create()` succeeded, so a failure before `_user` is assigned
cannot leak it.
--
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]