Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/components/GPSTripStateChecker/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import ROUTES from '@src/ROUTES';
import {useSplashScreenState} from '@src/SplashScreenStateContext';

import {hasStartedLocationUpdatesAsync, startLocationUpdatesAsync, stopLocationUpdatesAsync} from 'expo-location';
import React, {useEffect, useState} from 'react';
import OnyxUtils from 'react-native-onyx/dist/OnyxUtils';
import React, {useEffect, useRef, useState} from 'react';

import useUpdateGpsNotification from './useUpdateGpsNotification';
import useUpdateGpsTripOnReconnect from './useUpdateGpsTripOnReconnect';

function GPSTripStateChecker() {
const {translate} = useLocalize();
const [showContinueTripModal, setShowContinueTripModal] = useState(false);
const [gpsDraftDetails] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS);
const [gpsDraftDetails, gpsDraftDetailsMetadata] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS);
const hasHandledAppRestart = useRef(false);
const {isOffline} = useNetwork();

const {splashScreenState} = useSplashScreenState();
Expand All @@ -37,11 +37,17 @@ function GPSTripStateChecker() {
useUpdateGpsNotification();

useEffect(() => {
// Wait for the GPS_DRAFT_DETAILS subscription to hydrate before running the restart check once, so we don't
// misread the not-yet-loaded state as "no trip" and wrongly stop an in-progress trip's background task.
if (gpsDraftDetailsMetadata.status !== 'loaded' || hasHandledAppRestart.current) {
return;
}
hasHandledAppRestart.current = true;

async function handleGpsTripInProgressOnAppRestart() {
await checkAndCleanGpsNotification();
const gpsTrip = await OnyxUtils.get(ONYXKEYS.GPS_DRAFT_DETAILS);

if (!gpsTrip?.isTracking) {
if (!gpsDraftDetails?.isTracking) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Re-read GPS state after notification cleanup

On iOS, checkAndCleanGpsNotification() awaits liveActivityManager.listAll(), so this closure can become stale while it is suspended. If the hydrated state is not tracking and the user starts or resumes a trip before that call finishes, this branch still sees the old false value, detects the newly started background task, and stops it while Onyx remains isTracking: true, causing the trip to stop collecting locations. The removed OnyxUtils.get() ran after this await and observed the latest state; retain the latest subscribed value in a ref or otherwise verify the current tracking state after cleanup before stopping the task.

Useful? React with 👍 / 👎.

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.

This is not something introduced by this PR so I'd leave as is.
@MelvinBot confirm

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Mostly agree it's fine to leave as-is, with one nuance so the call is made with eyes open.

The check-then-act race itself is pre-existing. Even before this PR, once the handler read isTracking: false there was still an await (hasStartedLocationUpdatesAsync, index.native.tsx:51) before stopLocationUpdatesAsync, index.native.tsx:53 — a trip started in that gap would have been wrongly stopped too. So the class of race isn't new.

But strictly, this PR does slightly widen the window. The old OnyxUtils.get() read after await checkAndCleanGpsNotification(), whereas the new code reads the closure snapshot captured before it (index.native.tsx:48→50). On iOS that cleanup awaits liveActivityManager.listAll(), index.ios.ts:165, so that native call is now inside the stale window where it wasn't before. Codex's mechanism is technically valid.

Practically, the risk is negligible — so I'm fine leaving it. This is the once-per-mount app-restart handler; the dangerous branch only runs when there's no active trip on launch, and it would require the user to start a brand-new trip during a sub-second startup async window (the continue-trip modal is itself gated behind the splash screen being hidden). Not a realistic sequence.

If you ever want exact parity with the old read-after-cleanup semantics, the minimal change is to read the latest subscribed value from a ref after await checkAndCleanGpsNotification() instead of the pre-await snapshot — but that's optional and arguably outside this refactor's scope. 👍 to leaving as-is.

const isBackgroundTaskRunning = await hasStartedLocationUpdatesAsync(BACKGROUND_LOCATION_TRACKING_TASK_NAME);
if (isBackgroundTaskRunning) {
stopLocationUpdatesAsync(BACKGROUND_LOCATION_TRACKING_TASK_NAME).catch((error) =>
Expand All @@ -55,7 +61,9 @@ function GPSTripStateChecker() {
}

handleGpsTripInProgressOnAppRestart();
}, [gpsDraftDetails?.isTracking, gpsDraftDetailsMetadata.status]);

useEffect(() => {
return () => {
hasStartedLocationUpdatesAsync(BACKGROUND_LOCATION_TRACKING_TASK_NAME).then((isRunning) => {
if (!isRunning) {
Expand Down
Loading