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;