From 95687fdfa4fea61a2873616ff2d58149584b41dc Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Sat, 31 Jan 2026 17:40:00 +0100 Subject: [PATCH] Fix Twitch event sub reconnect handling If the connection was aborted by the local machine, the active subsciptions were not cleared causing the new connection not attempt to register any new subsciptions. An event sub connection without any active subsciptions will be dropped by Twitch after a certain amount of time. Additionally the reconnection logic was triggering to frequently causing unecessary load. --- plugins/twitch/event-sub.cpp | 22 +++++++++++++++++++--- plugins/twitch/event-sub.hpp | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/plugins/twitch/event-sub.cpp b/plugins/twitch/event-sub.cpp index 65aa0240..5d8ae91d 100644 --- a/plugins/twitch/event-sub.cpp +++ b/plugins/twitch/event-sub.cpp @@ -89,12 +89,24 @@ void EventSub::ConnectThread() void EventSub::WaitAndReconnect() { + if (_reconnecting) { + return; + } + + _reconnecting = true; + auto thread = std::thread([this]() { std::unique_lock lock(_waitMtx); blog(LOG_INFO, "Twitch EventSub trying to reconnect to in %d seconds.", reconnectDelay); _cv.wait_for(lock, std::chrono::seconds(reconnectDelay)); + _reconnecting = false; + + if (_disconnect) { + return; + } + Connect(); }); thread.detach(); @@ -102,6 +114,10 @@ void EventSub::WaitAndReconnect() void EventSub::Connect() { + if (_reconnecting) { + return; + } + std::lock_guard lock(_connectMtx); if (_connected) { vblog(LOG_INFO, "Twitch EventSub connect already in progress"); @@ -465,8 +481,8 @@ void EventSub::StartServerMigrationClient(const std::string &url) const auto &data = msg->payload; if (type == "session_welcome") { - vblog(LOG_INFO, - "Twitch EventSub migration successful - switching to new connection"); + blog(LOG_INFO, + "Twitch EventSub migration successful - switching to new connection"); OBSDataAutoRelease session = obs_data_get_obj(data, "session"); _migrationSessionID = @@ -565,7 +581,7 @@ void EventSub::OnClose(connection_hdl hdl) blog(LOG_INFO, "Twitch EventSub connection closed: %s / %s (%d)", msg.c_str(), reason.c_str(), code); - if (_migrating) { + if (!_migrating) { ClearActiveSubscriptions(); } _connected = false; diff --git a/plugins/twitch/event-sub.hpp b/plugins/twitch/event-sub.hpp index 33d802c8..f63b0a6f 100644 --- a/plugins/twitch/event-sub.hpp +++ b/plugins/twitch/event-sub.hpp @@ -110,6 +110,7 @@ private: std::condition_variable _cv; std::atomic_bool _connected{false}; std::atomic_bool _disconnect{false}; + std::atomic_bool _reconnecting{false}; std::string _url; std::string _sessionID;