From d70213bb175a34343f69986df85ab7c7efbf4864 Mon Sep 17 00:00:00 2001 From: CommaBot Date: Mon, 23 Nov 2026 22:44:58 +0800 Subject: [PATCH] fix(electron): keep trashed local workspaces from coming back (#15914) Trashing a local workspace copies its directory to `deleted-workspaces` and then removes the original. On Windows that removal often fails because the database file is still locked; the error was only logged. The leftover stays hidden while its backup exists, but deleting the backup from the archive removed only the backup, so the next scan of local workspaces found the leftover and the workspace came back. - Removing a workspace directory now retries (`fs.rm` with `maxRetries`), which covers a lock released shortly after the connection is closed. - Deleting a backup first removes the leftover original, then the backup. If the leftover still cannot be removed, the error is returned and the backup is kept, so the workspace stays in the archive (and hidden from the workspace list) and can be deleted again later instead of coming back. No data is removed unless the user deleted it. Adds tests for deleting a backup with a leftover original, both when it can be removed and when it is locked. fix #14731 Co-authored-by: DarkSky --- .../electron/src/helper/workspace/handlers.ts | 26 ++++++++- .../electron/test/workspace/handlers.spec.ts | 53 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/packages/frontend/apps/electron/src/helper/workspace/handlers.ts b/packages/frontend/apps/electron/src/helper/workspace/handlers.ts index c0255b615b..d60ab5d3c0 100644 --- a/packages/frontend/apps/electron/src/helper/workspace/handlers.ts +++ b/packages/frontend/apps/electron/src/helper/workspace/handlers.ts @@ -27,6 +27,17 @@ import { getWorkspaceMeta, } from './meta'; +// On Windows a SQLite file can stay locked for a moment after its connection +// is closed, so removing its directory needs a few retries. +async function removeWorkspaceDir(dir: string) { + await fs.rm(dir, { + recursive: true, + force: true, + maxRetries: 10, + retryDelay: 100, + }); +} + async function deleteWorkspaceV1(workspaceId: string) { try { await ensureSQLiteDisconnected('workspace', workspaceId); @@ -47,7 +58,7 @@ export async function deleteWorkspace(universalId: string) { const dbPath = await getSpaceDBPath(peer, type, id); try { await getDocStoragePool().disconnect(universalId); - await fs.remove(path.dirname(dbPath)); + await removeWorkspaceDir(path.dirname(dbPath)); } catch (e) { logger.error('deleteWorkspace', e); } @@ -78,7 +89,10 @@ export async function trashWorkspace(universalId: string) { await fs.copy(path.dirname(dbPath), movedPath, { overwrite: true, }); - await fs.remove(path.dirname(dbPath)); + // If the original still cannot be removed, it stays hidden from + // listLocalWorkspaceIds while its backup exists and is removed together + // with the backup in deleteBackupWorkspace. + await removeWorkspaceDir(path.dirname(dbPath)); } catch (error) { logger.error('trashWorkspace', error); } @@ -323,6 +337,14 @@ async function importWorkspaceDb(originalPath: string) { } export async function deleteBackupWorkspace(id: string) { + // Remove what trashWorkspace could not remove before the backup that hides + // it, so a failure keeps the workspace in the backup list instead of bringing + // it back as a local workspace. + const leftoverPath = path.dirname( + await getSpaceDBPath('local', 'workspace', id) + ); + await removeWorkspaceDir(leftoverPath); + const basePath = await getDeletedWorkspacesBasePath(); const workspacePath = path.join(basePath, normalizeWorkspaceIdForPath(id)); await fs.remove(workspacePath); diff --git a/packages/frontend/apps/electron/test/workspace/handlers.spec.ts b/packages/frontend/apps/electron/test/workspace/handlers.spec.ts index c1b4ee35be..275181717e 100644 --- a/packages/frontend/apps/electron/test/workspace/handlers.spec.ts +++ b/packages/frontend/apps/electron/test/workspace/handlers.spec.ts @@ -391,6 +391,59 @@ describe('workspace db management', () => { ).toBe(true); }); + test.each([ + { name: 'removes the leftover', locked: false }, + { name: 'keeps the backup when the leftover is locked', locked: true }, + ])( + 'deleting the backup of a workspace whose original survived trashing $name', + async ({ locked }) => { + const { deleteBackupWorkspace, listLocalWorkspaceIds } = + await import('@affine/electron/helper/workspace/handlers'); + const workspaceId = v4(); + const workspacePath = path.join( + appDataPath, + 'workspaces', + 'local', + workspaceId + ); + const backupPath = path.join( + appDataPath, + 'deleted-workspaces', + workspaceId + ); + await fs.ensureFile(path.join(workspacePath, 'storage.db')); + await fs.ensureFile(path.join(backupPath, 'storage.db')); + + const rm = fs.rm; + const rmSpy = vi.spyOn(fs, 'rm').mockImplementation((( + target: string, + options: Parameters[1] + ) => + locked && target === workspacePath + ? Promise.reject( + Object.assign(new Error('EBUSY: resource busy or locked'), { + code: 'EBUSY', + }) + ) + : rm(target, options)) as typeof rm); + try { + if (locked) { + await expect(deleteBackupWorkspace(workspaceId)).rejects.toThrow( + 'EBUSY' + ); + } else { + await deleteBackupWorkspace(workspaceId); + } + } finally { + rmSpy.mockRestore(); + } + + expect(await fs.pathExists(backupPath)).toBe(locked); + expect(await fs.pathExists(workspacePath)).toBe(locked); + expect(await listLocalWorkspaceIds()).not.toContain(workspaceId); + } + ); + test('delete workspace', async () => { const { deleteWorkspace } = await import('@affine/electron/helper/workspace/handlers');