From fb9475ecadd877abbd5ca066a2df472919dd3c4e Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 11 May 2025 14:29:31 +0200 Subject: [PATCH] fix: send weak ptr to session's checkKeepalive timer (#6204) --- CHANGELOG.md | 1 + lib/twitch-eventsub-ws/src/session.cpp | 26 +++++++++++++------- src/providers/twitch/eventsub/Controller.cpp | 2 ++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6b11eee..ac032203 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - Bugfix: Fixed some Twitch commands not getting tab-completed correctly. (#6143) - Bugfix: Fixed shared chat badges displaying pixelated when Chatterino is scaled too much. (#6146) - Bugfix: Fixed a crash that could occur when eventsub was enabled and Chatterino was attached to a conhost on Windows that was later gone. (#6161) +- Bugfix: Fixed a crash that could occur an eventsub connection's keepalive timer would run after the connection was dead, causing the keepalive timer to use-itself-after-free. (#6204) - Bugfix: Fixed some minor typos. (#6196) - Dev: Mini refactor of Split. (#6148) - Dev: Conan will no longer generate a `CMakeUserPresets.json` file. (#6117) diff --git a/lib/twitch-eventsub-ws/src/session.cpp b/lib/twitch-eventsub-ws/src/session.cpp index 45c2f104..6c83895d 100644 --- a/lib/twitch-eventsub-ws/src/session.cpp +++ b/lib/twitch-eventsub-ws/src/session.cpp @@ -589,15 +589,23 @@ void Session::checkKeepalive() this->keepaliveTimer = std::make_unique(this->ws.get_executor()); this->keepaliveTimer->expires_after(this->keepaliveTimeout); - this->keepaliveTimer->async_wait([this](boost::system::error_code ec) { - if (ec) - { - this->log->warn( - c2fmt::format("Keepalive timer cancelled: {}", ec.message())); - return; - } - this->checkKeepalive(); - }); + this->keepaliveTimer->async_wait( + [weak{this->weak_from_this()}](boost::system::error_code ec) { + auto strong = weak.lock(); + if (!strong) + { + // Session was destroyed + return; + } + + if (ec) + { + strong->log->warn(c2fmt::format("Keepalive timer cancelled: {}", + ec.message())); + return; + } + strong->checkKeepalive(); + }); } } // namespace chatterino::eventsub::lib diff --git a/src/providers/twitch/eventsub/Controller.cpp b/src/providers/twitch/eventsub/Controller.cpp index 6072f2e6..ba613e48 100644 --- a/src/providers/twitch/eventsub/Controller.cpp +++ b/src/providers/twitch/eventsub/Controller.cpp @@ -465,6 +465,8 @@ void Controller::createConnection(std::string host, std::string port, std::string path, std::unique_ptr listener) { + qCDebug(LOG) << "Create EventSub connection"; + try { boost::asio::ssl::context sslContext{