Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions packages/loader/driver-utils/src/prefetchDocumentStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ export class PrefetchDocumentStorageService extends DocumentStorageServiceProxy
public async getSnapshotTree(version?: IVersion): Promise<ISnapshotTree | null> {
const p = this.internalStorageService.getSnapshotTree(version);
if (this.prefetchEnabled) {
// We don't care if the prefetch succeeds
void p.then((tree: ISnapshotTree | null | undefined) => {
if (tree === null || tree === undefined) {
return;
}
this.prefetchTree(tree);
});
// Fire-and-forget prefetch - we don't care if it succeeds.
// The .catch() prevents unhandled rejection when p rejects, since
// p.then() creates a derived promise that also rejects if p rejects.
// Callers awaiting the returned p will still receive the error.
void p
.then((tree: ISnapshotTree | null | undefined) => {
if (tree === null || tree === undefined) {
return;
}
this.prefetchTree(tree);
})
.catch(() => {
// Intentionally empty - error will be handled by caller awaiting p
});
}
return p;
}
Expand Down Expand Up @@ -81,17 +88,19 @@ export class PrefetchDocumentStorageService extends DocumentStorageServiceProxy
this.prefetchTreeCore(tree, secondary);

for (const blob of secondary) {
// We don't care if the prefetch succeeds
void this.cachedRead(blob);
// Fire-and-forget prefetch. The .catch() prevents unhandled rejection
// since cachedRead is async and returns a separate promise chain.
this.cachedRead(blob).catch(() => {});
}
}

private prefetchTreeCore(tree: ISnapshotTree, secondary: string[]): void {
for (const [blobKey, blob] of Object.entries(tree.blobs)) {
if (blobKey.startsWith(".") || blobKey === "header" || blobKey.startsWith("quorum")) {
if (blob !== null) {
// We don't care if the prefetch succeeds
void this.cachedRead(blob);
// Fire-and-forget prefetch. The .catch() prevents unhandled rejection
// since cachedRead is async and returns a separate promise chain.
this.cachedRead(blob).catch(() => {});
}
} else if (!blobKey.startsWith("deltas")) {
if (blob !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MockStorageService implements Partial<IDocumentStorageService> {
public readBlobCalls: string[] = [];
public shouldFail = false;
public failureError = new Error("Mock read failure");
public shouldGetSnapshotTreeFail = false;
public getSnapshotTreeError = new Error("Mock getSnapshotTree failure");

public async readBlob(blobId: string): Promise<ArrayBufferLike> {
this.readBlobCalls.push(blobId);
Expand All @@ -47,6 +49,9 @@ class MockStorageService implements Partial<IDocumentStorageService> {
}

public async getSnapshotTree(): Promise<ISnapshotTree | null> {
if (this.shouldGetSnapshotTreeFail) {
throw this.getSnapshotTreeError;
}
return {
blobs: {
".metadata": "blob1",
Expand Down Expand Up @@ -160,4 +165,27 @@ describe("PrefetchDocumentStorageService", () => {
"Explicit readBlob should still receive the error",
);
});

it("should not cause unhandled rejections when getSnapshotTree fails", async () => {
// Set up getSnapshotTree to fail (e.g., network timeout)
const networkError = new Error("Socket timeout");
mockStorage.shouldGetSnapshotTreeFail = true;
mockStorage.getSnapshotTreeError = networkError;

// getSnapshotTree internally does: void p.then(...).catch(...)
// Without the .catch(), if p rejects, the derived promise from .then() also
// rejects and causes an unhandled rejection. This test verifies the fix.
await assert.rejects(
async () => prefetchService.getSnapshotTree(),
(error: Error) => error.message === "Socket timeout",
"Caller should receive the error",
);

// Allow microtask queue to flush - if there's an unhandled rejection,
// it would surface here or cause the test to fail
await Promise.resolve();
await new Promise((resolve) => setTimeout(resolve, 10));

// If we reach here without unhandled rejection, the test passes
});
});