Skip to content

[No QA] fix: introduce useStartSpansOnRender hook that registers spans on first render - #97885

Merged
roryabraham merged 6 commits into
Expensify:mainfrom
software-mansion-labs:war-in/fix-broken-navigation-root-ready-span
Aug 11, 2026
Merged

[No QA] fix: introduce useStartSpansOnRender hook that registers spans on first render#97885
roryabraham merged 6 commits into
Expensify:mainfrom
software-mansion-labs:war-in/fix-broken-navigation-root-ready-span

Conversation

@war-in

@war-in war-in commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

NavigationRootReady was started in a useEffect but is ended from NavigationContainer's onReady, which React flushes first — so the end was dropped and the span leaked, landing as cancelled on 36–53% of native traces (single span evidence, aggregated spans that didn't finish) . New useStartSpansOnRender hook registers spans during render instead, which also makes the span cover the navigator's render cost.

Fixed Issues

$
PROPOSAL:

Tests

  1. Run the app on iOS Native or Android Native (npm run ios / npm run android).
  2. Open the Metro/debugger console and filter logs for [Sentry][NavigationRootReady].
  3. Cold start the app (fully kill it first, then relaunch) and sign in.
  4. Verify two lines appear, in this order:
    • [Sentry][NavigationRootReady] Starting span
    • [Sentry][NavigationRootReady] Ending span (Nms)
  5. Verify that on main (before this change) only the Starting span line appears on native and the Ending span line is never logged, because the end ran before the span was registered and was silently dropped.

Offline tests

N/A

QA Steps

N/A - telemetry change

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

@war-in war-in changed the title fix: introduce useStartSpansOnRender hook that registers spans on first render [No QA] fix: introduce useStartSpansOnRender hook that registers spans on first render Aug 6, 2026
roryabraham
roryabraham previously approved these changes Aug 6, 2026

@roryabraham roryabraham left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great find 👍🏼

@MelvinBot Are there other places where we're starting spans in effects and ending them in effects of a child component?

Comment thread src/libs/telemetry/useStartSpansOnRender.ts Outdated
@war-in

war-in commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

@roryabraham answering the question Melvin missed

Are there other places where we're starting spans in effects and ending them in effects of a child component?

I think TelemetrySpanManager is a good candidate. It's designed solely for starting and finishing spans but I'd rather verify if the proposed solution fixes the issue and then prepare a follow-up PR with a fix for TelemetrySpanManager. What do you think?

Here is my bot's analysis

Answer: one candidate, not yet an active bug, fragile.
SPAN_CONFIRMATION_LIST_READY / SPAN_CONFIRMATION_RECEIPT_LOAD:
• Started in TelemetrySpanManager's mount effect (TelemetrySpanManager.tsx:25-35)
• SPAN_CONFIRMATION_LIST_READY ended in ConfirmationTelemetry's mount effect (ConfirmationTelemetry.tsx:18-23), which lives inside MoneyRequestConfirmationList — a sibling subtree, not a descendant of TelemetrySpanManager
Both mount in the same commit under IOURequestStepConfirmation.tsx, where is at line 853 and at line 964. It currently works only because React commits sibling subtrees left-to-right — start wins the race by JSX source order, not by anything that guarantees it. Reordering those two elements would silently reintroduce this exact bug. ConfirmationTelemetry's effect is keyed on [transactionID], so if transactionID is already defined on first render (common case), the end fires in the same commit as the start — this isn't a someday-later-tick end, it's the same race NavigationRootReady had, just currently winning by luck.
Everything else audited clean: zero other startSpan-in-effect / endSpan-in-descendant-effect pairs in the same commit, and zero endSpan calls inside any useLayoutEffect/useInsertionEffect anywhere in src/ (which would invert even against an ancestor's passive-effect start).

@war-in
war-in marked this pull request as ready for review August 10, 2026 10:07
@war-in
war-in requested review from a team as code owners August 10, 2026 10:07
@melvin-bot
melvin-bot Bot requested review from inimaga and trjExpensify and removed request for a team August 10, 2026 10:07
@melvin-bot

melvin-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown

@inimaga Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

Comment thread src/libs/telemetry/useStartSpansOnRender.ts Outdated
@trjExpensify

Copy link
Copy Markdown
Contributor

PR doesn’t need product input as a perf tooling PR. Unassigning and unsubscribing myself.

@trjExpensify
trjExpensify removed their request for review August 10, 2026 11:28
@inimaga

inimaga commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari

@roryabraham roryabraham reopened this Aug 11, 2026
@roryabraham
roryabraham merged commit edc4aad into Expensify:main Aug 11, 2026
44 of 46 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚧 roryabraham has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions

Copy link
Copy Markdown
Contributor

🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
Built from App PR #97885.

Android 🤖 iOS 🍎
https://ad-hoc-expensify-cash.s3.us-east-1.amazonaws.com/rock-artifacts/ad-hoc/rock-android-Adhoc-edc4aad882a-7d586de-0d7ec3a17c27bd752f5d3eae4a35cfaed16ad8c7/index.html ❌ FAILED ❌
Android The QR code can't be generated, because the iOS build failed
Web 🕸️
https://97885.pr-testing.expensify.com
Web

👀 View the workflow run that generated this build 👀

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/roryabraham in version: 9.4.53-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

🤖 No help site changes required — no draft PR created.

This PR is a [No QA] telemetry-only change. It fixes a timing bug where the NavigationRootReady Sentry span was started in a useEffect but ended from NavigationContainer's onReady (flushed first), causing the span to leak as cancelled. The fix introduces a useStartSpansOnRender hook that registers the span during render instead.

Both changed files are internal observability code with no user-facing impact:

  • src/libs/Navigation/NavigationRoot.tsx — moves span registration from an effect to render
  • src/libs/telemetry/useStartSpansOnRender.ts — new internal telemetry hook

There are no new or changed features, UI, settings, tab names, buttons, workflows, billing, or integration behavior — nothing that the help site articles under App/docs/articles document. Accordingly, no help site article needs to be added or updated.

@war-in, if you believe there is a user-facing behavior change I've missed that the help site should cover, let me know and I'll draft the docs PR.

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.

6 participants