From 6f4a27925483e5a60addfa9d4b9edb4b6d512be5 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 9 Feb 2026 23:09:55 -0600 Subject: [PATCH] Enhance session management in App:startSession - Added a check to end any existing session before starting a new one to prevent overlapping sessions, particularly in one-shot mode. - Implemented a safety timeout for one-shot sync to force disconnect if the session does not end within 30 seconds. --- plugin/src/App/init.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/plugin/src/App/init.lua b/plugin/src/App/init.lua index f003de9d..0f8ef36e 100644 --- a/plugin/src/App/init.lua +++ b/plugin/src/App/init.lua @@ -571,6 +571,15 @@ function App:useRunningConnectionInfo() end function App:startSession() + -- If a session is already in progress, tear it down first. + -- In one-shot mode the sync lock is skipped, so this is the only guard + -- against overlapping sessions (e.g., auto-reconnect racing with a + -- manual connect). + if self.serveSession ~= nil then + Log.trace("Ending existing session before starting a new one") + self:endSession() + end + -- Skip session lock in one-shot mode since it's a quick sync-and-disconnect if not Settings:get("oneShotSync") then local claimedLock, priorOwner = self:claimSyncLock() @@ -678,8 +687,11 @@ function App:startSession() -- Safety: if endSession hasn't fired within 30s, force disconnect. -- This catches cases where the promise chain silently breaks -- (e.g., write request hangs, promise never resolves). + -- Capture the current session so we only kill THIS session, not a + -- different one that may have replaced it (e.g., auto-reconnect race). + local timeoutSession = serveSession task.delay(30, function() - if self.serveSession ~= nil and Settings:get("oneShotSync") then + if self.serveSession ~= nil and self.serveSession == timeoutSession and Settings:get("oneShotSync") then Log.warn("One-shot sync safety timeout: forcing disconnect") self:endSession() end