fix(project): stabilize project deletion
- Guard renderer project list/get against stale initial loads - Retry project zip/cache removal to handle transient Windows locks - Surface deletion failures in UI and add regression tests Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { rmWithRetries } from './fsRetry';
|
||||
|
||||
void test('rmWithRetries: retries on EPERM and then succeeds', async () => {
|
||||
let calls = 0;
|
||||
const rm = () => {
|
||||
calls += 1;
|
||||
if (calls < 3) {
|
||||
const err = new Error('nope') as Error & { code: string };
|
||||
err.code = 'EPERM';
|
||||
throw err;
|
||||
}
|
||||
return Promise.resolve();
|
||||
};
|
||||
await rmWithRetries(rm, 'x', { force: true }, 5);
|
||||
assert.equal(calls, 3);
|
||||
});
|
||||
|
||||
void test('rmWithRetries: does not retry on ENOENT', async () => {
|
||||
let calls = 0;
|
||||
const rm = () => {
|
||||
calls += 1;
|
||||
const err = new Error('missing') as Error & { code: string };
|
||||
err.code = 'ENOENT';
|
||||
return Promise.reject(err);
|
||||
};
|
||||
await assert.rejects(() => rmWithRetries(rm, 'x', { force: true }, 5), /missing/);
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
type RmLike = (path: string, opts: { recursive?: boolean; force?: boolean }) => Promise<void>;
|
||||
|
||||
function isRetryableRmError(err: unknown): boolean {
|
||||
if (!err || typeof err !== 'object') return false;
|
||||
const code = (err as { code?: unknown }).code;
|
||||
return code === 'EBUSY' || code === 'EPERM' || code === 'EACCES';
|
||||
}
|
||||
|
||||
export async function rmWithRetries(
|
||||
rm: RmLike,
|
||||
targetPath: string,
|
||||
opts: { recursive?: boolean; force?: boolean },
|
||||
retries = 6,
|
||||
): Promise<void> {
|
||||
let attempt = 0;
|
||||
// Windows: file locks/antivirus/indexers can cause transient EPERM/EBUSY.
|
||||
// We retry a few times before surfacing the error.
|
||||
// Delays: 20, 40, 80, 160, 320, 640ms (capped by retries).
|
||||
for (;;) {
|
||||
try {
|
||||
await rm(targetPath, opts);
|
||||
return;
|
||||
} catch (e) {
|
||||
attempt += 1;
|
||||
if (attempt > retries || !isRetryableRmError(e)) {
|
||||
throw e;
|
||||
}
|
||||
const delayMs = Math.min(800, 20 * 2 ** (attempt - 1));
|
||||
await new Promise((r) => setTimeout(r, delayMs));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import { asAssetId, asGraphNodeId, asProjectId } from '../../shared/types/ids';
|
||||
import { getAppSemanticVersion } from '../versionInfo';
|
||||
|
||||
import { reconcileAssetFiles } from './assetPrune';
|
||||
import { rmWithRetries } from './fsRetry';
|
||||
import { getLegacyProjectsRootDirs, getProjectsCacheRootDir, getProjectsRootDir } from './paths';
|
||||
import { readProjectJsonFromZip, unzipToDir } from './yauzlProjectZip';
|
||||
|
||||
@@ -792,8 +793,8 @@ export class ZipProjectStore {
|
||||
this.projectSession += 1;
|
||||
}
|
||||
|
||||
await fs.rm(zipPath, { force: true }).catch(() => undefined);
|
||||
await fs.rm(cacheDir, { recursive: true, force: true }).catch(() => undefined);
|
||||
await rmWithRetries(fs.rm, zipPath, { force: true });
|
||||
await rmWithRetries(fs.rm, cacheDir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
private randomId(): string {
|
||||
|
||||
@@ -917,7 +917,13 @@ function ProjectPicker({ projects, onCreate, onOpen, onDelete }: ProjectPickerPr
|
||||
) {
|
||||
return;
|
||||
}
|
||||
void onDelete(id);
|
||||
void (async () => {
|
||||
try {
|
||||
await onDelete(id);
|
||||
} catch (e) {
|
||||
window.alert(e instanceof Error ? e.message : String(e));
|
||||
}
|
||||
})();
|
||||
}}
|
||||
>
|
||||
Удалить
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
void test('projectState: list/get after delete invalidates in-flight initial load (epoch guard)', () => {
|
||||
const src = fs.readFileSync(path.join(here, 'projectState.ts'), 'utf8');
|
||||
assert.match(src, /projectDataEpochRef/);
|
||||
assert.match(src, /const epoch = projectDataEpochRef\.current/);
|
||||
assert.match(src, /if \(projectDataEpochRef\.current !== epoch\) return/);
|
||||
assert.match(
|
||||
src,
|
||||
/const deleteProject = async[\s\S]+?projectDataEpochRef\.current \+= 1[\s\S]+?await api\.invoke/,
|
||||
);
|
||||
assert.match(
|
||||
src,
|
||||
/const openProject = async[\s\S]+?projectDataEpochRef\.current \+= 1[\s\S]+?await api\.invoke/,
|
||||
);
|
||||
assert.match(src, /const refreshProjects = async \(\) => \{[\s\S]+?projectDataEpochRef\.current \+= 1/);
|
||||
});
|
||||
@@ -58,12 +58,15 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
const api = getDndApi();
|
||||
const [state, setState] = useState<State>({ projects: [], project: null, selectedSceneId: null });
|
||||
const projectRef = useRef<Project | null>(null);
|
||||
/** Bumps on mutations / refresh; initial license load only applies if still current (avoids racing late list/get over newer state). */
|
||||
const projectDataEpochRef = useRef(0);
|
||||
useEffect(() => {
|
||||
projectRef.current = state.project;
|
||||
}, [state.project]);
|
||||
|
||||
const actions = useMemo<Actions>(() => {
|
||||
const refreshProjects = async () => {
|
||||
projectDataEpochRef.current += 1;
|
||||
const res = await api.invoke(ipcChannels.project.list, {});
|
||||
setState((s) => ({ ...s, projects: res.projects }));
|
||||
};
|
||||
@@ -75,6 +78,7 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
};
|
||||
|
||||
const openProject = async (id: ProjectId) => {
|
||||
projectDataEpochRef.current += 1;
|
||||
const res = await api.invoke(ipcChannels.project.open, { projectId: id });
|
||||
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project.currentSceneId }));
|
||||
};
|
||||
@@ -276,6 +280,7 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
};
|
||||
|
||||
const deleteProject = async (projectId: ProjectId) => {
|
||||
projectDataEpochRef.current += 1;
|
||||
await api.invoke(ipcChannels.project.deleteProject, { projectId });
|
||||
const listRes = await api.invoke(ipcChannels.project.list, {});
|
||||
const res = await api.invoke(ipcChannels.project.get, {});
|
||||
@@ -316,14 +321,18 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
||||
useEffect(() => {
|
||||
if (!licenseActive) {
|
||||
queueMicrotask(() => {
|
||||
projectDataEpochRef.current += 1;
|
||||
setState({ projects: [], project: null, selectedSceneId: null });
|
||||
});
|
||||
return;
|
||||
}
|
||||
void (async () => {
|
||||
const epoch = projectDataEpochRef.current;
|
||||
const listRes = await api.invoke(ipcChannels.project.list, {});
|
||||
if (projectDataEpochRef.current !== epoch) return;
|
||||
setState((s) => ({ ...s, projects: listRes.projects }));
|
||||
const res = await api.invoke(ipcChannels.project.get, {});
|
||||
if (projectDataEpochRef.current !== epoch) return;
|
||||
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project?.currentSceneId ?? null }));
|
||||
})();
|
||||
}, [licenseActive, api]);
|
||||
|
||||
Reference in New Issue
Block a user