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');