Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions packages/react-pdf/src/Document.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,14 @@ describe('Document', () => {

vi.mocked(globalThis.console.error).mockRestore();
});

it('does not throw an error on unmount', async () => {
const { func: onLoadProgress, promise: onLoadProgressPromise } = makeAsyncCallback();

const { unmount } = render(<Document file={pdfFile} onLoadProgress={onLoadProgress} />);

await onLoadProgressPromise;

expect(unmount).not.toThrowError();
});
});
12 changes: 6 additions & 6 deletions packages/react-pdf/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import useResolver from './shared/hooks/useResolver.js';

import type { PDFDocumentProxy } from 'pdfjs-dist';
import type { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api.js';
import type { EventProps } from 'make-event-props';
import type {
ClassName,
Expand Down Expand Up @@ -507,10 +508,9 @@ const Document: React.ForwardRefExoticComponent<
return;
}

const documentInitParams: Source = {
...source,
...options,
};
const documentInitParams: DocumentInitParameters = options
? { ...source, ...options }
: source;

const destroyable = pdfjs.getDocument(documentInitParams);
if (onLoadProgress) {
Expand All @@ -521,7 +521,7 @@ const Document: React.ForwardRefExoticComponent<
}
const loadingTask = destroyable;

loadingTask.promise
const loadingPromise = loadingTask.promise
.then((nextPdf) => {
pdfDispatch({ type: 'RESOLVE', value: nextPdf });
})
Expand All @@ -534,7 +534,7 @@ const Document: React.ForwardRefExoticComponent<
});

return () => {
loadingTask.destroy();
loadingPromise.finally(() => loadingTask.destroy());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change introduced a race condition when quickly switching to a new PDF file. The cleanup could run asynchronously after the new PDF started loading and cause a crash.

It's now fixed with PRs mozilla/pdf.js#20503, Expensify/react-fast-pdf#51, Expensify/react-fast-pdf#52, and Expensify/App#80160.

};
},
[options, pdfDispatch, source],
Expand Down