fix: the drain loops must terminate, and must drain the right channels
shutdown()'s drain step was an unbounded
while (anything, anywhere, is non-empty) poll every channel
Three defects in one loop.
It drained the wrong thing. Stopping a node should wait for that node's own
outputs before moving to the next layer; this waited for the entire graph to
fall idle each time. The dynamic Network's version made it explicit — it took
a node name and ignored it. Both now track which node feeds each probe and
wait only on those.
It had no deadline, so anything wedged downstream turned a graceful shutdown
into the hang it exists to avoid. Now bounded two ways, because a stalled
consumer and a slow one fail differently: a deadline for fill that never
changes, and a no-progress counter that keeps waiting as long as the queue is
shrinking, so a slow drain is not cut short merely for taking a while.
And it could fail to terminate with nothing wedged at all. current_fill came
from a snapshot that loaded tail_ before head_. A concurrent pop between the
two reads yields a head_ past the sampled tail_, and the unsigned difference
wraps to ~2^64 — so a poll for "is it empty yet" runs forever on a channel
that is in fact empty. Both indices only ever increase, so loading head_
first can at worst under-report a concurrent push, which this loop tolerates
and a wrap does not. size() and snapshot() are both corrected; size() feeds
approx_size(), which is what node readiness checks call.
Giving up is now reported rather than silent, because undrained data at that
point is about to be discarded by the stop that follows, and a graceful
shutdown quietly dropping values is the thing worth knowing about.
Verified in both directions: with the old loop the new case is killed at a
30 s timeout; with this it returns in under a second, having reported four
items its wedged consumer never took. Full suite 138/138.
Note the drain timeout is per node and defaults to 5 s, so a graph of N
stalled nodes can still take N x 5 s to shut down. That is a deliberate
trade against cutting off legitimate slow drains, and set_drain_timeout()
exists for callers who want it tighter.
This commit is contained in:
@@ -271,3 +271,81 @@ TEST_CASE("static_network: fanout with labelled same-function consumers", "[stat
|
||||
REQUIRE(outB.pop() == 7);
|
||||
net.stop();
|
||||
}
|
||||
|
||||
// Regression: shutdown() must return even when a consumer stopped consuming.
|
||||
//
|
||||
// The drain step was an unbounded `while (anything anywhere is non-empty)` poll
|
||||
// over *every* channel in the graph. Two defects in one loop: it waited for the
|
||||
// whole network to be idle before stopping each successive layer rather than
|
||||
// just the node it had stopped — the dynamic Network's version even took a node
|
||||
// name and ignored it — and it had no deadline, so anything wedged downstream
|
||||
// turned a graceful shutdown into the hang it exists to avoid.
|
||||
//
|
||||
// It could also fail to terminate with nothing wedged at all. current_fill came
|
||||
// from a snapshot that loaded tail_ before head_; a concurrent pop between the
|
||||
// two reads yields a head_ past the sampled tail_, and the unsigned difference
|
||||
// wraps to ~2^64. Any poll for "is it empty yet" against that value runs
|
||||
// forever. Both indices only ever increase, so loading head_ first can at worst
|
||||
// under-report a push, which this loop tolerates and a wrap does not.
|
||||
//
|
||||
// Here the sink never takes anything, so its input cannot drain and the only
|
||||
// correct outcome is to give up and say so. The bound asserted is deliberately
|
||||
// loose: the point is that it terminates, not how fast.
|
||||
namespace {
|
||||
|
||||
struct DrainSource {
|
||||
static constexpr std::string_view label() { return "drain_source"; }
|
||||
int n{0};
|
||||
int operator()() {
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||
return n++;
|
||||
}
|
||||
};
|
||||
|
||||
struct NeverConsumes {
|
||||
static constexpr std::string_view label() { return "never_consumes"; }
|
||||
std::atomic<bool>* wedged;
|
||||
void operator()(int) {
|
||||
// Blocks for the duration of the test: the input channel behind it
|
||||
// fills and stays full.
|
||||
while (!wedged->load(std::memory_order_acquire))
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("shutdown returns when a consumer has wedged", "[static_network][shutdown]") {
|
||||
std::atomic<bool> release{false};
|
||||
|
||||
DrainSource src_fn;
|
||||
NeverConsumes sink_fn{&release};
|
||||
|
||||
kpn::ObjectNode<DrainSource, kpn::in<>, kpn::out<"v">, "drain_source", 0> s(src_fn, 4);
|
||||
kpn::ObjectNode<NeverConsumes, kpn::in<"v">, kpn::out<>, "never_consumes", 0> k(sink_fn, 4);
|
||||
|
||||
auto net = kpn::make_network(kpn::edge(s.output<"v">(), k.input<"v">()));
|
||||
net.set_drain_timeout(std::chrono::milliseconds(100));
|
||||
net.start();
|
||||
|
||||
// Let the channel fill and the sink jam.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
// Unjam the sink well after the drain timeout should have expired. Stopping
|
||||
// a node joins its worker, so a sink blocked forever would hang the test in
|
||||
// stop() rather than in the drain loop this case is about.
|
||||
std::thread unjam([&] {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
||||
release.store(true, std::memory_order_release);
|
||||
});
|
||||
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
net.shutdown();
|
||||
const auto elapsed = std::chrono::steady_clock::now() - t0;
|
||||
|
||||
unjam.join();
|
||||
|
||||
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
|
||||
INFO("shutdown took " << ms << " ms");
|
||||
CHECK(ms < 3000); // unbounded before; one 100 ms drain timeout after
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user