chenBright commented on code in PR #3780:
URL: https://github.com/apache/brpc/pull/3780#discussion_r4729469380
##########
src/brpc/ubshm/ub_endpoint.cpp:
##########
@@ -999,23 +1104,51 @@ int UBShmEndpoint::PollingModeInitialize(bthread_tag_t
tag,
poller_sids.emplace(op);
}
}
+ // Sids to forget after the iteration: an ADD/MOD and the REMOVE of
+ // a teardown come from different producers, so an op of a released
+ // endpoint can be applied after its REMOVE. Such an id must not
+ // stay in the set and be resolved again on every poll.
+ std::vector<SocketId> released_sids;
for (const auto& poller_sid : poller_sids) {
- SocketUniquePtr s;
- if (Socket::Address(poller_sid.sid, &s) < 0) {
+ SocketUniquePtr poller_socket;
+ if (Socket::Address(poller_sid.sid, &poller_socket) < 0) {
+ released_sids.push_back(poller_sid.sid);
continue;
}
- UBShmEndpoint* ep = static_cast<UBShmEndpoint*>(s->user());
- if (!ep) {
+ auto* user = static_cast<PollerUser*>(poller_socket->user());
+ if (user == nullptr) {
+ released_sids.push_back(poller_sid.sid);
+ continue;
+ }
+ // Resolve the endpoint from the owning Socket and keep that
+ // Socket referenced: the endpoint may be released concurrently
+ // by Reset() and must not be dereferenced directly.
+ SocketUniquePtr owner;
+ if (Socket::Address(user->owner_id, &owner) < 0) {
+ released_sids.push_back(poller_sid.sid);
+ continue;
+ }
+ auto* transport =
+ static_cast<UBShmTransport*>(owner->_transport.get());
+ UBShmEndpoint* ep = transport->GetUBShmEp();
+ UBRing* ring = nullptr;
+ if (!ep->AcquireRing(&ring)) {
+ released_sids.push_back(poller_sid.sid);
Review Comment:
The `_poller_sid` for this registration also needs to be checked here. The
old poller can pause after acquiring the synthetic socket, while a health check
can still reset and restore the owning socket in place. After the handshake,
the old `PollerUser::owner_id` can still resolve successfully, but
`AcquireRing()` returns the new ring. If the new registration lands on another
poller, both pollers will read the same ring and `read_buf()` concurrently.
Consider comparing `ep->_poller_sid` with `poller_sid.sid` after acquiring the
ring reference; if they do not match, release the reference and remove the
stale entry.
##########
test/brpc_ubring_unittest.cpp:
##########
@@ -992,6 +1098,140 @@ TEST(UBRingRecvChunkHeaderTest,
crafted_chunk_is_rejected) {
chunk.header[UBR_MSG_LEN_INDEX], chunk.header[UBR_MSG_CUR_INDEX]));
}
+// The ring is read by the poller and written by the sender while Reset() may
+// run in a health-check bthread, so the endpoint only hands it out until it is
+// released and closes it only after its last user returns it.
+TEST_F(UBShmEndpointOneTrxTest, ring_lifetime_is_bounded_by_its_users) {
+ GFLAGS_NAMESPACE::FlagSaver flag_saver;
+ brpc::ubring::FLAGS_ub_disconnect_timeout_s = 0;
+ brpc::ubring::UBRing* ring = nullptr;
+ EXPECT_FALSE(_ep->AcquireRing(&ring));
+ EXPECT_FALSE(_ep->IsWritable());
+ butil::IOBuf buf;
+ butil::IOBuf* blocks[] = {&buf};
+ errno = 0;
+ EXPECT_EQ(-1, _ep->CutFromIOBufList(blocks, 1));
+ EXPECT_EQ(EPIPE, errno);
+
+ brpc::ubring::SHM local_trx_shm =
+ {nullptr, 4 * 1024 * 1024, 0, {0}, (uint32_t)_socket->fd()};
+ ASSERT_EQ(0, _ep->AllocateClientResources(&local_trx_shm,
"UBRING_ut_ring"));
Review Comment:
This name directly determines the actual IPC shared memory object name. When
the same test runs in parallel, the second process encounters `EEXIST` at
`shm_open(O_CREAT | O_EXCL)`. Consider using short names with a process ID and
sequence number, as in `ChurnEndpoints()`, for both `UBRING_ut_late` and
`UBRING_ut_op`.
##########
test/brpc_ubring_unittest.cpp:
##########
@@ -883,6 +884,111 @@ class UBShmEndpointOneTrxTest : public UBShmEndpointTest {
private:
GFLAGS_NAMESPACE::FlagSaver _flag_saver;
};
+
+// The teardown of a UBRing is asynchronous when a poller is still using it, so
+// a test that owns the only trx must wait for it before it allocates again or
+// finishes.
+bool WaitForTrxRelease() {
+ for (int i = 0; i < 5000; ++i) {
+ UbrTrx* trx = nullptr;
+ if (UBRingManager::AcquireUbrTrxFromMgr(&trx) == 0) {
+ UBRingManager::ReleaseUbrTrxFromMgr(trx);
+ return true;
+ }
+ bthread_usleep(1000);
+ }
+ return false;
+}
+
+// A trx slot that is free means the ring that owned it was closed. Unlike
+// WaitForTrxRelease() this does not wait, which lets a test check that a ring
+// was already closed when a call returned.
+bool TrxIsFree() {
+ UbrTrx* trx = nullptr;
+ if (UBRingManager::AcquireUbrTrxFromMgr(&trx) != 0) {
+ return false;
+ }
+ UBRingManager::ReleaseUbrTrxFromMgr(trx);
+ return true;
+}
+
+// Returns the endpoint of a Socket created in SOCKET_MODE_UBRING. The Socket
+// keeps the endpoint alive.
+UBShmEndpoint* EndpointOf(Socket* s) {
+ return static_cast<UBShmTransport*>(s->_transport.get())->GetUBShmEp();
+}
+
+// Holds the ring of an endpoint for a while, across the release of the
+// endpoint, like a poller or a sender does.
+struct RingUser {
+ UBShmEndpoint* ep{nullptr};
+ butil::atomic<bool> acquired{false};
+ butil::atomic<bool> released{false};
+ butil::atomic<int> writable{INT_MIN};
+};
+
+void HoldRing(RingUser* user) {
+ UBRing* ring = nullptr;
+ if (!user->ep->AcquireRing(&ring)) {
+ return;
+ }
+ user->acquired.store(true);
+ // The endpoint is released concurrently from the test thread: the ring
must
+ // stay usable until it is returned below.
+ bthread_usleep(20000);
+ user->writable.store(ring->IsUbrTrxWriteable(EPOLLET));
+ user->ep->ReleaseRing();
+ user->released.store(true);
+}
+
+// Creates and releases ubring endpoints from another thread, so that the
+// poller polls an endpoint while it is released.
+void ChurnEndpoints(int* failures) {
+ static std::atomic<uint32_t> name_sequence(0);
+ for (int i = 0; i < 200 && *failures == 0; ++i) {
+ if (!WaitForTrxRelease()) {
+ ++*failures;
+ break;
+ }
+ SocketOptions options;
+ options.socket_mode = SOCKET_MODE_UBRING;
+ SocketId id = INVALID_SOCKET_ID;
+ if (Socket::Create(options, &id) != 0) {
+ ++*failures;
+ break;
+ }
+ {
+ SocketUniquePtr s;
+ if (Socket::Address(id, &s) != 0) {
+ ++*failures;
+ continue;
+ }
+ UBShmEndpoint* ep = EndpointOf(s.get());
+ SHM local_trx_shm =
+ {nullptr, 4 * 1024 * 1024, 0, {0}, (uint32_t)s->fd()};
+ const std::string name = "UBRING_ut_churn_" +
+ std::to_string(getpid()) + "_" +
+ std::to_string(name_sequence.fetch_add(
+ 1, std::memory_order_relaxed));
+ if (ep->AllocateClientResources(&local_trx_shm, name.c_str()) !=
0) {
+ ++*failures;
+ continue;
+ }
+ // Let the poller poll the endpoint before it is released.
+ bthread_usleep(100);
Review Comment:
Sleeping for 100 microseconds does not guarantee that the poller has seen
this endpoint. `ADD` and `REMOVE` can both be dequeued in the same iteration,
leaving the registration set empty, while the transaction slot is still
released normally at the end. Consider making at least one iteration wait for a
signal that the poller has successfully borrowed the target ring before tearing
it down concurrently, so the test can catch regressions that skip the polling
path.
##########
test/brpc_ubring_unittest.cpp:
##########
@@ -992,6 +1098,140 @@ TEST(UBRingRecvChunkHeaderTest,
crafted_chunk_is_rejected) {
chunk.header[UBR_MSG_LEN_INDEX], chunk.header[UBR_MSG_CUR_INDEX]));
}
+// The ring is read by the poller and written by the sender while Reset() may
+// run in a health-check bthread, so the endpoint only hands it out until it is
+// released and closes it only after its last user returns it.
+TEST_F(UBShmEndpointOneTrxTest, ring_lifetime_is_bounded_by_its_users) {
+ GFLAGS_NAMESPACE::FlagSaver flag_saver;
+ brpc::ubring::FLAGS_ub_disconnect_timeout_s = 0;
+ brpc::ubring::UBRing* ring = nullptr;
+ EXPECT_FALSE(_ep->AcquireRing(&ring));
+ EXPECT_FALSE(_ep->IsWritable());
+ butil::IOBuf buf;
+ butil::IOBuf* blocks[] = {&buf};
+ errno = 0;
+ EXPECT_EQ(-1, _ep->CutFromIOBufList(blocks, 1));
+ EXPECT_EQ(EPIPE, errno);
+
+ brpc::ubring::SHM local_trx_shm =
+ {nullptr, 4 * 1024 * 1024, 0, {0}, (uint32_t)_socket->fd()};
+ ASSERT_EQ(0, _ep->AllocateClientResources(&local_trx_shm,
"UBRING_ut_ring"));
+ ASSERT_TRUE(_ep->AcquireRing(&ring));
+ ASSERT_NE(nullptr, ring);
+ EXPECT_EQ(ring, _ep->_ub_ring.load(butil::memory_order_relaxed));
+ _ep->ReleaseRing();
+
+ // Reset() must wait for the user of the ring: it returns only after the
+ // user returned the ring, and the ring is closed by then.
+ // A thread of its own: this thread blocks in Reset() below, which waits
+ // for the user, so both must be able to run at the same time.
+ brpc::ubring::RingUser user;
+ user.ep = _ep;
+ std::thread holder(brpc::ubring::HoldRing, &user);
+ while (!user.acquired.load()) {
+ bthread_usleep(1000);
+ }
+ _ep->Reset();
+ EXPECT_TRUE(user.released.load());
Review Comment:
`ReleaseRing()` and `released.store(true)` are two separate operations. If
the holding thread is preempted between them, a correct `Reset()` may already
have returned while this assertion still fails. A fixed sleep also allows the
holding thread to release the ring before `Reset()` starts. Consider having the
holding thread wait until `_ring_closed` is set before releasing the reference,
using `TrxIsFree()` to check the reset result, and reading the thread
completion flag after `join()`.
##########
test/brpc_ubring_unittest.cpp:
##########
@@ -883,6 +884,111 @@ class UBShmEndpointOneTrxTest : public UBShmEndpointTest {
private:
GFLAGS_NAMESPACE::FlagSaver _flag_saver;
};
+
+// The teardown of a UBRing is asynchronous when a poller is still using it, so
+// a test that owns the only trx must wait for it before it allocates again or
+// finishes.
+bool WaitForTrxRelease() {
+ for (int i = 0; i < 5000; ++i) {
+ UbrTrx* trx = nullptr;
+ if (UBRingManager::AcquireUbrTrxFromMgr(&trx) == 0) {
+ UBRingManager::ReleaseUbrTrxFromMgr(trx);
+ return true;
+ }
+ bthread_usleep(1000);
+ }
+ return false;
+}
+
+// A trx slot that is free means the ring that owned it was closed. Unlike
+// WaitForTrxRelease() this does not wait, which lets a test check that a ring
+// was already closed when a call returned.
+bool TrxIsFree() {
+ UbrTrx* trx = nullptr;
+ if (UBRingManager::AcquireUbrTrxFromMgr(&trx) != 0) {
+ return false;
+ }
+ UBRingManager::ReleaseUbrTrxFromMgr(trx);
+ return true;
+}
+
+// Returns the endpoint of a Socket created in SOCKET_MODE_UBRING. The Socket
+// keeps the endpoint alive.
+UBShmEndpoint* EndpointOf(Socket* s) {
+ return static_cast<UBShmTransport*>(s->_transport.get())->GetUBShmEp();
+}
+
+// Holds the ring of an endpoint for a while, across the release of the
+// endpoint, like a poller or a sender does.
+struct RingUser {
+ UBShmEndpoint* ep{nullptr};
+ butil::atomic<bool> acquired{false};
+ butil::atomic<bool> released{false};
+ butil::atomic<int> writable{INT_MIN};
+};
+
+void HoldRing(RingUser* user) {
+ UBRing* ring = nullptr;
+ if (!user->ep->AcquireRing(&ring)) {
+ return;
+ }
+ user->acquired.store(true);
+ // The endpoint is released concurrently from the test thread: the ring
must
+ // stay usable until it is returned below.
+ bthread_usleep(20000);
+ user->writable.store(ring->IsUbrTrxWriteable(EPOLLET));
+ user->ep->ReleaseRing();
+ user->released.store(true);
+}
+
+// Creates and releases ubring endpoints from another thread, so that the
+// poller polls an endpoint while it is released.
+void ChurnEndpoints(int* failures) {
+ static std::atomic<uint32_t> name_sequence(0);
+ for (int i = 0; i < 200 && *failures == 0; ++i) {
+ if (!WaitForTrxRelease()) {
+ ++*failures;
+ break;
+ }
+ SocketOptions options;
+ options.socket_mode = SOCKET_MODE_UBRING;
+ SocketId id = INVALID_SOCKET_ID;
+ if (Socket::Create(options, &id) != 0) {
+ ++*failures;
+ break;
+ }
+ {
+ SocketUniquePtr s;
+ if (Socket::Address(id, &s) != 0) {
+ ++*failures;
+ continue;
+ }
+ UBShmEndpoint* ep = EndpointOf(s.get());
+ SHM local_trx_shm =
+ {nullptr, 4 * 1024 * 1024, 0, {0}, (uint32_t)s->fd()};
+ const std::string name = "UBRING_ut_churn_" +
+ std::to_string(getpid()) + "_" +
+ std::to_string(name_sequence.fetch_add(
+ 1, std::memory_order_relaxed));
+ if (ep->AllocateClientResources(&local_trx_shm, name.c_str()) !=
0) {
+ ++*failures;
+ continue;
Review Comment:
The direct `continue` here skips the `SetFailed()` call at the end.
Destruction of the local `s` only releases the reference from `Address()`; the
owning socket still holds the extra reference acquired at creation, and some
resources already attached to the endpoint cannot be cleaned up. Please mark
the socket as failed in this error branch, or use scope cleanup for all exit
paths.
##########
test/brpc_ubring_unittest.cpp:
##########
@@ -992,6 +1098,140 @@ TEST(UBRingRecvChunkHeaderTest,
crafted_chunk_is_rejected) {
chunk.header[UBR_MSG_LEN_INDEX], chunk.header[UBR_MSG_CUR_INDEX]));
}
+// The ring is read by the poller and written by the sender while Reset() may
+// run in a health-check bthread, so the endpoint only hands it out until it is
+// released and closes it only after its last user returns it.
+TEST_F(UBShmEndpointOneTrxTest, ring_lifetime_is_bounded_by_its_users) {
+ GFLAGS_NAMESPACE::FlagSaver flag_saver;
+ brpc::ubring::FLAGS_ub_disconnect_timeout_s = 0;
+ brpc::ubring::UBRing* ring = nullptr;
+ EXPECT_FALSE(_ep->AcquireRing(&ring));
+ EXPECT_FALSE(_ep->IsWritable());
+ butil::IOBuf buf;
+ butil::IOBuf* blocks[] = {&buf};
+ errno = 0;
+ EXPECT_EQ(-1, _ep->CutFromIOBufList(blocks, 1));
+ EXPECT_EQ(EPIPE, errno);
+
+ brpc::ubring::SHM local_trx_shm =
+ {nullptr, 4 * 1024 * 1024, 0, {0}, (uint32_t)_socket->fd()};
+ ASSERT_EQ(0, _ep->AllocateClientResources(&local_trx_shm,
"UBRING_ut_ring"));
+ ASSERT_TRUE(_ep->AcquireRing(&ring));
+ ASSERT_NE(nullptr, ring);
+ EXPECT_EQ(ring, _ep->_ub_ring.load(butil::memory_order_relaxed));
+ _ep->ReleaseRing();
+
+ // Reset() must wait for the user of the ring: it returns only after the
+ // user returned the ring, and the ring is closed by then.
+ // A thread of its own: this thread blocks in Reset() below, which waits
+ // for the user, so both must be able to run at the same time.
+ brpc::ubring::RingUser user;
+ user.ep = _ep;
+ std::thread holder(brpc::ubring::HoldRing, &user);
+ while (!user.acquired.load()) {
+ bthread_usleep(1000);
+ }
+ _ep->Reset();
+ EXPECT_TRUE(user.released.load());
+ EXPECT_TRUE(brpc::ubring::TrxIsFree());
+ EXPECT_FALSE(_ep->AcquireRing(&ring));
+ EXPECT_FALSE(_ep->IsWritable());
+ holder.join();
+ EXPECT_NE(INT_MIN, user.writable.load());
+}
+
+// A sender may register EPOLLOUT after the endpoint stopped handing out the
+// ring. Such an op must not reach the poller: it can be applied after the
+// REMOVE of the teardown, which puts the socket id back into the poller set
+// and keeps it there.
+TEST_F(UBShmEndpointTest, late_registration_after_release_is_dropped) {
+ GFLAGS_NAMESPACE::FlagSaver flag_saver;
+ brpc::ubring::FLAGS_ub_disconnect_timeout_s = 0;
+ brpc::ubring::SHM local_trx_shm =
+ {nullptr, 4 * 1024 * 1024, 0, {0}, (uint32_t)_socket->fd()};
+ ASSERT_EQ(0, _ep->AllocateClientResources(&local_trx_shm,
"UBRING_ut_late"));
+ auto* poller = _ep->_poller.load();
+ ASSERT_NE(nullptr, poller);
+ brpc::ubring::UBShmEndpoint::PollerSidOp op;
+ while (poller->op_queue.Dequeue(op)) {
+ }
+
+ _ep->_ring_closed.store(true);
+ _ep->PollerRegisterEpollOut(false); // ADD
+ _ep->PollerRegisterEpollOut(true); // MOD
+ EXPECT_FALSE(poller->op_queue.TryDequeue(op));
+
+ // The REMOVE of the teardown itself still goes through.
+ _ep->PollerRegisterEvent(brpc::ubring::UBShmEndpoint::PollerSidOp::REMOVE);
+ ASSERT_TRUE(poller->op_queue.Dequeue(op));
+ EXPECT_EQ(brpc::ubring::UBShmEndpoint::PollerSidOp::REMOVE, op.type);
+ _ep->Reset();
+}
+
+// PollIn() drains the ring until it is empty, which a busy peer can postpone
+// for a long time. It must stop as soon as the endpoint is released, because
+// the teardown waits for the poller to leave the ring.
+TEST_F(UBShmEndpointTest, polling_stops_when_the_endpoint_is_released) {
+ _ep->_ring_closed.store(true);
+ // Returns before the ring is touched: a ring that must not be used is
+ // passed to prove that the closed flag is checked first.
+ _ep->PollIn(_ep, _socket, nullptr, EPOLLET);
+ _ep->_ring_closed.store(false);
+}
+
+// The poller of an endpoint is chosen when the event is added. Later ops must
+// use the same poller even when they run in a bthread of another poller group,
+// otherwise the stale socket id stays in the original poller set.
+TEST_F(UBShmEndpointTest, poller_of_add_serves_later_operations) {
+ GFLAGS_NAMESPACE::FlagSaver flag_saver;
+ brpc::ubring::FLAGS_ub_disconnect_timeout_s = 0;
+ brpc::ubring::SHM local_trx_shm =
+ {nullptr, 4 * 1024 * 1024, 0, {0}, (uint32_t)_socket->fd()};
+ ASSERT_EQ(0, _ep->AllocateClientResources(&local_trx_shm, "UBRING_ut_op"));
+
+ auto& pollers =
+
brpc::ubring::UBShmEndpoint::_poller_groups[bthread_self_tag()].pollers;
+ ASSERT_FALSE(pollers.empty());
+ brpc::ubring::UBShmEndpoint::Poller* derived =
+ &pollers[butil::fmix32(_ep->_poller_sid.load()) % pollers.size()];
+ ASSERT_EQ(derived, _ep->_poller.load());
+ brpc::ubring::UBShmEndpoint::PollerSidOp op;
+ while (derived->op_queue.Dequeue(op)) {
+ }
+
+ // Pretend that the events were added to another poller: a REMOVE must
+ // still be delivered there.
+ brpc::ubring::UBShmEndpoint::Poller other;
+ _ep->_poller.store(&other);
+ _ep->PollerRegisterEvent(brpc::ubring::UBShmEndpoint::PollerSidOp::REMOVE);
+ ASSERT_TRUE(other.op_queue.Dequeue(op));
Review Comment:
If this routing assertion fails, the test body returns early and destroys
`other`, but `_ep->_poller` still points to it. `TearDown()` then deletes the
endpoint and sends `REMOVE` to the destroyed queue. Please use scope cleanup to
restore the original poller even on an early return.
--
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]