Skip to content

Android: reloading or closing a PDF while a page is rendering crashes the app (PDF renderer thread) #1052

Open

Description

@Junveloper

What react-native version are you using?

0.81.1 (the repository's FabricExample). We hit it in production in an Expo app.

What react-native-pdf version are you using?

7.0.5. Also reproduced on master at d4c19c7.

What platform does your issue occur on? (android/ios/both)

Android.

Describe your issue as precisely as possible

On Android, PDFView.recycle() closes the pdfium document on the UI thread while the "PDF renderer" thread may still be drawing a page from it. RenderingHandler only catches PageRenderingException, so the renderer thread throws and the app crashes.

recycle() runs on every reload, and react-native-pdf reloads a lot: every prop update posts drawPdf() → Configurator.load() → recycle(). It also runs when the view detaches and after a load error.

This is what we see in production. xr9 is AndroidPdfViewer's RenderingHandler, obfuscated by R8, which also inlined PdfFile.renderPageBitmap into proceed:

java.lang.RuntimeException: Get page pdf document null
	at io.legere.pdfiumandroid.PdfDocument.nativeLoadPage(Native Method)
	at io.legere.pdfiumandroid.PdfDocument.openPage
	at io.legere.pdfiumandroid.PdfiumCore.renderPageBitmap
	at xr9.b
	at xr9.handleMessage
	at android.os.Handler.dispatchMessage(Handler.java:125)
	at android.os.Looper.loopOnce(Looper.java:321)
	at android.os.Looper.loop(Looper.java:422)
	at android.os.HandlerThread.run(HandlerThread.java:139)

The steps below reproduce the same race in the example app. There it surfaces as one of two other exceptions, on the same frames:

FATAL EXCEPTION: PDF renderer
java.lang.IllegalStateException: Already closed
	at io.legere.pdfiumandroid.PdfDocument.openPage(PdfDocument.kt:143)
	at io.legere.pdfiumandroid.PdfiumCore.renderPageBitmap(PdfiumCore.kt:505)
	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:110)
	at android.os.Looper.loopOnce(Looper.java:248)
	at android.os.Looper.loop(Looper.java:338)
	at android.os.HandlerThread.run(HandlerThread.java:85)
FATAL EXCEPTION: PDF renderer
java.lang.NullPointerException: Parameter specified as non-null is null: method io.legere.pdfiumandroid.PdfiumCore.renderPageBitmap, parameter pdfDocument
	at io.legere.pdfiumandroid.PdfiumCore.renderPageBitmap(Unknown Source:5)
	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)
	...

Which one you get depends on where the render task is when recycle() lands. recycle() calls removeMessages(MSG_RENDER_TASK), which drops queued tasks but not the one already running, and then pdfFile.dispose():

  • dispose() has already nulled PdfFile.pdfDocument, so the task passes null to PdfiumCore.renderPageBitmap: the NullPointerException.
  • The task holds the document, but close() finished before PdfDocument.openPage checks isClosed: Already closed.
  • The task passed that check and then waited on PdfiumCore.lock while close() held it: nativeLoadPage gets a closed document, Get page pdf document null. That's the narrowest window. We see it in production, but didn't hit it in the example app.

AlreadyClosedBehavior.IGNORE (#999) doesn't cover any of these. They all reproduce on 7.0.5.

This looks like the same crash as #830, #847, #882, #976, #987, #989, #1024 and #1041. #1045 moves dispose() to the renderer thread, but review there found it clears pdfFile first, which opens a NullPointerException in RenderingHandler.proceed. With the steps below, #1045 crashed that way in 2 of 3 runs. I'll open a PR with the approach proposed in that review: wait for the renderer to go idle before recycle().

Steps to reproduce

The repro branch adds a Start reload loop button to FabricExample. It flips the spacing prop every 120 ms, and each flip runs drawPdf() → Configurator.load() → recycle().

  1. Check out the repro branch below.
  2. cd FabricExample && yarn install && yarn start. npm can't install "react-native-pdf": "link:../". The branch also makes metro.config.js accept exclusionList as a default export, which current metro-config uses; without that, yarn start fails.
  3. In another terminal: cd FabricExample/android && ./gradlew :app:installDebug -PreactNativeArchitectures=arm64-v8a, then open the app.
  4. Wait for the PDF to show, then tap Start reload loop.
  5. The app crashes on the "PDF renderer" thread, usually within a minute. It crashed in 4 of 4 runs, after 102, 151, 160 and 324 loads.

Environment

System:
  OS: macOS 27.0.1
  CPU: (11) arm64 Apple M3 Pro
Binaries:
  Node: 24.3.0
SDKs:
  Android SDK:
    API Levels: 35, 36, 37
Languages:
  Java: 17.0.14
npmPackages:
  react: 19.1.0
  react-native: 0.81.1
Android:
  hermesEnabled: true
  newArchEnabled: true

AndroidPdfViewer 4.0.1, pdfiumandroid 1.0.32. Device: Android emulator, API 36 (arm64).

Reproducible example repository

https://github.com/Junveloper/react-native-pdf/tree/repro/android-recycle-race (this repository with the reload loop added to FabricExample)

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions