clawwork-ai/ClawWork
在 GitHub 查看[Bug] writeConfig is not atomic — mid-write crash corrupts user config
Open
#385 建立於 2026年4月15日
area/artifacthelp wantedkind/bug
倉庫指標
- Star
- (519 star)
- PR 合併指標
- (PR 指標待抓取)
描述
Problem
writeConfig calls writeFileSync directly on the target path. If the process is killed or the machine loses power mid-write, the config file is left in a partial state — subsequent readConfig calls trigger a JSON parse error and the user loses their entire configuration (see the companion silent-null bug in the same file).
Location
File: packages/desktop/src/main/workspace/config.ts:168-172
export function writeConfig(config: AppConfig): void {
const cfgPath = configFilePath();
const encrypted = encryptGatewayCredentials(config);
writeFileSync(cfgPath, JSON.stringify(encrypted, null, 2), { encoding: 'utf-8', mode: 0o600 });
}
Fix Approach
Use the classic write-to-temp-then-rename pattern:
- Write the encoded JSON to
cfgPath + '.tmp'with the samemode: 0o600. - Call
renameSync(cfgPath + '.tmp', cfgPath)—renameis atomic on both POSIX and NTFS. - On write failure, best-effort clean up the temp file before re-throwing the original error.
Verification
- Run
pnpm check— must pass. - Manual: force-kill the process immediately after a
writeConfiginvocation, relaunch — config must be either the old version or the new version, never a half-written mix.
Context
- WG: Artifact & File System
- Priority: Low
- Estimated effort: 20-30 minutes