Skip to content

Commit e2b6bf1

Browse files
committed
fix(data): prevent stale joins, timestamp shared values, and signal gap for null-cursor upstreams
- Record seed keys in askedFor so the first reload after hydration sends fresh: true and avoids joining a stale in-flight request from another tab. - Add wall-clock timestamp to the owner's value broadcast so replicas can compare fetchedAt and counter-offer when their value is newer, preventing an older load from overwriting a newer seed. - Signal gap: true when a joiner has a cursor but the upstream's cursor is still null, so missed events between the seed and the connection's first position trigger a reload.
1 parent baace73 commit e2b6bf1

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ type ToReplica =
139139
| { readonly t: 'failed'; readonly id: number; readonly error: unknown }
140140
| { readonly t: 'frame'; readonly id: number; readonly frame: FeedFrame }
141141
| { readonly t: 'end'; readonly id: number; readonly error?: unknown }
142-
/** Another replica loaded a read. */
143-
| { readonly t: 'value'; readonly name: string; readonly input: unknown; readonly value: unknown }
142+
/** Another replica loaded a read; `at` is the wall-clock time it loaded. */
143+
| {
144+
readonly t: 'value';
145+
readonly name: string;
146+
readonly input: unknown;
147+
readonly value: unknown;
148+
readonly at: number;
149+
}
144150
/** A value another replica holds, which the owner passes on as it came. */
145151
| Offer
146152
/** Another replica sent a write. */
@@ -442,6 +448,7 @@ export function createDataOwner(options: {
442448
name: payload.name,
443449
input: payload.input,
444450
value: reply.value,
451+
at: hostDateNow(),
445452
};
446453
for (const port of replicas) if (!asked.get(started)!.has(port)) post(port, value);
447454
},
@@ -478,7 +485,9 @@ export function createDataOwner(options: {
478485
id,
479486
frame:
480487
cursor === null
481-
? {}
488+
? payload.cursor !== null
489+
? { gap: true }
490+
: {}
482491
: { cursor, gap: payload.cursor !== null && payload.cursor !== cursor },
483492
} satisfies ToReplica);
484493
return;
@@ -905,7 +914,12 @@ export function createReplica(
905914
const held = inner.held(entry);
906915
if (held) send({ t: 'offer', ...held });
907916
};
908-
inner.onSeed(offer);
917+
inner.onSeed((entry) => {
918+
const key = `read\n${entry.key}`;
919+
askedFor.add(key);
920+
if (askedFor.size > 512) askedFor.delete(askedFor.values().next().value!);
921+
offer(entry);
922+
});
909923

910924
/**
911925
* Take a value another replica holds, if it loaded later than the one held
@@ -929,18 +943,20 @@ export function createReplica(
929943
}
930944

931945
/** Take a value another replica loaded, for a read held here and not loading. */
932-
async function value(name: string, input: unknown, raw: unknown) {
946+
async function value(name: string, input: unknown, raw: unknown, at: number) {
933947
const entry: Entry | undefined = inner.wireEntry(entryKey(name, input));
934948
if (!entry || entry.inflight || store.retired) return;
949+
if (entry.hasBase && entry.fetchedAt >= at) return offer(entry);
935950
let parsed: unknown;
936951
try {
937952
parsed = await parse(entry.read.output, raw);
938953
} catch {
939954
return;
940955
}
941956
if (store.retired || entry.inflight || inner.entries.get(entry.key) !== entry) return;
957+
if (entry.fetchedAt >= at) return;
942958
const reload = new Set<Entry>();
943-
inner.rebase(entry, parsed, false, reload);
959+
inner.rebase(entry, parsed, false, reload, Math.max(0, hostDateNow() - at));
944960
inner.finish(reload, [entry], entry.names);
945961
}
946962

@@ -986,7 +1002,7 @@ export function createReplica(
9861002
feeds.get(message.id)?.end(message.error);
9871003
break;
9881004
case 'value':
989-
void value(message.name, message.input, message.value);
1005+
void value(message.name, message.input, message.value, message.at);
9901006
break;
9911007
case 'offer':
9921008
void offered(message);

0 commit comments

Comments
 (0)