Data safety (2026-11-13 incident: a deploy read a half-written rotator.json, the store fell back to empty and its next save wiped 294 spin links): safefs.js makes every .json write inside DATA_DIR atomic (temp file + rename); LinkSpin's rotator refuses to start empty when rotator.json exists but will not parse

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-13 09:11:27 -06:00
parent 656c9a940d
commit d1c9d62801
3 changed files with 39 additions and 1 deletions
+11 -1
View File
@@ -18,7 +18,17 @@ const DAY = 86400000;
const J = {
db: null,
FILE: () => path.join(DATA_DIR, 'rotator.json'),
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, rotations: [], hits: [], conv: [] }; } if (!this.db.conv) this.db.conv = []; },
// a missing file starts empty; a file that is there but will not parse is NEVER replaced with an empty list (2026-11-13: that
// fallback wiped 294 spin links when a deploy read a half-written file). Retry a moment, then stop the app so it restarts.
load() {
let last = null;
for (let i = 0; i < 10; i++) {
try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); last = null; break; }
catch (e) { last = e; if (e.code === 'ENOENT') { this.db = { v: 1, rotations: [], hits: [], conv: [] }; last = null; break; } Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 500); }
}
if (last) throw new Error('rotator.json exists but cannot be read (' + last.message + '); refusing to start with no spin links');
if (!this.db.conv) this.db.conv = [];
},
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} }
};
function init(opts) { DATA_DIR = opts.dataDir; geo = opts.geo || null; OWN_HOSTS = opts.ownHosts || (() => []); SOURCES = opts.sources || null; if (opts.wantsClickId) WANTS_CLICK_ID = opts.wantsClickId; if (opts.isNetworkHost) IS_NETWORK = opts.isNetworkHost; if (opts.goalRows) GOAL_ROWS = opts.goalRows; J.load(); }