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];