test(settings): poll again when settings.json is read half-written

This commit is contained in:
Devvyn committed 2026-10-24 00:32:20 +08:00
1 parent ce7d198b91
commit f15afe1de3
1 file changed
+6 -2
+6 -2
View File
@@ -57,8 +57,12 @@ async function send(method: string, path: string, headers: Record<string, string
async function settleSettings(predicate: (s: Record<string, unknown>) => boolean): Promise<Record<string, unknown>> {
let last: Record<string, unknown> = {};
for (let i = 0; i < 40; i++) {
last = JSON.parse(await readSettingsBytes());
if (predicate(last)) return last;
try {
last = JSON.parse(await readSettingsBytes());
if (predicate(last)) return last;
} catch {
// the write is not atomic: a read that lands between its truncate and its data is empty, so poll again
}
await new Promise((r) => setTimeout(r, 25));
}
return last;