Skip to content

[Sentry: APP-A2K] HybridApp iOS Fatal App Hang in bcmap_addDelta during PDF rendering #97099

Closed

Description

@mountiny

Sentry

https://expensify.sentry.io/issues/APP-A2K
https://expensify.sentry.io/issues/APP-85A

Impact (snapshot at filing)

  • Users (total since first seen): 360 (APP-A2K), 155 (APP-85A)
  • Events: 496 (APP-A2K), 193 (APP-85A)
  • Users (last 7d): 346 (APP-A2K), 79 (APP-85A)
  • First seen: 2026-03-22 (APP-85A), 2026-03-29 (APP-A2K)
  • Last seen: ongoing (2026-07-27)
  • Platform: iOS HybridApp
  • App version(s): 9.4.42-1, 9.4.43-1, 9.4.44-x (production)
  • Affected route(s): unknown (native hang during PDF/receipt rendering)
  • Mechanism: AppHang (fatal main-thread block ≥2000ms)

Stack trace (top frames)

bcmap_addDelta
FSTerminate (repeated)
-[RCTMountingManager synchronouslyDispatchCommandOnUIThread:commandName:args:]
_dispatch_main_queue_drain
UIApplicationMain

Suspected cause

PDF CMap/font decoding (bcmap_addDelta) is blocking the main thread during receipt or PDF display on HybridApp iOS. APP-A2K and APP-85A are siblings with the same hang site; Sentry splits fully-blocked vs non-fully-blocked variants.

Reproduction

Unknown — see Sentry events linked above. Likely triggered when opening or rendering a PDF receipt attachment.

Related

  • Sibling Sentry IDs: APP-85A
  • Prior GH issues: none
Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @brunovjk

