fix: ensure liveupdate pubsubs exit (#5557)

Previously, the derived class (i.e. BttvLiveUpdates or SeventvEventAPI)
    would have their destructor ran before BasicPubSubManager called
    stop, meaning there was a time wherein messages could still flow
    through, attempting to call `onMessage` on a pure virtual, causing a
    crash.
This commit is contained in:
pajlada
2024-08-24 14:01:13 +02:00
committed by GitHub
parent 175afa8b16
commit aa048b3793
7 changed files with 34 additions and 6 deletions
@@ -99,7 +99,8 @@ public:
virtual ~BasicPubSubManager()
{
this->stop();
// The derived class must call stop in its destructor
assert(this->stopping_);
}
BasicPubSubManager(const BasicPubSubManager &) = delete;
@@ -127,6 +128,11 @@ public:
void stop()
{
if (this->stopping_)
{
return;
}
this->stopping_ = true;
for (const auto &client : this->clients_)
@@ -138,10 +144,20 @@ public:
if (this->mainThread_->joinable())
{
this->mainThread_->join();
// NOTE: We spawn a new thread to join the websocket thread.
// There is a case where a new client was initiated but not added to the clients list.
// We just don't join the thread & let the operating system nuke the thread if joining fails
// within 1s.
auto joiner = std::async(std::launch::async, &std::thread::join,
this->mainThread_.get());
if (joiner.wait_for(std::chrono::seconds(1)) ==
std::future_status::timeout)
{
qCWarning(chatterinoLiveupdates)
<< "Thread didn't join within 1 second, rip it out";
this->websocketClient_.stop();
}
}
assert(this->clients_.empty());
}
protected: