Skip to content

Fix Android PDF crash: IllegalStateException: Already closed (#989)#999

Open
coolsoftwaretyler wants to merge 1 commit intowonday:masterfrom
coolsoftwaretyler:fix/android-pdf-crash-989
Open

Fix Android PDF crash: IllegalStateException: Already closed (#989)#999
coolsoftwaretyler wants to merge 1 commit intowonday:masterfrom
coolsoftwaretyler:fix/android-pdf-crash-989

Conversation

@coolsoftwaretyler
Copy link

Summary

Fixes the Android crash java.lang.IllegalStateException: Already closed reported in wonday/react-native-pdf#989.

Change

Set AlreadyClosedBehavior.IGNORE via global pdfiumandroid config (PdfView.java)

  • Calls ConfigKt.setPdfiumConfig(new Config(new DefaultLogger(), AlreadyClosedBehavior.IGNORE)) in the PdfView constructor
  • This makes pdfiumandroid silently ignore attempts to close already-closed resources instead of throwing IllegalStateException. 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

  • Build on Android and verify no compilation errors
  • Load a multi-page PDF, scroll rapidly back and forth — should not crash
  • Navigate away from the PDF screen while scrolling — should not crash
  • Load a new PDF while the current one is still rendering — should not crash
  • Verify iOS still works (no Android changes affect iOS)

)

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments