* test(#4091): RED — worker cache write must be rename-atomic Structural regression guard: gsd-check-update-worker.js must not write the shared per-package cache in place; assert temp+rename publish shape plus a behavioral no-residue e2e. * fix(#4091): publish update-check cache via atomic temp+rename writeFileSync on the shared per-package cache truncates before writing, so concurrent cross-runtime readers (statusline/banner) could see a torn or empty record. Stage under a unique same-directory temp and rename into place — POSIX rename(2) is atomic, so readers see the old or new record, never a partial one. Degrade policy (#3582) unchanged: errors swallowed, temp best-effort removed. * refactor(#4091): hoist temp path, tighten uniqueness assertion (review) * changeset(#4091): add fragment (pr backfill to follow) * changeset(#4091): backfill PR 4313 --------- Co-authored-by: sim <sim@local>
This commit is contained in:
@@ -156,5 +156,22 @@ const result = {
|
||||
};
|
||||
|
||||
if (cacheFile) {
|
||||
try { fs.writeFileSync(cacheFile, JSON.stringify(result)); } catch (e) {}
|
||||
}
|
||||
// #4091: the cache file is shared per-PACKAGE across every runtime's worker
|
||||
// (#607/#1421), so concurrent statusline/banner readers parse it while this
|
||||
// worker writes it. A direct writeFileSync truncates before writing — a
|
||||
// reader landing mid-write sees a torn/empty record (its JSON.parse catch
|
||||
// swallows it, so the symptom is an intermittently blank update segment).
|
||||
// Publish atomically instead: stage under a unique same-directory temp
|
||||
// (same filesystem, so rename(2) is atomic — readers see the old or the new
|
||||
// record, never a partial one), then renameSync into place. Failure policy
|
||||
// is unchanged (#3582 degrade): any error is swallowed and the temp, if
|
||||
// left behind, is best-effort removed.
|
||||
const tmp = cacheFile + '.tmp-' + process.pid;
|
||||
try {
|
||||
fs.writeFileSync(tmp, JSON.stringify(result));
|
||||
fs.renameSync(tmp, cacheFile);
|
||||
} catch (e) {
|
||||
try {
|
||||
fs.rmSync(tmp, { force: true });
|
||||
} catch (e2) {}
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user