zchuango commented on code in PR #3509:
URL: https://github.com/apache/brpc/pull/3509#discussion_r4229601137
##########
src/brpc/ubshm/ub_ring.cpp:
##########
@@ -89,25 +235,53 @@ RETURN_CODE UBRing::UbrTrxClose() {
LOG(WARNING) << "Local shm " << _trx->local_shm.name
<< " wait for the peer to close timed out, force cleanup.";
_trx->ubr_rx.trx_state = UBR_STATE_CLOSED;
- // Force synchronous cleanup instead of relying on async timer
- DeleteTimerSafe((uint32_t)_trx->timer_fd);
- DeleteTimerSafe((uint32_t)_trx->hb_timer_fd);
- if (_trx->ubr_tx.remote_rx_event_q.addr != nullptr) {
- ((UbrEventQMsg *)_trx->ubr_tx.remote_rx_event_q.addr)->flag =
UBR_STATE_CLOSED;
+ // Wait out the close/heartbeat callbacks, which may schedule a
+ // delayed cleanup, then settle the cleanup ownership: force
+ // runs the cleanup itself when it can claim it, and leaves it
+ // to an already running delayed-clear callback otherwise.
+ UbrTimerDelAndWait(&_trx->close_timer);
+ UbrTimerDelAndWait(&_trx->hb_timer);
+ UbrCleanupCtl* ctl =
UBRingManager::SnapshotUnitCleanupCtl(_trx->trx_mgr_index);
+ if (ctl != nullptr && ctl->ubr_id != expect_ubr_id) {
+ ctl->ReleaseRef(); // snapshot reference
+ ctl = nullptr; // slot reused, not ours
}
- if (UNLIKELY(UbrTrxFreeShm(_trx) != UBRING_OK)) {
- LOG(WARNING) << "Force close, local shm " <<
_trx->local_shm.name << " free failed.";
+ bool cleanup_owned = false;
+ if (ctl != nullptr) {
+ int expected = UBR_CLEANUP_PENDING;
+ if (ATOMIC_COMPARE_EXCHANGE_STRONG(ctl->state, expected,
UBR_CLEANUP_RUNNING)) {
+ cleanup_owned = true;
+ if (UbrTimerDel(&ctl->timer) == 0) {
+ ctl->ReleaseRef(); // timer/callback reference
+ }
+ }
+ } else if (ATOMIC_LOAD(_trx->ubr_id) == expect_ubr_id) {
+ cleanup_owned = true;
}
Review Comment:
The snapshot and forced-cleanup claim now happen under the same manager
lock. Once force-close claims cleanup, a later attempt to publish delayed
cleanup is rejected.
--
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]