Repository navigation
A failed settings save can no longer empty config.json - #3473
Merged
Merged
Conversation
writeConfigAtomic fell back to a plain writeFileSync on the real file when
the temp-file-and-rename path failed. A plain write truncates first, so one
that stopped partway left config.json empty, and readConfigForSave then read
an empty file as {} and the next save wrote only its own keys.
The fallback is gone: a rename Windows reports as locked is retried for a
moment, anything else throws and the caller reports the save as failed with
the old bytes intact. The temp file is fsynced before the rename. An empty
config.json is now refused by every save like corrupt JSON; reads still
give defaults.
…d rename for 2 s An fsync error other than "no fsync here" (EINVAL, ENOTSUP) now stops the write before the rename: on a network home or under a quota the full-disk error can show up only at fsync, and the rename would put a short file in place of config.json. An empty config.json holds nothing to lose and is saved over again. A rename Windows reports as locked is retried with backoff for about 2 s, touching the config lock so it never looks stale.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
How settings get lost today
writeConfigAtomic. It writes a temp file beside config.json, then renames it over config.json.writeFileSync(targetPath, contents). A plain write empties the file first. If that write also stops partway (the disk is still full, an I/O error, the process is killed), config.json is left empty or cut short.Why the fallback existed
#1364 added the fallback together with the temp-and-rename write, which replaced a plain write. Its comment says: "Same-directory rename should not fail, but a write that lands is better than a settings change that is lost to an exotic filesystem." No test pinned the fallback. These are the cases it actually served:
realpathSyncand the temp file goes beside the target, so the rename never replaces the link.EPERM/EACCES/EBUSYuntil that program lets go.The fix, per case
writeConfigAtomicdeletes the temp file and throws.saveConfigandsaveCookieHostalready catch that: they print the existingfailed to write config.jsonwarning and report the save as failed (false/failed). The old bytes stay.state.jsonuses the same writer, so it gets the same protection.ENOSPC,EDQUOT,EIO, ...) therefore stops the write before the rename, and config.json keeps its bytes. The only errors ignored areEINVAL,ENOTSUPandEOPNOTSUPP, which mean "this filesystem has no fsync".EPERM/EACCES/EBUSY, the rename is retried with backoff for about 2 s in total. Before each wait the config lock's mtime is refreshed, so another writer never takes the lock over as stale (its stale window is 1.5 s). If the file is still held after that, the save fails as above and the old file is untouched. Other errors are not retried..bakcopy beside config.json, and a write in place there risks exactly the empty file this PR removes. In that setup the lock file and state.json already cannot be written either, and the lock warning already shows.Tests
packages/shared/config.test.ts:loadConfiggives{}, the save succeeds, and nothing is warned.__setConfigWriteForTest({ rename, fsync, sleep }), built like the existing lock seams. ForENOSPCandEPERM, the save returnsfalse, config.json is byte for byte the same, and no.tmpfile is left.ENOSPCis tried once with no wait.EPERMis retried with growing waits that add up to 1.8–2.5 s, and the lock is held during every wait.EBUSYsix times and then success, so the save lands with no warning.ENOSPC,EDQUOTandEIO, the rename never runs, the bytes are intact and no temp file is left. ForEINVALandENOTSUP, the save lands.0555with a writable config.json. The save fails and the file keeps its bytes. The old code wrote the file in place here.0555folder, pointing into a writable folder. The save goes through the link, and the link survives.apps/pi-extension/vendored-config-write.test.ts: in the vendored copy, a failed rename and a failed fsync both keep config.json's bytes, and an empty config.json is saved over.Results: config.test.ts 160 pass, vendored-config-write.test.ts 4 pass, and these neighbouring files run one at a time all pass:
config.auto-update,data-dir,inbox-reminder-text,shared-handlers, bothupdate-advertfiles andupdate-lifecycle.model.tsc --noEmitis clean forpackages/sharedandapps/pi-extension. One sentence is added todocs/agent-guides/environment.md.e2e: none
Not verified
EBUSY/EPERM, not a really locked file.#3458 (which this PR was built on) is merged, and
origin/mainis merged into this branch, so the diff shows only this change.