You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Crash on Android When Closing PDF Page After Scrolling || java.lang.IllegalStateException: Already closed #976
We are encountering an app crash on Android devices when a user is viewing a PDF using react-native-pdf and closes the page after scrolling through it. The issue is consistently reported in Firebase with an IllegalStateException: Already closed error in the stack trace.
Steps to Reproduce:
Open a PDF with the react-native-pdf component.
Scroll or slide through multiple pages.
Close the PDF viewing page (e.g., by navigating away or closing the screen).
The app crashes.
Stack Trace (Firebase Report):
E FATAL EXCEPTION: PDF renderer
Process: com.app, PID: [varies]
java.lang.IllegalStateException: Already closed
at io.legere.pdfiumandroid.util.ConfigKt.handleAlreadyClosed(Config.kt:20)
at io.legere.pdfiumandroid.PdfPage.close(PdfPage.kt:582)
at kotlin.io.CloseableKt.closeFinally(Closeable.kt:56)
at io.legere.pdfiumandroid.PdfiumCore.renderPageBitmap(PdfiumCore.kt:406)
at com.github.barteksc.pdfviewer.PdfFile.renderPageBitmap(PdfFile.java:301)
at com.github.barteksc.pdfviewer.RenderingHandler.proceed(RenderingHandler.java:109)
at com.github.barteksc.pdfviewer.RenderingHandler.handleMessage(RenderingHandler.java:66)
at android.os.Handler.dispatchMessage(Handler.java:117)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:293)
at android.os.HandlerThread.run(HandlerThread.java:110)
Environment:
Platform: Android
Library: react-native-pdf
react-native-pdf version: 7.0.0
React Native Version: 0.81.4
Affected on multiple Android device models
Expected Behavior:
Closing the PDF page should safely release resources without crashing the app.
Observed Behavior:
The app terminates with a fatal exception mentioning "Already closed" in the PDF renderer.
Additional Notes:
The issue is critical as it affects the core PDF viewing functionality and is being observed frequently in production.
Request:
Please investigate the page closure and resource disposal logic to ensure the PDF component handles repeated or premature closures gracefully. A fix or guidance to mitigate these crashes would be highly appreciated.
Reacted by Saifudheen P M, Logan-FE, Vince Abruzzese, Kadiryildirim1, Diego Segura, Jean, tree1891, Jordan, Vili Kinnunen and Yash Dahiwale
From what I can see, appearantly pdfiumandroid:1.0.33 was supposed to fix this, but for me it would crash when i built the app, 1.0.34 which recently released should be good.
I can't verify 100% that it works, but you can try for yourself by either overwriting the installed version in your own app/build.gradle file, or you could patch the package yourself in the library android/build.gradle i believe.
rnproject/android/app/build.gradle:
dependencies {
/** Other dependencies */
implementation("io.legere:pdfiumandroid:1.0.34")
}
diff --git a/android/build.gradle b/android/build.gradle
index 34da3248a540ad228acc7c4efdf294bf2f3b79e4..1635531ed1f984818efb13047dc792ff4d6ba747 100644
--- a/android/build.gradle+++ b/android/build.gradle@@ -143,6 +143,6 @@ dependencies {
// The repo from zacharee is based on PdfiumAndroidKt, a much newer fork of PdfiumAndroid, with better maintenance and updated native libraries.
implementation 'com.github.zacharee:AndroidPdfViewer:4.0.1'
// Depend on PdfiumAndroidKt directly so this can be updated independently of AndroidPdfViewer as updates are provided.
- implementation 'io.legere:pdfiumandroid:1.0.32'+ implementation 'io.legere:pdfiumandroid:1.0.34'
implementation 'com.google.code.gson:gson:2.13.2'
}
At the same time, update the version of pdfiumandroid
dependencies {
/** Other dependencies */
implementation("io.legere:pdfiumandroid:1.0.34")
}
By setting the parameter AlreadyClosedBehavior.IGNORE, you can solve the Java layer crash, and by setting the pdfiumandroid version to 1.0.34, you can solve the C++ layer crash
I'm not 100% sure, but it appears the issue doesn't happen when the PDF renderer is wrapped with memo and the page prop isn't used.
I found the same solution working for me too. It appears that any time props update the PDF is forced to re-render, however if props change rapidly this can call new render actions before previous ones have completed.
I found wrapping my viewer in a memo that only re-renders when the input source change updates was a reasonable workaround, e.g.
import{memo}from"react";importPdf,{PdfProps,Source}from"react-native-pdf";/** * Isolated PDF component that only re-renders when URI actually changes. * This prevents the native onAfterUpdateTransaction from being called during animations. * * NOTE - parent component should also only try to limit rate at which passed uri updates */exportconstIsolatedPdfView=memo((props: PdfProps)=>{return<Pdf{...props}/>;},// Only re-render if URI changes - ignore all other prop changes(prevProps,nextProps)=>(prevProps.sourceasSource).uri===(nextProps.sourceasSource).uri,);
I still had the issue re-emerge on some older devices if changing source uri rapidly, and so added some extra logic to the parent component to debounce source uri changes.
From the java implementation, I think the issue possibly comes from
/** * Callback that will be triggered after all properties are updated in current update transaction * (all @ReactProp handlers for properties updated in current transaction have been called). If * you want to override this method you should call super.onAfterUpdateTransaction from it as the * parent class of the ViewManager may rely on callback being executed. */protectedvoidonAfterUpdateTransaction(@NonNullTview) {}
So I'm not really sure how fixable it is from a react-native-pdf side (quite implementation-specific)... perhaps the documentation could provide some guidance around avoiding too many prop-based rerenders? Or maybe there is a better way to memoise prop changes within the react-native-pdf component?
Oh, now I can reproduce this bug by scrolling really fast pdfview and navigating to a new screen. then I try to use your workaround @DYP1016, but can not fix :(.
FATAL EXCEPTION: PDF renderer
Process: com.galaxyfinx.vikki.uat, PID: 15581
java.lang.IllegalStateException: Already closed
at io.legere.pdfiumandroid.PdfDocument.openPage(PdfDocument.kt:144)
at io.legere.pdfiumandroid.PdfiumCore.renderPageBitmap(PdfiumCore.kt:503)
at com.github.barteksc.pdfviewer.PdfFile.renderPageBitmap(PdfFile.java:301)
at com.github.barteksc.pdfviewer.RenderingHandler.proceed(RenderingHandler.java:109)
at com.github.barteksc.pdfviewer.RenderingHandler.handleMessage(RenderingHandler.java:66)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.os.HandlerThread.run(HandlerThread.java:85)
implementation 'io.legere:pdfiumandroid:1.0.34'
try {
Field field = PDFView.class.getDeclaredField("pdfiumCore");
field.setAccessible(true);
field.set(this, new PdfiumCore(context, new Config(new DefaultLogger(), AlreadyClosedBehavior.IGNORE)));
} catch (Exception e) {
e.printStackTrace();
}
good news, but could you try to reproduce following my case:
screen A -> navigate to pdfview, scroll really fast and go back A. I got this crash when pdfview is still scrolling and go back.
It won't be compatible with Expo because it has native functionalities. Libraries with native code operation on development build which bundle the native code.
Activity
InfoGreen-Suku commentedon Oct 13, 2025
I am Also facing the same issue
Prajwaltechversant commentedon Oct 13, 2025
I’m also facing the same crash on Android when closing a PDF after scrolling.
saif-techversant commentedon Oct 13, 2025
+1
ersyhsyn commentedon Oct 21, 2025
+1
marius-styreportalen commentedon Oct 22, 2025
From what I can see, appearantly pdfiumandroid:1.0.33 was supposed to fix this, but for me it would crash when i built the app, 1.0.34 which recently released should be good.
I can't verify 100% that it works, but you can try for yourself by either overwriting the installed version in your own app/build.gradle file, or you could patch the package yourself in the library android/build.gradle i believe.
rnproject/android/app/build.gradle:
acuD1 commentedon Oct 23, 2025
I did patch from 1.0.32, still having the issue.
sangmin4208 commentedon Oct 24, 2025
I'm not 100% sure, but it appears the issue doesn't happen when the PDF renderer is wrapped with
memoand thepageprop isn't used.DYP1016 commentedon Oct 28, 2025
+1
DYP1016 commentedon Oct 29, 2025
I found a workaround that can be solved by modifying the following source code:
At the same time, update the version of pdfiumandroid
By setting the parameter AlreadyClosedBehavior.IGNORE, you can solve the Java layer crash, and by setting the pdfiumandroid version to 1.0.34, you can solve the C++ layer crash
chrismclarke commentedon Nov 12, 2025
I found the same solution working for me too. It appears that any time props update the PDF is forced to re-render, however if props change rapidly this can call new render actions before previous ones have completed.
I found wrapping my viewer in a
memothat only re-renders when the input source change updates was a reasonable workaround, e.g.I still had the issue re-emerge on some older devices if changing source uri rapidly, and so added some extra logic to the parent component to debounce source uri changes.
From the java implementation, I think the issue possibly comes from
Given the source suggests that
So I'm not really sure how fixable it is from a react-native-pdf side (quite implementation-specific)... perhaps the documentation could provide some guidance around avoiding too many prop-based rerenders? Or maybe there is a better way to memoise prop changes within the react-native-pdf component?
DuLe-FinX commentedon Dec 8, 2025
I can not reproduce this crash in my Emulator, but I can confirm that I see this crash event in Firebase dashboard.
@DYP1016 your workaround is still work right?
DuLe-FinX commentedon Dec 8, 2025
Oh, now I can reproduce this bug by scrolling really fast pdfview and navigating to a new screen. then I try to use your workaround @DYP1016, but can not fix :(.
DuLe-FinX commentedon Dec 10, 2025
@wonday Could you help this?
Natanaelvich commentedon Dec 10, 2025
I migrated the PDF viewing implementation to https://github.com/126punith/react-native-pdf-jsi, and since then I have not encountered this crash anymore. It seems the issue is resolved with this library.
tordillos commentedon Dec 10, 2025
Not yet compatible with Expo.
DuLe-FinX commentedon Dec 10, 2025
good news, but could you try to reproduce following my case:
screen A -> navigate to pdfview, scroll really fast and go back A. I got this crash when pdfview is still scrolling and go back.
NJneeraj commentedon Dec 19, 2025
See this if it works for you
adammonteil commentedon Jan 8, 2026
+1
AsianManiac commentedon Jan 11, 2026
It won't be compatible with Expo because it has native functionalities. Libraries with native code operation on development build which bundle the native code.