fix: a shared resource must be able to release its waiters
SharedResource::acquire() blocks on a condition variable whose predicate only becomes true when release() hands over ownership. No timeout, no stop condition. A node parked there was not observing stop flags, so teardown had no way to reach it: the worker never returned, the pool's join never completed, and shutdown waited on a resource nobody was going to release — which is precisely the situation when the holder is being stopped too. close() wakes every waiter and refuses further acquisitions, and the waiters leave through ResourceClosedError, which is an exception the node error path already handles rather than a new mechanism. StaticNetwork calls it on registered resources at the top of halt() and shutdown(), before stopping any node, since a node stopped while parked cannot respond to being stopped. The handover needed care in two places. A waiter woken by close() has not been given ownership, so it takes no Guard and leaves held_ exactly as it found it; and release() now skips handing over to waiters when closed, because handing ownership to a thread that is on its way out would leave held_ true with nobody holding it. reopen() is there for reuse across runs, which the persistent-pipeline work will want; teardown does not need it. Verified in both directions: without close() the waiter thread never returns and the test's join blocks; with it the waiter leaves through ResourceClosedError while the holder still has the resource. 145/145.
This commit is contained in:
@@ -237,3 +237,51 @@ TEST_CASE("make_shared_resource constructs with forwarded args", "[shared_resour
|
||||
auto g = res.acquire();
|
||||
REQUIRE(*g == "hello");
|
||||
}
|
||||
|
||||
// Regression: a waiter must be releasable, or teardown waits on it forever.
|
||||
//
|
||||
// acquire() blocks on a condition variable whose predicate only becomes true
|
||||
// when release() hands over ownership. There was no timeout and no stop
|
||||
// condition, so a node parked there ignored teardown entirely: its worker never
|
||||
// returned, the pool's join never completed, and shutdown hung waiting for a
|
||||
// resource nobody was going to release — which is exactly the case when the
|
||||
// holder is being stopped too.
|
||||
//
|
||||
// close() turns that into an exception the node's existing error path already
|
||||
// handles, and networks now call it on registered resources before stopping any
|
||||
// node, for the same reason.
|
||||
TEST_CASE("closing a shared resource releases its waiters", "[shared_resource]") {
|
||||
SharedResource<int> res(42);
|
||||
|
||||
auto holder = res.acquire(); // resource is now held
|
||||
|
||||
std::atomic<bool> threw{false}, returned{false};
|
||||
std::thread waiter([&] {
|
||||
try {
|
||||
auto g = res.acquire(); // blocks: someone else holds it
|
||||
(void)g;
|
||||
} catch (const ResourceClosedError&) {
|
||||
threw.store(true, std::memory_order_release);
|
||||
}
|
||||
returned.store(true, std::memory_order_release);
|
||||
});
|
||||
|
||||
// Let it park, then tear down without ever releasing the holder.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
REQUIRE_FALSE(returned.load(std::memory_order_acquire));
|
||||
|
||||
res.close();
|
||||
waiter.join();
|
||||
|
||||
CHECK(threw.load(std::memory_order_acquire));
|
||||
}
|
||||
|
||||
TEST_CASE("acquiring a closed resource fails immediately", "[shared_resource]") {
|
||||
SharedResource<int> res(7);
|
||||
res.close();
|
||||
CHECK_THROWS_AS(res.acquire(), ResourceClosedError);
|
||||
|
||||
// Reusable across runs once reopened.
|
||||
res.reopen();
|
||||
CHECK_NOTHROW(res.acquire());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user