diff --git a/src/iosource/Manager.cc b/src/iosource/Manager.cc index fcb4a1e961..952bb13704 100644 --- a/src/iosource/Manager.cc +++ b/src/iosource/Manager.cc @@ -104,6 +104,22 @@ void Manager::Wakeup(std::string_view where) { wakeup->Ping(where); } +void Manager::ReapSource(Source* src) { + auto* iosource = src->src; + assert(! iosource->IsOpen()); + + DBG_LOG(DBG_MAINLOOP, "Reaping %s", src->src->Tag()); + iosource->Done(); + + if ( src->manage_lifetime ) + delete iosource; + + if ( src->dont_count ) + dont_counts--; + + delete src; +} + void Manager::FindReadySources(ReadySources* ready) { ready->clear(); @@ -111,8 +127,7 @@ void Manager::FindReadySources(ReadySources* ready) { // remove at most one each time. for ( SourceList::iterator i = sources.begin(); i != sources.end(); ++i ) if ( ! (*i)->src->IsOpen() ) { - (*i)->src->Done(); - delete *i; + ReapSource(*i); sources.erase(i); break; } diff --git a/src/iosource/Manager.h b/src/iosource/Manager.h index c533afb982..48f8814b2b 100644 --- a/src/iosource/Manager.h +++ b/src/iosource/Manager.h @@ -143,6 +143,15 @@ public: void Wakeup(std::string_view where); private: + /** + * Internal data structure for managing registered IOSources. + */ + struct Source { + IOSource* src = nullptr; + bool dont_count = false; + bool manage_lifetime = false; + }; + /** * Calls the appropriate poll method to gather a set of IOSources that are * ready for processing. @@ -170,6 +179,19 @@ private: void RemoveAll(); + /** + * Reap a closed IO source. + * + * Reaping involves calling IOSource::Done() on the underlying IOSource, + * freeing it if Source.manage_lifetime is \c true, updating \c dont_counts + * and freeing \a src, making it invalid. + * + * The caller ensures \a src is removed from Manager.sources. + * + * @param src The source to reap. + */ + void ReapSource(Source* src); + class WakeupHandler final : public IOSource { public: WakeupHandler(); @@ -192,12 +214,6 @@ private: zeek::detail::Flare flare; }; - struct Source { - IOSource* src = nullptr; - bool dont_count = false; - bool manage_lifetime = false; - }; - using SourceList = std::vector; SourceList sources;