diff --git a/src/iosource/Manager.cc b/src/iosource/Manager.cc index a7821e1a5f..bbef07a922 100644 --- a/src/iosource/Manager.cc +++ b/src/iosource/Manager.cc @@ -126,20 +126,6 @@ void Manager::ReapSource(Source* src) { void Manager::FindReadySources(ReadySources* ready) { ready->clear(); - // Remove sources which have gone dry. For simplicity, we only - // remove at most one each time. - for ( SourceList::iterator i = sources.begin(); i != sources.end(); ++i ) - if ( ! (*i)->src->IsOpen() ) { - ReapSource(*i); - sources.erase(i); - break; - } - - // If there aren't any sources and exit_only_after_terminate is false, just - // return an empty set of sources. We want the main loop to end. - if ( Size() == 0 && (! BifConst::exit_only_after_terminate || run_state::terminating) ) - return; - double timeout = -1; IOSource* timeout_src = nullptr; bool time_to_poll = false; @@ -151,7 +137,8 @@ void Manager::FindReadySources(ReadySources* ready) { } // Find the source with the next timeout value. - for ( auto src : sources ) { + for ( auto i = sources.begin(); i != sources.end(); /* noop */ ) { + auto* src = *i; auto iosource = src->src; if ( iosource->IsOpen() ) { double next = iosource->GetNextTimeout(); @@ -179,7 +166,19 @@ void Manager::FindReadySources(ReadySources* ready) { ready->push_back({pkt_src, -1, 0}); } } + ++i; } + else { + ReapSource(src); + i = sources.erase(i); + } + } + + // If there aren't any sources and exit_only_after_terminate is false, just + // return an empty set of sources. We want the main loop to end. + if ( Size() == 0 && (! BifConst::exit_only_after_terminate || run_state::terminating) ) { + ready->clear(); + return; } DBG_LOG(DBG_MAINLOOP, "timeout: %f ready size: %zu time_to_poll: %d\n", timeout, ready->size(), time_to_poll);