Activity

  1. self-assigned this
    on Jul 27, 2026
  2. added
    ExternalAdded to denote the issue can be worked on by a contributor
    BugSomething is broken. Auto assigns a BugZero manager.
    on Jul 27, 2026
  3. melvin-bot commented on Jul 27, 2026

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @brunovjk (External)

  4. melvin-bot commented on Jul 27, 2026

    @melvin-bot
  5. MelvinBot commented on Jul 27, 2026

    @MelvinBot
    Contributor

    Proposal

    What is the root cause of that problem?

    The hang is Apple PDFKit decoding an embedded PDF font/CMap synchronously on the main thread, not App JS code.

    On iOS every PDF (full‑screen viewer and thumbnails) renders through react-native-pdf@7.0.2, which uses usePDFKit: true → the native Fabric view RNPDFPdfView (Apple PDFDocument/PDFView). When the path prop is applied, Fabric flushes it on the main queue (RCTMountingManager synchronouslyDispatchCommandOnUIThread → _dispatch_main_queue_drain), and RNPDFPdfView.didSetProps: synchronously does [[PDFDocument alloc] initWithURL:] → _pdfView.document = … → layoutDocumentView. PDFKit then parses the doc and decodes its fonts/CMaps on that same thread — the bcmap_addDelta / FSTerminate frames. bcmap_addDelta is CMap/CID decode, so the hang concentrates on receipts with non‑Latin / CID (e.g. CJK) or heavily‑subset embedded fonts.

    The App‑controllable amplifier: PDFThumbnail/index.native.tsx spins up a full PDFKit instance per thumbnail (not a lightweight raster), and thumbnails are mounted in lists/carousels (e.g. ReportActionItemImage.tsx, AttachmentPreview, ReceiptImage), so several synchronous font decodes can stack on the main thread at once.

    Investigation details (high confidence — code path traced end to end)

    Render path on iOS: AttachmentViewPdf/index.ios.tsx → BaseAttachmentViewPdf → PDFView/index.native.tsx:189-206 → react-native-pdf <PDF>. pdfjs-dist/react-fast-pdf is web‑only (PDFView/index.tsx), never hit on native.

    • usePDFKit defaults to true (node_modules/react-native-pdf/index.js:94) → iOS always uses PDFKit RNPDFPdfView.
    • Synchronous decode site: node_modules/react-native-pdf/ios/RNPDFPdf/RNPDFPdfView.mm didSetProps: (PDFDocument initWithURL, _pdfView.document =, layoutDocumentView). This is upstream/Apple code — no App permalink.
    • The App never calls the one imperative command (setNativePage) and never sets page, so the trigger is the initial path prop apply (document open + font decode), not paging.
    • The 4 existing App/patches/react-native-pdf/*.patch are all Android‑only; nothing touches the iOS main‑thread decode.

    Alternative explanations considered & ruled out: (a) a specific App synchronous command — ruled out, App issues no PDF commands; (b) the JS pdfjs path — ruled out, web‑only; (c) network on main thread — ruled out, JS downloads/caches the file before setting path. The remaining variable that fits bcmap_addDelta is embedded font/CMap decode inside PDFKit.

    What changes do you think we should make in order to solve the problem?

    The actual bcmap_addDelta font decode is inside Apple PDFKit and cannot be run in App JS — so this is mitigation, not elimination. In rough order of impact:

    1. Cap/virtualize concurrent PDFThumbnail rendering. Only mount a PDFKit instance for on‑screen thumbnails and/or a small concurrency limit, using the component's existing enabled/visibility props, so multiple decodes don't stack on the main thread in receipt/attachment lists. This is the biggest App‑side lever.
    2. Defer/lazy‑mount the <PDF> viewer until the item is actually focused/visible.
    3. Add an iOS patch (or upgrade react-native-pdf past 7.0.2) that opens PDFDocument on a background dispatch queue and assigns _pdfView.document back on main. This is the only way to move the decode off the main thread; it requires a native patch since the current 4 patches are Android‑only.

    What alternative solutions did you explore? (Optional)

    Rendering thumbnails as pre‑generated raster images (server‑side or a one‑time off‑main decode) instead of a live PDFKit instance per thumbnail would remove repeated font decode entirely in list contexts.

    Recommend pulling Sentry data before committing to a fix (via the sentry skill): group APP‑A2K / APP‑85A by screen and device to confirm the concurrent‑thumbnail amplification, and sample the affected receipts to confirm the non‑Latin/CID‑font hypothesis. That decides whether (1) alone is enough or whether the native patch (3) is required.


    Next Steps for Contributor+ team:
    To accept: @MelvinBot implement this to create a draft PR.
    To refine: @MelvinBot <your feedback>
    To reject: Explain why you are rejecting Melvin's proposal.

  6. yusufdeveloper2903 commented on Jul 27, 2026

    @yusufdeveloper2903
    Contributor

    Proposal

    What is the root cause of that problem?

    The crash is a main-thread AppHang (≥2000ms watchdog), and the reliable frame in the trace is -[RCTMountingManager synchronouslyDispatchCommandOnUIThread:...] → PDFKit → bcmap_addDelta. bcmap_addDelta is CoreGraphics decoding an embedded CID/CMap font program while it rasterizes a PDF page. That decode is running on the main thread.

    On iOS, react-native-pdf renders every PDF — including receipt previews — into a live, on-screen PDFView (PDFKit). When the path prop is committed on the UI thread, its native RNPDFPdfView.didSetProps assigns the document to that live view (_pdfView.document = _pdfDocument) and forces layout (layoutDocumentView + setNeedsDisplay). PDFKit then draws the visible page on the main thread, and for a receipt whose text uses an embedded CID-keyed font it must build that font's CMap (bcmap_addDelta) before it can lay out a single glyph. For a font-heavy receipt this one draw pass exceeds 2000ms and the OS kills the app. PDFKit exposes no API to draw a live PDFView off the main thread, so react-native-pdf's iOS path has no way to avoid blocking the UI thread here.

    Two things make this concrete and iOS-specific:

    • Sibling contrast (why Android stays silent): react-native-pdf's Android view decodes and rasterizes pages on a background DecodingAsyncTask thread (the same worker the existing Android patches in patches/react-native-pdf/ interact with), so the identical PDF never blocks the Android UI thread. Only the iOS live-PDFView path rasterizes fonts on the main thread — matching "Platform: iOS HybridApp" in the report.

    • The codebase already treats a live PDF view as too expensive to mount freely. The attachment carousel mounts the full viewer for only the focused item and renders a placeholder for the rest:

    if (!hasPDFFailedToLoad && !isUploading && (isSourcePDF || isFilePDF)) {
    // Every mounted PDF viewer is a full PDF.js document parse (its own worker + parsed document), so in a
    // carousel the memory cost scales with the number of PDF attachments — enough to OOM the WebContent
    // process on iOS Safari and reload the tab when several PDFs are added at once. Only mount the viewer
    // for the item the carousel currently focuses; off-screen items render a lightweight placeholder until
    // they're swiped to. isFocused is undefined outside the carousel (single-attachment hosts), which must
    // keep mounting immediately.
    if (isFocused === false) {
    return (
    <DefaultAttachmentView
    fileName={file?.name}
    shouldShowLoadingSpinnerIcon
    containerStyles={containerStyles}
    />
    );
    }

    The receipt thumbnail surface has no equivalent gate. Local PDF receipts render a live <Pdf singlePage> — PDFThumbnail/index.native.tsx — chosen only for local files (ReportActionItemImage.tsx:202; remote receipts use a server-generated JPG and never touch native PDFKit). These thumbnails are userInteractionEnabled = NO, kPDFDisplaySinglePage static previews, yet each one still runs a full main-thread PDFKit rasterization, and several can mount at once from ReportActionItemImages.tsx:84.

    So the root cause is: the non-interactive receipt-thumbnail surface rasterizes embedded CID/CMap fonts through a live PDFKit PDFView on the main thread, and PDFKit cannot do that off-main — exactly the work the Android sibling does on a background thread.

    What changes do you think we should make in order to solve the problem?

    For the thumbnail surface — where the PDF is static, non-interactive, and displayed at a fixed small size — we should stop rendering a live PDFView and instead rasterize page 1 to a UIImage on a background queue, then display that image, moving the bcmap font decode off the main thread. PDFKit supports this directly: -[PDFPage thumbnailOfSize:forBox:] operates on the PDFPage/PDFDocument model (not a UIView) and is safe to call off the main thread; only the final image assignment hops back to main.

    The convergence point is react-native-pdf's ios/RNPDFPdf/RNPDFPdfView.mm, which the app already patches via patches/react-native-pdf/. In the singlePage branch of didSetProps (currently just _pdfView.displayMode = kPDFDisplaySinglePage; _pdfView.userInteractionEnabled = NO;), render the page off-main instead of driving the live view:

    // _singlePage == static, non-interactive preview: render off the main thread.
    PDFPage *page = [_pdfDocument pageAtIndex:0];
    CGSize size = self.bounds.size;
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        UIImage *img = [page thumbnailOfSize:size forBox:kPDFDisplayBoxCropBox]; // bcmap decode happens HERE, off-main
        dispatch_async(dispatch_get_main_queue(), ^{
            self->_thumbnailView.image = img;            // only the assignment is on main
            [self notifyOnChangeWithMessage:@"loadComplete|1"]; // preserve the onLoadComplete contract
        });
    });

    The interactive multi-page viewer (!_singlePage, PDFView/index.native.tsx) keeps live PDFView rendering so scroll/zoom/link-tap still work, and that path is already bounded to one instance by the carousel focus gate above.

    This is safe because:

    • singlePage thumbnails already disable interaction (userInteractionEnabled = NO) and use kPDFDisplaySinglePage, so a rasterized UIImage is visually equivalent — no gesture, zoom, or link behavior is lost on the thumbnail surface.
    • -[PDFPage thumbnailOfSize:forBox:] reads the model object; the only UIKit touch (UIImageView.image =) is dispatched back to the main queue, so it respects UIKit's main-thread requirement.
    • The interactive !_singlePage path is untouched, so the full-screen viewer's scroll/zoom/onPressLink (and the existing Android onPressLink/already-closed patches) are unaffected.
    • The loadComplete/error messages are still emitted, so PDFThumbnail's onLoadSuccess/onLoadError — and the sequential mount gate in useFilesValidation that depends on them — keep working unchanged.

    Note: the full-screen viewer still draws on the main thread, but it is a deliberate single-instance action already gated to the focused carousel item, whereas the thumbnail is the passive, high-frequency, multi-instance surface (it re-renders as users scroll chats/reports containing local PDF receipts). Moving the thumbnail off-main removes the widespread, compounding source of the hang; deferring the viewer's mount out of the modal-open transition can follow as a smaller change if the viewer path shows up in the Sentry route breakdown.

    What alternative solutions did you explore? (Optional)

    • Offload only the PDFDocument construction (initWithURL:/initWithData:) to a background queue. Insufficient: the watchdog fires on the draw tick, not on document parsing. bcmap_addDelta is font rasterization that PDFKit performs when the live PDFView draws (scheduled via setNeedsDisplay), so moving construction off-main leaves the blocking draw on the main thread.
    • Set enableAntialiasing={false} on the thumbnails. Only trims per-glyph cost and regresses text sharpness; the CID/CMap decode still runs on the main thread, so a font-heavy receipt can still trip the 2000ms watchdog.
    • Defer the thumbnail mount with InteractionManager.runAfterInteractions. This only reschedules the same main-thread draw to a later tick; a single ≥2000ms font decode still trips the watchdog wherever it runs, and multiple thumbnails flipped after interactions still commit and draw together.
  7. war-in commented on Jul 27, 2026

    @war-in
    Contributor

    I'm debugging the issue and trying to reproduce it - no luck with repro steps for now.
    My main suspect is FullStory that is present in all those issues

    When app is under heavy load (e.g. rendering markdown in composer) it also have to perform some FS-related work (e.g. set_fsClass) on the main thread.

    It looks like all issues started happening after RN upgrade in march (0.81 -> 0.83) but intensified a few days ago after upgrading FullStory (PR).

    I'll keep you posted about my findings and potential fixes

  8. brunovjk commented on Jul 27, 2026

    @brunovjk
    Contributor

    Thanks @war-in! Please let me know if I can help with anything.

  9. war-in commented on Jul 28, 2026

    @war-in
    Contributor

    The revert PR was merged and CPed to staging, so I think we should wait a bit and monitor the Sentry issue. Once we confirm it's fixed, we can investigate further and try to find the real cause in FS

  10. 3 remaining items

  11. mountiny commented on Jul 31, 2026

    @mountiny
    ContributorAuthor

    Monitoring

  12. melvin-bot commented on Aug 3, 2026

    @melvin-bot

    @brunovjk Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

  13. brunovjk commented on Aug 4, 2026

    @brunovjk
    Contributor

    Not overdue

  14. mountiny commented on Aug 6, 2026

    @mountiny
    ContributorAuthor

    I dont see this for the version above the revert

    Image

    so I would say the revert actually fixed it

  15. melvin-bot commented on Aug 7, 2026

    @melvin-bot

    @brunovjk Whoops! This issue is 2 days overdue. Let's get this updated quick!

  16. mountiny commented on Aug 9, 2026

    @mountiny
    ContributorAuthor

    We can close this now, the original author is aware and looking into preventing this crash

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

Metadata

Metadata

Labels

BugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions