From f8e4e7573e76a02d8618b286dc58eb4d6e9eb2b1 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Fri, 4 Dec 2026 11:59:01 +0100 Subject: [PATCH] DPL: keep the run loop going until in-flight streams finish With DPL_THREADPOOL_SIZE>0 the device loop hands doRun to the uv pool and then returns from Run() without waiting for it, so FairMQ tears the channels down while a worker is still inside a send and the channel is reached through a dangling pointer -- instrumenting the failing send showed index=0 of 1 output channel and a sane proxy, but IsValid() false and a name of garbage bytes. Keep looping while a stream is running rather than blocking after the loop: the transition expires in the middle of the body and what follows it still matters to a stream which is finishing, in particular draining the asynchronous queue, which is how the oldest possible timeslice goes out. Stop scheduling new work once the transition has expired, since we are only draining by then. stream.running is set when the work is queued and cleared by run_completion, both on the loop thread, so none of this needs atomics. On o2-test-framework-SimpleDataProcessingDevice01, 20 runs at DPL_THREADPOOL_SIZE=1: 8/20 passing to 16/20. --- Framework/Core/src/DataProcessingDevice.cxx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Framework/Core/src/DataProcessingDevice.cxx b/Framework/Core/src/DataProcessingDevice.cxx index 59204eb053ac4..3d75cc602b91d 100644 --- a/Framework/Core/src/DataProcessingDevice.cxx +++ b/Framework/Core/src/DataProcessingDevice.cxx @@ -1216,7 +1216,19 @@ void DataProcessingDevice::Run() auto* poolSizeEnv = getenv("DPL_THREADPOOL_SIZE"); bool dplEnableMultithreding = poolSizeEnv && std::atoi(poolSizeEnv) > 0; - while (state.transitionHandling != TransitionHandlingState::Expired) { + // A stream runs doRun on a pool thread and its completion is dispatched on + // this loop. Leaving while one is in flight lets FairMQ tear down the + // channels under a send which has not finished with them. + auto anyStreamRunning = [this]() { + return std::any_of(mStreams.begin(), mStreams.end(), [](TaskStreamInfo const& stream) { return stream.running; }); + }; + + // The transition expires in the middle of the body, and what follows it still + // matters to a stream which is finishing: the asynchronous queue is drained + // down there, and that is how the oldest possible timeslice goes out. So keep + // going round rather than breaking out and waiting outside, which would run + // the completions but none of the work they queue. + while (state.transitionHandling != TransitionHandlingState::Expired || anyStreamRunning()) { if (state.nextFairMQState.empty() == false) { (void)this->ChangeState(state.nextFairMQState.back()); state.nextFairMQState.pop_back(); @@ -1352,7 +1364,7 @@ void DataProcessingDevice::Run() using o2::monitoring::tags::Value; // We have an empty stream, let's check if we have enough // resources for it to run something - if (streamRef.index != -1) { + if (streamRef.index != -1 && state.transitionHandling != TransitionHandlingState::Expired) { // Synchronous execution of the callbacks. This will be moved in the // moved in the on_socket_polled once we have threading in place. uv_work_t& handle = mHandles[streamRef.index];