TCP pusher: never block the receiver thread; queue bounded by image-buffer slots
TCPStreamPusher::SendImage is called from the receiver's frame-transformation threads. It used a 128-deep per-connection queue and waited when it was full. With images routed per file ((n / images_per_file) % writers), every writer receives a whole file as a burst at the full frame rate (1000 images/file at 2 kHz fills 128 in ~64 ms), so the queue filled on every burst and all transformation threads ended up waiting on one writer. They then stopped returning FPGA frame buffers, and the FPGA dropped packets: partial images, images without a background estimate, data loss on every writer. The ZeroMQ pusher has an unbounded queue and never blocks, which is why it coped. - The per-connection queue is unbounded: every element owns a distinct image-buffer slot, so the bound is the image buffer's slot count. When slots run out the receiver drops only the write (GetImageSlot() == nullptr), after analysis. Absorbing one file burst therefore needs an image buffer of about images_per_file slots (~20 GB for a 9M at 1000 images/file); below that, images are still dropped, now as writer-queue-full instead of packet loss. - SendImage never blocks. A dead or wedged writer is still caught in SendAll (15 s silence / 60 s without progress), after which WriterThread returns the queued slots unsent. - The 200 ms fallback in StopDataCollectionThreads is gone: at END it closed the connection of any writer whose queue stayed full for 200 ms - a normal state for a slow but healthy writer - discarding up to 128 queued images and never sending END. END now follows the whole backlog. - A per-connection enqueue mutex makes "check active, enqueue" atomic against "clear active, enqueue the end sentinel": an image could land behind the sentinel when a connection was torn down mid-run, leaking its slot (CheckIfBufferReturned then threw) or being sent as stale data in the next run. New stress test SendImageRacingEnd_NoSlotLeak (verified to fail with the race widened and the lock removed). - TearDownConnection marks the connection broken first, so the destructor returns a backlog instead of sending it. Writer side: - jfjoch_writer -b/--rcv_buffer sets the TCP receive socket buffer (was hardcoded to the kernel default). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e39e40f955
commit
d7521bf1ea
7 files changed
+104
-53
No files matched your search
@@ -1020,11 +1020,11 @@ private:
|
||||
|
||||
TEST_CASE("TCPImageCommTest_StalledWriter_SurvivesViaHeartbeat", "[TCP]") {
|
||||
// A writer that is alive (still heartbeating) but has stopped draining — e.g. wedged
|
||||
// on a slow filesystem at high frame rate — must NOT be dropped mid-run. The pusher
|
||||
// rides out the backpressure on the production zero-copy queue path until the writer
|
||||
// recovers. Regression for the queue-path send giving up on a fixed deadline, and for
|
||||
// the BUSY heartbeat keeping the connection alive past the peer-liveness window.
|
||||
constexpr int64_t N = 1000; // > queue depth + socket buffers
|
||||
// on a slow filesystem at high frame rate — must NOT be dropped mid-run. The backlog
|
||||
// waits in the image buffer, and SendImage must never block the caller (the receiver
|
||||
// thread) meanwhile. Regression for the queue-path send blocking on a short fixed queue,
|
||||
// and for the BUSY heartbeat keeping the connection alive past the peer-liveness window.
|
||||
constexpr int64_t N = 1000; // > socket buffers, < image buffer slots
|
||||
constexpr auto liveness = std::chrono::milliseconds(2000);
|
||||
constexpr auto stall = std::chrono::milliseconds(4000); // > liveness AND > old send deadline
|
||||
|
||||
@@ -1058,10 +1058,10 @@ TEST_CASE("TCPImageCommTest_StalledWriter_SurvivesViaHeartbeat", "[TCP]") {
|
||||
}
|
||||
});
|
||||
|
||||
// During the stall the queue is full; SendImage must block, not drop the connection.
|
||||
// During the stall every image is queued at once, holding its slot, and the connection stays.
|
||||
std::this_thread::sleep_for(stall);
|
||||
CHECK(pusher.GetConnectedWriters() == 1);
|
||||
CHECK(sender.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready);
|
||||
CHECK(sender.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready);
|
||||
|
||||
// Writer recovers and starts draining.
|
||||
writer.Release();
|
||||
@@ -1144,3 +1144,57 @@ TEST_CASE("TCPImageCommTest_WedgedWriter_DroppedByBackpressureCap", "[TCP]") {
|
||||
|
||||
image_buffer.Finalize(std::chrono::seconds(5));
|
||||
}
|
||||
|
||||
TEST_CASE("TCPImageCommTest_SendImageRacingEnd_NoSlotLeak", "[TCP]") {
|
||||
// Producers keep calling SendImage while EndDataCollection runs. Every image must either be
|
||||
// accepted and delivered before END, or refused with its slot returned - none may land behind
|
||||
// the end sentinel, where its slot would leak and CheckIfBufferReturned would fail.
|
||||
for (int iteration = 0; iteration < 10; iteration++) {
|
||||
TCPStreamPusher pusher("tcp://127.0.0.1:*", 1);
|
||||
|
||||
StallableWriterDouble writer(pusher.GetAddress()[0], 1024 * 1024);
|
||||
REQUIRE(writer.Connected());
|
||||
writer.Release();
|
||||
|
||||
for (int attempt = 0; attempt < 200 && pusher.GetConnectedWriters() < 1; ++attempt)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(25));
|
||||
REQUIRE(pusher.GetConnectedWriters() == 1);
|
||||
|
||||
ImageBuffer image_buffer(16 * 1024 * 1024);
|
||||
image_buffer.StartMeasurement(static_cast<size_t>(4096));
|
||||
|
||||
StartMessage start{.images_per_file = 1000, .write_master_file = true};
|
||||
pusher.StartDataCollection(start);
|
||||
|
||||
std::atomic<bool> stop{false};
|
||||
std::atomic<int64_t> next_image{0};
|
||||
std::atomic<size_t> accepted{0};
|
||||
std::vector<std::future<void>> senders;
|
||||
for (int t = 0; t < 4; t++) {
|
||||
senders.emplace_back(std::async(std::launch::async, [&] {
|
||||
while (!stop) {
|
||||
ZeroCopyReturnValue *slot = image_buffer.GetImageSlot();
|
||||
if (slot == nullptr)
|
||||
continue;
|
||||
slot->SetImageNumber(next_image++);
|
||||
slot->SetImageSize(256);
|
||||
slot->ReadyToSend();
|
||||
if (pusher.SendImage(*slot))
|
||||
accepted++;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
EndMessage end{};
|
||||
CHECK(pusher.EndDataCollection(end));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20)); // producers still running after END
|
||||
stop = true;
|
||||
for (auto &s: senders)
|
||||
s.get();
|
||||
|
||||
CHECK(image_buffer.CheckIfBufferReturned(std::chrono::seconds(5)));
|
||||
CHECK(writer.DataFramesReceived() == accepted.load());
|
||||
image_buffer.Finalize(std::chrono::seconds(5));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user