fix: serialize concurrent event-log syncs to stop duplicate ingestion
trySyncWithBackend's pulled-entry dedup is a non-atomic read-modify-write (read the existing (authorPubkey,signature) set, then appendEntry in a loop). Several mini-app mounts plus the shell poll can fire it at once; two overlapping runs both saw a remote entry as absent and ingested it twice, duplicating it locally and corrupting the replayed ledger (a real-browser pull of 26 relay events produced 156 = 6x local rows). Chain all calls on one promise so only one runs at a time, making the dedup atomic. Adds a 4-concurrent-sync regression test.
This commit is contained in:
1 parent
322ef20cad
commit
afa29a70f3
2 files changed
+50
-1
No files matched your search
@@ -107,4 +107,26 @@ describe("trySyncWithBackend", () => {
|
||||
expect(await db.eventLog.count()).toBe(countBefore);
|
||||
expect(computeBalances(await readAll()).balances["remoteAcct"]).toBe(42);
|
||||
});
|
||||
|
||||
it("coalesces concurrent syncs so one pulled entry is ingested exactly once (dedup race)", async () => {
|
||||
const wire = await signedWireEntry(
|
||||
{ type: "wallet.issue", account: "raceAcct", amount: 10 },
|
||||
3,
|
||||
);
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn().mockResolvedValue(okResponse({ stored: 0, entries: [wire], cursor: 1 })),
|
||||
);
|
||||
|
||||
const results = await Promise.all([
|
||||
trySyncWithBackend({ endpoint: "http://relay/api/sync" }),
|
||||
trySyncWithBackend({ endpoint: "http://relay/api/sync" }),
|
||||
trySyncWithBackend({ endpoint: "http://relay/api/sync" }),
|
||||
trySyncWithBackend({ endpoint: "http://relay/api/sync" }),
|
||||
]);
|
||||
|
||||
expect(results.every((r) => r.ok)).toBe(true);
|
||||
expect(await db.eventLog.count()).toBe(1);
|
||||
expect(computeBalances(await readAll()).balances["raceAcct"]).toBe(10);
|
||||
});
|
||||
});
|
||||
+28
-1
@@ -81,6 +81,20 @@ export function resetSyncCursors(): void {
|
||||
cursors.clear();
|
||||
}
|
||||
|
||||
const SYNC_NOOP: SyncResult = { ok: false, pushed: 0, pulled: 0 };
|
||||
|
||||
/**
|
||||
* One promise chain shared by every caller so concurrent sync requests run
|
||||
* one-at-a-time. Several mini-app mounts plus the shell's periodic poll can
|
||||
* fire `trySyncWithBackend()` at once; the pulled-entry dedup in `runSync` is a
|
||||
* non-atomic read-modify-write (read the `existing` (authorPubkey, signature)
|
||||
* set, then `appendEntry` in a loop), so two overlapping runs would both see a
|
||||
* remote entry as absent and ingest it twice - duplicating it in the local log
|
||||
* and corrupting the replayed ledger. Chaining each run after the previous one
|
||||
* settles makes that dedup effectively atomic.
|
||||
*/
|
||||
let syncChain: Promise<SyncResult> = Promise.resolve(SYNC_NOOP);
|
||||
|
||||
function bytesToHex(bytes: Uint8Array): string {
|
||||
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
@@ -126,6 +140,19 @@ function fromWire(wire: unknown): UnsignedEventLogEntry | null {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialized entry point (see `syncChain`): safe to call fire-and-forget from
|
||||
* any number of mini-app mounts and the shell's poll at once.
|
||||
*/
|
||||
export function trySyncWithBackend(options: SyncOptions = {}): Promise<SyncResult> {
|
||||
const result = syncChain.then(
|
||||
() => runSync(options),
|
||||
() => runSync(options),
|
||||
);
|
||||
syncChain = result.catch(() => SYNC_NOOP);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push new local events and pull new remote entries through the relay,
|
||||
* persisting each pulled entry via `appendEntry()` after a (authorPubkey,
|
||||
@@ -133,7 +160,7 @@ function fromWire(wire: unknown): UnsignedEventLogEntry | null {
|
||||
* resolves to `{ ok: false }` without throwing and without touching the local
|
||||
* log - safe to call on any mini-app mount.
|
||||
*/
|
||||
export async function trySyncWithBackend(
|
||||
async function runSync(
|
||||
options: SyncOptions = {},
|
||||
): Promise<SyncResult> {
|
||||
const endpoint = options.endpoint ?? "/api/sync";
|
||||
|
||||
Reference in new issue
Block a user