Fix IllegalStateException: Already closed in PDF viewer#142
Open
Fix IllegalStateException: Already closed in PDF viewer#142
Conversation
When switching view modes (single/continuous), the Pdf component was
unmounted and remounted via key={viewMode}. This caused a race condition
where the native PdfiumAndroid rendering thread was still rendering a
page bitmap when the document was closed during unmount.
Fix: explicitly unmount the Pdf component before changing the view mode,
then remount on the next animation frame. This gives the native rendering
thread time to complete before the document resources are released.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Sentry reported an
IllegalStateException: Already closedcrash in the PDF viewer on Android (Sentry issue).The crash originates from
io.legere.pdfiumandroid.util.ConfigKt.handleAlreadyClosedwhen the native PdfiumAndroid rendering thread tries to render a page bitmap after the PDF document has already been closed.Root cause: When switching between single-page and continuous view modes, the
Pdfcomponent useskey={viewMode}which causes React to unmount the old instance and mount a new one synchronously. During unmount, the native PDF document is closed, but the backgroundRenderingHandlerthread may still be mid-render, leading to theAlready closedexception.Fix
pdfMountedstate that explicitly unmounts thePdfcomponent before changing the view moderequestAnimationFrame, giving the native rendering thread time to finish before document resources are releasedkey={viewMode}is preserved so React still creates a fresh instance with the new configurationStack trace