fix(settings): clean up orphaned temp files and harden atomic writes

This commit is contained in:
Bohdan Triapitsyn
2026-08-22 11:55:38 +03:00
parent 735a9463ca
commit fcbe2c0414
8 changed files with 142 additions and 25 deletions
@@ -231,8 +231,13 @@ export const createProjectContextRuntime = (deps) => {
const writeJsonAtomic = async (filePath, value) => {
const temporaryPath = `${filePath}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
await fsPromises.mkdir(path.dirname(filePath), { recursive: true });
await fsPromises.writeFile(temporaryPath, JSON.stringify(value, null, 2), 'utf8');
await fsPromises.rename(temporaryPath, filePath);
try {
await fsPromises.writeFile(temporaryPath, JSON.stringify(value, null, 2), 'utf8');
await fsPromises.rename(temporaryPath, filePath);
} catch (error) {
await fsPromises.rm(temporaryPath, { force: true }).catch(() => {});
throw error;
}
};
const withWriteLock = async (projectId, mutate) => {