EloAdaptor: waitForReachingServerVersion() never resolves on a one-shot backfill (compare runs before the un-awaited imports land)
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 76/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- typescript
- Bereich
- distributed-systems
Rechercherichtung
Start in packages/loro-adaptors/src/elo-adaptor.ts, focusing on applyUpdate(), importEloContainer(), and waitForReachingServerVersion(). Run the minimal reproduction with the listed loro-adaptors, loro-crdt, loro-protocol, and Bun or Node versions, then verify that a one-shot backfill resolves the wait without a redundant update. Add a regression test covering the EloAdaptor path and the existing LoroAdaptor control case.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Summary
EloAdaptor.waitForReachingServerVersion() never resolves when the whole backfill arrives in a single applyUpdate call (the normal join path), even though the document does receive the data. The same scenario with LoroAdaptor (%LOR) resolves immediately.
Cause
EloAdaptor.applyUpdate fires importEloContainer(...) for each container without awaiting it (decryption is async), then compares this.doc.version() with initServerVersion in the same synchronous tick. The comparison therefore runs against the version before the imports land, and nothing re-runs it once they do:
// packages/loro-adaptors/src/elo-adaptor.ts (main, unchanged since 6570a2ee)
applyUpdate(updates: Uint8Array[]): void {
if (this.destroyed || !updates?.length) return;
for (const containerBytes of updates) {
try {
this.importEloContainer(containerBytes).catch(err => { ... }); // async, not awaited
} catch (error) { ... }
}
if (this.initServerVersion && !this.hasReachedServerVersion) {
const cmp = this.doc.version().compare(this.initServerVersion); // pre-import version
if (cmp != null && cmp >= 0) this.reachServerVersionPromise.resolve();
}
}
The promise resolves only when the server version is empty (handleJoinOk), or if some later unrelated update happens to arrive on the room and re-runs the compare.
Minimal reproduction
[email protected], [email protected], [email protected], Bun 1.x (Node behaves the same). No other code.
// Minimal repro: EloAdaptor.waitForReachingServerVersion() never resolves on a
// one-shot backfill. No application code: [email protected] + loro-crdt only.
import { LoroDoc } from "loro-crdt";
import { EloAdaptor, LoroAdaptor } from "loro-adaptors/loro";
const key = new Uint8Array(32).fill(7);
const config = { getPrivateKey: async () => ({ keyId: "k1", key }) };
const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
const ctx = (sink: Uint8Array[][]) =>
({
send: (u: Uint8Array[]) => void sink.push(u),
onImportError: (e: Error) => { throw e; },
onJoinFailed: (reason: string) => { throw new Error(reason); },
}) as any;
const race = (p: Promise<void>, ms: number) =>
Promise.race([p.then(() => "resolved"), sleep(ms).then(() => `still pending after ${ms} ms`)]);
async function scenario(label: string, make: (doc: LoroDoc) => EloAdaptor | LoroAdaptor) {
// 1. A peer with content joins an empty room: its adaptor sends the snapshot.
const docA = new LoroDoc();
docA.setPeerId(1n);
docA.getMap("m").set("hello", "world");
docA.commit();
const sentByA: Uint8Array[][] = [];
const a = make(docA);
a.setCtx(ctx(sentByA));
await a.handleJoinOk({ version: new Uint8Array(0) } as any);
const backfill = sentByA.flat();
const serverVersion = docA.version().encode();
// 2. A fresh peer joins the room whose server version is A's version, then
// receives the whole backfill in ONE applyUpdate call (what a join does).
const docB = new LoroDoc();
docB.setPeerId(2n);
const b = make(docB);
b.setCtx(ctx([]));
await b.handleJoinOk({ version: serverVersion } as any);
const waiting = b.waitForReachingServerVersion();
b.applyUpdate(backfill);
const outcome = await race(waiting, 2000);
const cmp = docB.version().compare(docA.version());
console.log(`[${label}] backfill containers: ${backfill.length}`);
console.log(`[${label}] waitForReachingServerVersion(): ${outcome}`);
console.log(`[${label}] docB has the data: ${docB.getMap("m").get("hello") === "world"}; docB.version() vs server: ${cmp} (0 = equal)`);
if (outcome !== "resolved") {
// Proof that only the timing of the compare is wrong: a second, redundant
// applyUpdate re-runs the compare after the first import landed.
b.applyUpdate(backfill);
console.log(`[${label}] after a second (redundant) applyUpdate: ${await race(waiting, 500)}`);
}
console.log("");
}
await scenario("EloAdaptor (%ELO)", (doc) => new EloAdaptor(doc, config));
await scenario("LoroAdaptor (%LOR, control)", (doc) => new LoroAdaptor(doc));
Output:
[EloAdaptor (%ELO)] backfill containers: 1
[EloAdaptor (%ELO)] waitForReachingServerVersion(): still pending after 2000 ms
[EloAdaptor (%ELO)] docB has the data: true; docB.version() vs server: 0 (0 = equal)
[EloAdaptor (%ELO)] after a second (redundant) applyUpdate: resolved
[LoroAdaptor (%LOR, control)] backfill containers: 1
[LoroAdaptor (%LOR, control)] waitForReachingServerVersion(): resolved
[LoroAdaptor (%LOR, control)] docB has the data: true; docB.version() vs server: 0 (0 = equal)
The third line shows the document is complete while the promise is still pending; the fourth shows that merely re-running the comparison after the import resolves it, so the only defect is the timing of the compare.
Suggested fix
Run the comparison after the imports have settled, e.g.
applyUpdate(updates: Uint8Array[]): void {
if (this.destroyed || !updates?.length) return;
const imports = updates.map(bytes =>
this.importEloContainer(bytes).catch(err => this.ctx?.onImportError(toError(err), [bytes]))
);
void Promise.allSettled(imports).then(() => {
if (this.destroyed) return;
if (this.initServerVersion && !this.hasReachedServerVersion) {
const cmp = this.doc.version().compare(this.initServerVersion);
if (cmp != null && cmp >= 0) this.reachServerVersionPromise.resolve();
}
});
}
(Alternatively, subscribe to the doc's import events and compare there, which also covers imports triggered outside applyUpdate.)
Impact
Any client that gates "initial sync done" on waitForReachingServerVersion() for an %ELO room waits forever on a one-shot join backfill and has to hand-roll a content-side wait instead. Happy to open a PR with the fix above and a test if you want one.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 40
- Forks
- 12
- Ø Merge
- 4 T. 1 Std.
- Gemergte PRs (30 T.)
- 1
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus loro-dev/protocol
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 25/100
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
-
bug
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 48/100
-
bug
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
Alle Issues in loro-dev/protocol
Ähnliche Issues
-
fix(frontend): correct VITE_IS_DEVELPOMENT typo in shared/app.ts and align dev environment flagsOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
vicharanashala/ajrasakha#1439 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Add: TV5Monde AsieOffenapproved check:passed streams:add
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
backnotprop/plannotator#1932 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 62/100
ibm-granite/granite.build#504 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
reproduced
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
anomalyco/opencode#55676 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag