Skip to content

[Due for payment 2026-06-09] [Due for payment 2026-06-08] [$250] Android - App crashes reopening while the GPS trip is in progress after discard tracking twice #88700

Closed

Description

@jponikarchuk

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Version Number: v9.3.62-1
Reproducible in staging?: Yes
Reproducible in production?: Yes
If this was caught during regression testing, add the test name, ID and link from BrowserStack: https://test-management.browserstack.com/projects/2219752/folder/39523254/test-cases/86239624
Email or phone of affected tester (no customers): N/A
Issue reported by: Applause Internal Team
Bug source: Regression TC Execution
Device used: Android 13 / Xiaomi Poco F5
App Component: Money Requests

Action Performed:

  1. Login to the ND app
  2. Navigate to WS Chat
  3. Click + > Track Distance > GPS
  4. Start the GPS tracking, kill and reopen the app
  5. Stop the GPS tracking, then discard the distance tracking
  6. Repeat steps 4-5
  7. Start the GPS tracking
  8. While the trip is in progress kill the app or force close
  9. Reopen the app

Expected Result:

App should not crash when reopening while the GPS trip is in progress

Actual Result:

App crashes reopening while the GPS trip is in progress after discard the distance tracking twice
Note:

  • The app will crash several time on the next reopening, then it can be launched normally again
  • Not reproducible on iOS

Workaround:

Unknown

Platforms:

  • Android: App
  • Android: mWeb Chrome
  • iOS: App
  • iOS: mWeb Safari
  • iOS: mWeb Chrome
  • Windows: Chrome
  • MacOS: Chrome / Safari

Screenshots/Videos

Video.mp4

logs.txt

View all open jobs on GitHub

Issue OwnerCurrent Issue Owner: @truph01
Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~022047732395455734421
  • Upwork Job ID: 2047732395455734421
  • Last Price Increase: 2026-06-15

Activity

  1. added
    BugSomething is broken. Auto assigns a BugZero manager.
    on Apr 24, 2026
  2. added
    BugSomething is broken. Auto assigns a BugZero manager.
    and removed
    BugSomething is broken. Auto assigns a BugZero manager.
    on Apr 24, 2026
  3. MelvinBot commented on Apr 24, 2026

    @MelvinBot
    Contributor

    Proposal

    What is the root cause of that problem?

    When the app is killed during GPS tracking, GpsTripService.onTaskRemoved() cancels the Android notification but does not end the Airship LiveUpdate. The LiveUpdate persists in Airship's internal storage across app kills.

    On app restart, Airship SDK re-initializes and restores the pending LiveUpdate, which calls GpsLiveUpdateHandler.onUpdate(). For non-END events, this calls context.startService(GpsTripService) at line 32 without a try-catch. On Android 12+ (API 31+), if this fires before the Activity is fully in the foreground during early Airship initialization, it throws an uncaught IllegalStateException / ForegroundServiceStartNotAllowedException, crashing the app.

    This is worsened by a race condition in GPSTripStateChecker where checkAndCleanGpsNotification() is called twice — once inside handleGpsTripInProgressOnAppRestart() (async) and once immediately after. Both race to listAll() and end() the same LiveUpdate concurrently, which can corrupt Airship's internal state. After multiple start/kill/discard cycles, the accumulated stale LiveUpdate state makes the crash reliably reproducible.

    The crash eventually self-resolves because each crash-restart cycle gives checkAndCleanGpsNotification another opportunity to clean up the stale LiveUpdate before the handler fires.

    Android-only because iOS Live Activities are managed by the OS and cleaned up on app termination, while Android's Airship LiveUpdate persists across kills.

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

    1. End the Airship LiveUpdate in GpsTripService.onTaskRemoved() — call Airship.android.liveUpdateManager.end() (or equivalent native API) alongside the notification cancellation so stale LiveUpdates don't persist after app kill.

    2. Wrap context.startService() in a try-catch in GpsLiveUpdateHandler.onUpdate() to prevent the crash from IllegalStateException on Android 12+ when the app isn't fully foregrounded.

    3. Remove the duplicate checkAndCleanGpsNotification() call on line 46 of GPSTripStateChecker — it's already called inside handleGpsTripInProgressOnAppRestart() on line 35, and the duplicate creates the race condition.

    Supporting evidence
    • GpsTripService.onTaskRemoved() only calls cancelGpsNotifications() (dismisses the Android notification) and stopSelf() — no LiveUpdate cleanup
    • GpsLiveUpdateHandler.onUpdate() unconditionally calls context.startService() for all non-END events with no exception handling
    • checkAndCleanGpsNotification() in index.android.ts calls stopGpsTripNotification() → end() for each matching LiveUpdate, but if called concurrently (as the race condition causes), both instances may try to end the same update
    • Related issue [$250] Distance - App keep crashing after start GPS and switch to OD #80593 was a different crash (OD switch during GPS tracking), fixed by PR Warn about ongoing GPS trip when switching to OD #80748 — different root cause, different code path

    Next Steps for Contributor+ team: Reply with @MelvinBot implement this to create a draft PR, @MelvinBot <your feedback> to refine this analysis, or explain why you are rejecting Melvin's proposal.

  4. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Apr 24, 2026
  5. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Apr 24, 2026
  6. melvin-bot commented on Apr 24, 2026

    @melvin-bot

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

  7. changed the title [-]Android - App crashes reopening while the GPS trip is in progress after discard tracking twice[/-] [+][$250] Android - App crashes reopening while the GPS trip is in progress after discard tracking twice[/+] on Apr 24, 2026
  8. melvin-bot commented on Apr 24, 2026

    @melvin-bot
  9. twilight2294 commented on Apr 24, 2026

    @twilight2294
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    On Android, the app crashes when reopened while a GPS trip was in progress and the user had discarded tracking twice.

    What is the root cause of that problem?

    The background location task in backgroundLocationTrackingTask calls addGpsPoints(gpsDraftDetails, newGpsPoints) without checking if gpsDraftDetails is null. On Android, the headless JS background task can fire after stopLocationUpdatesAsync due to timing races — especially after discarding twice quickly. When it fires post-discard, gpsDraftDetails is null, so addGpsPoints() in GPSDraftDetails writes a partial object to Onyx containing only gpsPoints and distanceInMeters — missing startAddress, endAddress, and other fields.

    On app reopen, useUpdateGpsTripOnReconnect destructures startAddress from this corrupt partial draft and accesses startAddress.type — a TypeError on undefined, crashing the app. A similar unsafe access exists in GPSDraftDetailsUtils with gpsDraftDetails?.startAddress.value. Android-only because expo-task-manager background tasks run in a separate headless JS context that survives app kill, unlike iOS.

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

    1. In backgroundLocationTrackingTask, add an early return if gpsDraftDetails is null or isTracking is falsy — this prevents corrupt partial data from ever being written:
    if (!gpsDraftDetails?.isTracking) {
        return;
    }
    1. In useUpdateGpsTripOnReconnect, add optional chaining as defense-in-depth: startAddress?.type and endAddress?.type.

    2. In GPSDraftDetailsUtils, fix the unsafe access: gpsDraftDetails?.startAddress?.value and gpsDraftDetails?.endAddress?.value.

    What alternative solutions did you explore? (Optional)

    MelvinBot's proposal targets the native Android Airship LiveUpdate layer (Kotlin GpsTripService/GpsLiveUpdateHandler) — wrapping startService() in try-catch and ending LiveUpdates in onTaskRemoved(). That addresses a different crash vector (native IllegalStateException from foreground service restrictions). This proposal targets the actual JS-layer TypeError from corrupt Onyx data, which is the crash path triggered by the specific "discard tracking twice" repro. The two are complementary but mine addresses the data corruption that causes the reproducible crash described in the issue. MelvinBot's duplicate checkAndCleanGpsNotification fix is unrelated to this crash path.

  10. hanif-2000 commented on Apr 24, 2026

    @hanif-2000

    Proposal

    Problem

    The app crashes on Android when reopening during an active GPS trip after discarding tracking twice.

    Root Cause

    The issue appears after multiple discard cycles, indicating a state desynchronization between persisted tracking data (Onyx) and the Android location tracking service. After repeated discard actions, the trip data is partially cleared while the background service or related metadata is not fully reset. On relaunch, the app attempts to resume an invalid session, leading to a crash.

    Solution

    • Make discard handling idempotent to prevent duplicate state mutations
    • Ensure complete cleanup of all tracking-related data (distance request, waypoints, tracking state)
    • Add validation during app initialization to prevent resuming invalid or corrupted trips
    • Stop the Android foreground service when no valid trip exists
    • Add defensive guards before resuming tracking

    Testing

    • Start trip → discard once → reopen app
    • Start trip → discard twice → reopen app (main case)
    • Kill app during active trip → reopen
    • Background → foreground transitions on Android
    • Verify no regression in normal trip flow

    I’m happy to implement this fix once the proposal is approved.

  11. melvin-bot commented on Apr 24, 2026

    @melvin-bot

    📣 @hanif-2000! 📣
    Hey, it seems we don’t have your contributor details yet! You'll only have to do this once, and this is how we'll hire you on Upwork.
    Please follow these steps:

    1. Make sure you've read and understood the contributing guidelines.
    2. Get the email address used to login to your Expensify account. If you don't already have an Expensify account, create one here. If you have multiple accounts (e.g. one for testing), please use your main account email.
    3. Get the link to your Upwork profile. It's necessary because we only pay via Upwork. You can access it by logging in, and then clicking on your name. It'll look like this. If you don't already have an account, sign up for one here.
    4. Copy the format below and paste it in a comment on this issue. Replace the placeholder text with your actual details.
      Screen Shot 2022-11-16 at 4 42 54 PM
      Format:
    Contributor details
    Your Expensify account email: <REPLACE EMAIL HERE>
    Upwork Profile Link: <REPLACE LINK HERE>
    
  12. 74 remaining items

  13. melvin-bot commented on Jun 8, 2026

    @melvin-bot

    Payment Summary

    Upwork Job

    • Reviewer: @truph01 owed $250 via NewDot

    BugZero Checklist (@mallenexpensify)

    • I have verified the correct assignees and roles are listed above and updated the necessary manual offers
    • I have verified that there are no duplicate or incorrect contracts on Upwork for this job (https://www.upwork.com/ab/applicants/2047732395455734421/hired)
    • I have verified the PR was not reverted
    • I have applied any discounts due to bugs/regressions introduced by this PR
    • I have paid out the Upwork contracts or cancelled the ones that are incorrect
    • I have verified the payment summary above is correct
  14. mallenexpensify commented on Jun 8, 2026

    @mallenexpensify
    Contributor

    @truph01 👀 plz. Are you the only one due payment? If so, how much? I see a regression it looks like it was expected. Thx

  15. truph01 commented on Jun 9, 2026

    @truph01
    Contributor

    @truph01 👀 plz. Are you the only one due payment? If so, how much? I see a regression it looks like it was expected. Thx

    @mallenexpensify Yes, I should be eligible for the $250 payment.

    This issue needs two PRs. The regression occurred because the two PRs were not merged and deployed together.

  16. mallenexpensify commented on Jun 9, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @truph01 due $250 via NewDot

    @truph01 do we need a checklist here? If no, why not? Thx

  17. melvin-bot commented on Jun 12, 2026

    @melvin-bot

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

  18. melvin-bot commented on Jun 15, 2026

    @melvin-bot

    📣 It's been a week! Do we have any satisfactory proposals yet? Do we need to adjust the bounty for this issue? 💸

  19. mallenexpensify commented on Jun 15, 2026

    @mallenexpensify
    Contributor

    @truph01 do we need a checklist here? If no, why not? Thx

  20. truph01 commented on Jun 16, 2026

    @truph01
    Contributor

    BugZero Checklist:

    • [Contributor] The offending PR and associated issue have been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake.

      Link to the comment on the PR:
      Link to the comment on the Issue:

    • [Contributor] If the regression was CRITICAL (e.g. interrupts a core flow) A discussion in #expensify-open-source has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner.

      Link to discussion:

    • [Contributor] If it was decided to create a regression test for the bug, please propose the regression test steps using the template below to ensure the same bug will not reach production again.

    • [BugZero Assignee] Create a GH issue for creating/updating the regression test once above steps have been agreed upon.

      Link to issue:

    Regression Test Proposal

    Precondition:

    Test:

    Crashes # 1:

    1. Start a GPS live tracking session
    2. Go back to Home
    3. Background the app
    4. Leave the phone for 1 - 2 minutes (Can lock the phone)
    5. Open the phone
    6. Tap on the Live tracking notification
    7. Repeat 4-7 for 2 - 3 times
    8. Verify the app doesn't crash

    Crashes # 2:

    1. Navigate to WS Chat
    2. Click + > Track Distance > GPS
    3. Start the GPS tracking, kill and reopen the app
    4. Stop the GPS tracking, then discard the distance tracking
    5. Repeat steps 4-5
    6. Start the GPS tracking
    7. While the trip is in progress kill the app or force close
    8. Reopen the app
    9. Verify that the app doesn't crash

    Starting GPS trip and backgrounding the app:

    1. Start GPS trip
    2. Verify that it shows a notification banner at the top of the screen
    3. Verify that there is a notification on the lock screen and in the notifications panel
    4. Navigate to a different screen and background the app
    5. Tap on the GPS notification
    6. Verify that it opens the app and navigates to the GPS screen with ongoing GPS trip
    7. Background the app again
    8. Tap on the GPS notification
    9. Verify that it opens the app on the GPS screen (there should be no unnecessary navigation animation or anything like that)

    Logging out:

    1. Start GPS trip
    2. Verify that the GPS notification appears
    3. Log out
    4. Verify that the GPS notification disappeared

    App termination:

    1. Start GPS trip
    2. Verify that the GPS notification appears
    3. Terminate the app
    4. Verify that the GPS notification disappeared
    5. Open the app again, it should show the Continue GPS trip recording? modal
    6. Tap Continue trip
    7. Verify that the GPS notification appears

    Do we agree 👍 or 👎

    Zapier Logs Run ID: 00040eee-5a9f-ab94-8f4a-5be5704eb424
  21. garrettmknight commented on Aug 10, 2026

    @garrettmknight
    Contributor

    $250 approved for @truph01

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

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributorHelp WantedApply this label when an issue is open to proposals by contributors

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions