Skip to content

Commit beb056f

Browse files
committed
fix(data): do not overwrite unsaved kept reply with a newer one
When overlapping loads complete for the same key, the newer reply overwrote the kept map entry before the older tab flushed its valueless save. If the newer reply was never saved (validation failed, or the tab never persisted it), the already-validated older value was silently dropped, causing data loss on restart. Guard kept.set so an existing entry whose at exceeds newestSaved is never overwritten. The newer tab receives kept: false and sends the full value in its save instead.
1 parent 885557e commit beb056f

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎packages/octane/src/data/owner.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,14 @@ export function createDataOwner(options: {
497497
(reply) => {
498498
if (!reply.ok) return;
499499
// Kept for the tabs that save it, unless a newer request's reply was kept or saved.
500-
const newer = kept.get(cacheKey)?.at ?? newestSaved.get(cacheKey) ?? 0;
501-
if (asking.keep && !receipt && options.persist && newer < at) {
502-
kept.set(cacheKey, { at, value: reply.value });
500+
const existing = kept.get(cacheKey);
501+
const savedAt = newestSaved.get(cacheKey) ?? 0;
502+
if (asking.keep && !receipt && options.persist && (existing?.at ?? savedAt) < at) {
503+
// An unsaved kept reply must not be overwritten: the tab it was
504+
// promised to will send a valueless save that needs the value here.
505+
if (!existing || existing.at <= savedAt) {
506+
kept.set(cacheKey, { at, value: reply.value });
507+
}
503508
}
504509
// A newer read has replaced this one; its value is the one to share.
505510
if (newest.get(key) !== started) return;

0 commit comments

Comments
 (0)