From 69a2e14b23978ab2961feee423387ac7339048ba Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 18 Dec 2024 15:28:34 -0800 Subject: [PATCH] Fix reconnection errors in some multi-window workflows (#5817) Addresses https://github.com/posit-dev/positron/issues/5750. The underlying issue is somewhat complicated. Basically: - The list of running sessions is stored in ephemeral storage, scoped to the workspace. - Ephemeral storage is shared among all active windows. - We rely on the fact that ephemeral storage goes away when the app quits in order to prevent reconnection to old sessions. - It is possible however in multi-window scenarios to close and reopen a window in a workspace *without ever quitting Positron itself*. In this scenario, we can try to reconnect to non-existent sessions. The fix is to manually clear out the sessions from ephemeral storage on close/quit. This is not related to the 1.95 merge. ### QA Notes This does not reproduce reliably with the steps in #5750. Here are steps that will cause it to reproduce every time in builds prior to this change. 1. Open a workspace (Workspace 1). 2. Using the Open in New Window gesture on the Project drop down in the upper right, open a different workspace (Workspace 2) in a new window. 3. Start Python and/or R in Workspace 2 and run a few commands. 4. Close Workspace 2, but leave the Workspace 1 window open. 5. In Workspace 1, once again use Open in New Window to open Workspace 2. In Step 5, you will see the window attempt to connect to the sessions that you started in Step 3, but get HTTP 404 since those sessions are not running any more. Also note that the purpose of this machinery is to reconnect to sessions on reload/re-navigate, so double check that that still works. --- .../runtimeStartup/common/runtimeStartup.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/vs/workbench/services/runtimeStartup/common/runtimeStartup.ts b/src/vs/workbench/services/runtimeStartup/common/runtimeStartup.ts index 90d94a19130..b411db6e21d 100644 --- a/src/vs/workbench/services/runtimeStartup/common/runtimeStartup.ts +++ b/src/vs/workbench/services/runtimeStartup/common/runtimeStartup.ts @@ -294,6 +294,13 @@ export class RuntimeStartupService extends Disposable implements IRuntimeStartup // before reloading the browser. e.veto(this.saveWorkspaceSessions(), 'positron.runtimeStartup.saveWorkspaceSessions'); + } else if (e.reason === ShutdownReason.CLOSE || e.reason === ShutdownReason.QUIT) { + // Clear the workspace sessions. In most cases this is not + // necessary since the sessions are stored in ephemeral + // storage, but it is possible that this workspace will be + // re-opened without an interleaving quit (e.g. if multiple + // Positron windows are open). + e.veto(this.clearWorkspaceSessions(), 'positron.runtimeStartup.clearWorkspaceSessions'); } })); } @@ -770,6 +777,17 @@ export class RuntimeStartupService extends Disposable implements IRuntimeStartup })); } + /** + * Clear the set of workspace sessions in the workspace storage. + */ + private async clearWorkspaceSessions(): Promise { + // Clear the sessions. + await this._ephemeralStateService.removeItem(this.getPersistentWorkspaceSessionsKey()); + + // Always return false (don't veto shutdown) + return false; + } + /** * Update the set of workspace sessions in the workspace storage. *