Skip to content

Crash on Android When Closing PDF Page After Scrolling || java.lang.IllegalStateException: Already closed #976

Open

Description

@maneesh-techversant

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:

  1. Open a PDF with the react-native-pdf component.
  2. Scroll or slide through multiple pages.
  3. Close the PDF viewing page (e.g., by navigating away or closing the screen).
  4. 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.

Activity

  1. InfoGreen-Suku commented on Oct 13, 2025

    @InfoGreen-Suku

    I am Also facing the same issue

  2. Prajwaltechversant commented on Oct 13, 2025

    @Prajwaltechversant

    I’m also facing the same crash on Android when closing a PDF after scrolling.

  3. saif-techversant commented on Oct 13, 2025

    @saif-techversant

    +1

  4. ersyhsyn commented on Oct 21, 2025

    @ersyhsyn

    +1

  5. marius-styreportalen commented on Oct 22, 2025

    @marius-styreportalen

    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")  
    }
  6. acuD1 commented on Oct 23, 2025

    @acuD1

    I did patch from 1.0.32, still having the issue.

    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'
     }
  7. sangmin4208 commented on Oct 24, 2025

    @sangmin4208

    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.

  8. DYP1016 commented on Oct 28, 2025

    @DYP1016

    +1

  9. DYP1016 commented on Oct 29, 2025

    @DYP1016

    I found a workaround that can be solved by modifying the following source code:

    public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompleteListener,OnErrorListener,OnTapListener,OnDrawListener,OnPageScrollListener, LinkHandler {
       public PdfView(Context context, AttributeSet set){
            super(context, set);
    
           //add
            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();
            }
        }
    }

    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

  10. chrismclarke commented on Nov 12, 2025

    @chrismclarke

    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";
    import Pdf, { 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
     */
    export const IsolatedPdfView = memo(
      (props: PdfProps) => {
        return <Pdf {...props} />;
      },
      // Only re-render if URI changes - ignore all other prop changes
      (prevProps, nextProps) => (prevProps.source as Source).uri === (nextProps.source as Source).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

    @Override
        public void onAfterUpdateTransaction(PdfView pdfView) {
            super.onAfterUpdateTransaction(pdfView);
            pdfView.drawPdf();
        }

    Given the source suggests that

    /**
       * 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.
       */
      protected void onAfterUpdateTransaction(@NonNull T view) {}

    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?

  11. DuLe-FinX commented on Dec 8, 2025

    @DuLe-FinX

    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?

  12. DuLe-FinX commented on Dec 8, 2025

    @DuLe-FinX

    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();
    }
    
  13. DuLe-FinX commented on Dec 10, 2025

    @DuLe-FinX

    @wonday Could you help this?

  14. Natanaelvich commented on Dec 10, 2025

    @Natanaelvich

    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.

  15. tordillos commented on Dec 10, 2025

    @tordillos

    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.

    Not yet compatible with Expo.

  16. DuLe-FinX commented on Dec 10, 2025

    @DuLe-FinX

    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.

    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.

  17. NJneeraj commented on Dec 19, 2025

    @NJneeraj
  18. adammonteil commented on Jan 8, 2026

    @adammonteil

    +1

  19. AsianManiac commented on Jan 11, 2026

    @AsianManiac

    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.

    Not yet compatible with Expo.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions