clawwork-ai/ClawWork

[Bug] writeConfig is not atomic — mid-write crash corrupts user config

Open

#385 建立於 2026年4月15日

在 GitHub 查看
 (3 留言) (0 反應) (0 負責人)TypeScript (63 fork)github user discovery
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:

  1. Write the encoded JSON to cfgPath + '.tmp' with the same mode: 0o600.
  2. Call renameSync(cfgPath + '.tmp', cfgPath)rename is atomic on both POSIX and NTFS.
  3. On write failure, best-effort clean up the temp file before re-throwing the original error.

Verification

  1. Run pnpm check — must pass.
  2. Manual: force-kill the process immediately after a writeConfig invocation, 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

貢獻者指南