Repository navigation
fix(storage): atomic saves keep file modes and sync the credential bytes #2458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import { randomUUID } from "node:crypto"; | ||
| import type { | ||
| AppWelcomeVideoState, | ||
| AutoUpdatePreferences, | ||
|
|
@@ -21,6 +20,7 @@ import { projectRefStateKey } from "../../../shared/projectIdentity"; | |
| import type { SavedWindowRepositoryTabs } from "../../../shared/windowRepositoryTabs"; | ||
| import { sanitizePortableGitRemote } from "../../../shared/crossMachineHandoff"; | ||
| import type { MainWindowState } from "./mainWindowState"; | ||
| import { writeFileAtomic } from "./durableFile"; | ||
|
|
||
| export type RecentProjectRemote = RecentProjectRemoteRef; | ||
|
|
||
|
|
@@ -210,8 +210,6 @@ export function readGlobalStateForUpdate(filePath: string): GlobalState { | |
| } | ||
|
|
||
| export function writeGlobalState(filePath: string, state: GlobalState): void { | ||
| let tempPath: string | null = null; | ||
| let fd: number | null = null; | ||
| try { | ||
| const serialized = `${JSON.stringify(state, null, 2)}\n`; | ||
| // The file already holds this state (the window restores the tabs it | ||
|
|
@@ -221,33 +219,12 @@ export function writeGlobalState(filePath: string, state: GlobalState): void { | |
| } catch { | ||
| // Missing or unreadable: write it. | ||
| } | ||
| const dir = path.dirname(filePath); | ||
| fs.mkdirSync(dir, { recursive: true }); | ||
| tempPath = path.join(dir, `.${path.basename(filePath)}.${process.pid}.${randomUUID()}.tmp`); | ||
| fd = fs.openSync(tempPath, "w"); | ||
| fs.writeFileSync(fd, serialized, "utf8"); | ||
| fs.fsyncSync(fd); | ||
| fs.closeSync(fd); | ||
| fd = null; | ||
| fs.renameSync(tempPath, filePath); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Non-atomic overwrite of desktop settings When Windows blocks replacement but allows writes, Learn moreThe desktop keeps preferences, recent projects, and update recovery data in one JSON file. The previous save used only a rename to publish new bytes, so a refused rename left the previous bytes untouched. The shared replacement helper first retries the rename, then tries a staged rename, and finally copies directly to the destination for certain repeated rename errors. The last operation is not atomic and does not flush the copied destination file. readGlobalState treats malformed JSON as empty state. Example: While the user changes a preference, a Windows reader holds the settings file open with write sharing but without delete sharing. Both renames fail, so the save copies directly onto the settings file. If that copy is interrupted, the next launch can find incomplete JSON instead of the previous settings. Recommended fix: Give global-state writes a rename-only replacement policy. Keep transient rename retries, but never let this caller fall back to copying over the live file. Verify that a persistent rename refusal leaves the original bytes untouched. Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| tempPath = null; | ||
| try { | ||
| const dirFd = fs.openSync(dir, "r"); | ||
| try { | ||
| fs.fsyncSync(dirFd); | ||
| } finally { | ||
| fs.closeSync(dirFd); | ||
| } | ||
| } catch { | ||
| // Directory fsync is best effort across filesystems/platforms. | ||
| } | ||
| fs.mkdirSync(path.dirname(filePath), { recursive: true }); | ||
| // One atomic writer for the whole app: it keeps the replaced file's | ||
| // permission bits (this hand-rolled save widened a 0o600 state file under | ||
| // the process umask) and syncs the temp file and its directory. | ||
| writeFileAtomic(filePath, serialized, { fsync: true }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Settings replaced after failed disk flush When the temp file’s disk flush fails, Learn moreDesktop settings, recent projects, and pending update records share one JSON file. Desktop startup reads it as a unit. The previous writer abandoned the temporary file when flushing its contents failed. The shared atomic writer now proceeds to replacement after its flush helper catches the error. If a real writeback error leaves incomplete bytes, readGlobalState falls back to empty state on the next launch. Example: A recent-project update fits in the filesystem cache, but delayed allocation returns Recommended fix: Use a strict regular-file flush for global state and propagate its failure before replacing the destination. Directory flush can remain best effort. Exercise this path with an injected temp-file Was this helpful? React with 👍 or 👎 to provide feedback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/desktop/src/main/services/state/globalState.ts
Line: 226
Comment:
**Saved state can be truncated**
`writeGlobalState` now reaches a fallback that copies directly over the saved file when both rename attempts are refused. On Windows, another program holding the file open can trigger this path. Readers can see incomplete JSON, and a failed copy can destroy the previous state. The old writer left the saved file untouched when rename failed. Keep this caller on a rename-only path, even if that means skipping the update.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| } catch { | ||
| if (fd != null) { | ||
| try { fs.closeSync(fd); } catch {} | ||
| } | ||
| if (tempPath) { | ||
| try { fs.unlinkSync(tempPath); } catch {} | ||
| } | ||
| // Non-fatal; global state is a convenience. | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temp created before mode is applied
Medium Severity
writeFileByDescriptorstill opens the sibling temp with0o666(umask-filtered, typically0644) and only thenfchmods the preserved mode. A local observer can open that temp in the create-to-chmod window and keep the fd after the mode is narrowed, so a0600.envis readable while its bytes land.writeFileAtomicalready passes the intended mode intoopenso the file is never born wider.Additional Locations (1)
apps/desktop/src/main/services/shared/utils.ts#L978-L986Reviewed by Cursor Bugbot for commit 2d57067. Configure here.