Fix Android PDF crash: IllegalStateException: Already closed (#989)#999
Open
coolsoftwaretyler wants to merge 1 commit intowonday:masterfrom
Open
Fix Android PDF crash: IllegalStateException: Already closed (#989)#999coolsoftwaretyler wants to merge 1 commit intowonday:masterfrom
coolsoftwaretyler wants to merge 1 commit intowonday:masterfrom
Conversation
) Set AlreadyClosedBehavior.IGNORE via ConfigKt.setPdfiumConfig() in the PdfView constructor. This makes pdfiumandroid silently ignore attempts to close already-closed resources instead of throwing IllegalStateException. The crash is caused by a race condition between the background rendering thread (RenderingHandler) and the main thread calling recycle() during component unmount. Closing an already-closed resource is semantically a no-op, so IGNORE is safe. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Fixes the Android crash
java.lang.IllegalStateException: Already closedreported in wonday/react-native-pdf#989.Change
Set
AlreadyClosedBehavior.IGNOREvia global pdfiumandroid config (PdfView.java)ConfigKt.setPdfiumConfig(new Config(new DefaultLogger(), AlreadyClosedBehavior.IGNORE))in thePdfViewconstructorIllegalStateException. We could potentially warn or log an error if we want. For now, this just ignores them.Reproducer
A standalone reproducer app is available at coolsoftwaretyler/pdf-989-reproduction, with a PR demonstrating the fix.
The reproducer uses a realistic document viewer with nested navigation (native stack + bottom tabs) and real PDF documents. The crash reproduces when navigating back from a PDF with heavy graphics (e.g. the Skimflow NP Product Overview brochure).
Interestingly, only the Product Overview doc caused the crash, not the technical data sheet. I'm not sure why that was the case - trying to figure out what the difference is.
Screen recordings
From the reproducer PR:
Before fix — crashes with
IllegalStateException: Already closed:pdf-reproduces.webm
After fix — double-close silently ignored, no crash:
no-crash.webm
Test plan