Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
### Features

- Support `SENTRY_ENVIRONMENT` in bare React Native builds ([#5823](https://github.com/getsentry/sentry-react-native/pull/5823))

- Add `expoUpdatesListenerIntegration` that records breadcrumbs for Expo Updates lifecycle events ([#5795](https://github.com/getsentry/sentry-react-native/pull/5795))
- Tracks update checks, downloads, errors, rollbacks, and restarts as `expo.updates` breadcrumbs
- Enabled by default in Expo apps (requires `expo-updates` to be installed)
-
### Fixes

- Fix native frames measurements being dropped due to race condition ([#5813](https://github.com/getsentry/sentry-react-native/pull/5813))
Expand Down
Binary file modified packages/core/android/libs/replay-stubs.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions packages/core/src/js/integrations/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
eventOriginIntegration,
expoConstantsIntegration,
expoContextIntegration,
expoUpdatesListenerIntegration,
functionToStringIntegration,
hermesProfilingIntegration,
httpClientIntegration,
Expand Down Expand Up @@ -133,6 +134,7 @@ export function getDefaultIntegrations(options: ReactNativeClientOptions): Integ

integrations.push(expoContextIntegration());
integrations.push(expoConstantsIntegration());
integrations.push(expoUpdatesListenerIntegration());

if (options.spotlight && __DEV__) {
const sidecarUrl = typeof options.spotlight === 'string' ? options.spotlight : undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/js/integrations/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export { screenshotIntegration } from './screenshot';
export { viewHierarchyIntegration } from './viewhierarchy';
export { expoContextIntegration } from './expocontext';
export { expoConstantsIntegration } from './expoconstants';
export { expoUpdatesListenerIntegration } from './expoupdateslistener';
export { spotlightIntegration } from './spotlight';
export { mobileReplayIntegration } from '../replay/mobilereplay';
export { feedbackIntegration } from '../feedback/integration';
Expand Down
180 changes: 180 additions & 0 deletions packages/core/src/js/integrations/expoupdateslistener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { addBreadcrumb, debug, type Integration, type SeverityLevel } from '@sentry/core';
import type { ReactNativeClient } from '../client';
import { isExpo, isExpoGo } from '../utils/environment';

const INTEGRATION_NAME = 'ExpoUpdatesListener';

const BREADCRUMB_CATEGORY = 'expo.updates';

/**
* Describes the state machine context from `expo-updates`.
* We define our own minimal type to avoid a hard dependency on `expo-updates`.
*/
interface UpdatesNativeStateMachineContext {
isChecking: boolean;
isDownloading: boolean;
isUpdateAvailable: boolean;
isUpdatePending: boolean;
isRestarting: boolean;
latestManifest?: { id?: string };
downloadedManifest?: { id?: string };
rollback?: { commitTime: string };
checkError?: Error;
downloadError?: Error;
}

interface UpdatesNativeStateChangeEvent {
context: UpdatesNativeStateMachineContext;
}

interface UpdatesStateChangeSubscription {
remove(): void;
}

interface ExpoUpdatesExports {
addUpdatesStateChangeListener: (
listener: (event: UpdatesNativeStateChangeEvent) => void,
) => UpdatesStateChangeSubscription;
latestContext: UpdatesNativeStateMachineContext;
}

/**
* Tries to load `expo-updates` and retrieve exports needed by this integration.
* Returns `undefined` if `expo-updates` is not installed.
*/
function getExpoUpdatesExports(): ExpoUpdatesExports | undefined {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const expoUpdates = require('expo-updates') as Partial<ExpoUpdatesExports>;
if (typeof expoUpdates.addUpdatesStateChangeListener === 'function') {
return expoUpdates as ExpoUpdatesExports;
}
} catch (_) {
// that happens when expo-updates is not installed
}
return undefined;
}

interface StateTransition {
field: keyof UpdatesNativeStateMachineContext;
message: string;
level: SeverityLevel;
getData?: (ctx: UpdatesNativeStateMachineContext) => Record<string, unknown> | undefined;
}

const STATE_TRANSITIONS: StateTransition[] = [
{ field: 'isChecking', message: 'Checking for update', level: 'info' },
{
field: 'isUpdateAvailable',
message: 'Update available',
level: 'info',
getData: ctx => {
const updateId = ctx.latestManifest?.id;
return updateId ? { updateId } : undefined;
},
},
{ field: 'isDownloading', message: 'Downloading update', level: 'info' },
{
field: 'isUpdatePending',
message: 'Update downloaded',
level: 'info',
getData: ctx => {
const updateId = ctx.downloadedManifest?.id;
return updateId ? { updateId } : undefined;
},
},
{
field: 'checkError',
message: 'Update check failed',
level: 'error',
getData: ctx => ({
error: (ctx.checkError as Error).message || String(ctx.checkError),
}),
},
{
field: 'downloadError',
message: 'Update download failed',
level: 'error',
getData: ctx => ({
error: (ctx.downloadError as Error).message || String(ctx.downloadError),
}),
},
{
field: 'rollback',
message: 'Rollback directive received',
level: 'warning',
getData: ctx => ({
commitTime: ctx.rollback!.commitTime,
}),
},
{ field: 'isRestarting', message: 'Restarting for update', level: 'info' },
];

/**
* Listens to Expo Updates native state machine changes and records
* breadcrumbs for meaningful transitions such as checking for updates,
* downloading updates, errors, rollbacks, and restarts.
*/
export const expoUpdatesListenerIntegration = (): Integration => {
let subscription: UpdatesStateChangeSubscription | undefined;

function setup(client: ReactNativeClient): void {
client.on('afterInit', () => {
if (!isExpo() || isExpoGo()) {
return;
}

const expoUpdates = getExpoUpdatesExports();
if (!expoUpdates) {
debug.log('[ExpoUpdatesListener] expo-updates is not available, skipping.');
return;
}

// Remove any previous subscription to prevent duplicate breadcrumbs
// if Sentry.init() is called multiple times.
subscription?.remove();

// Seed with the current state so that the first event does not
// generate spurious breadcrumbs for already-truthy fields.
let previousContext: Partial<UpdatesNativeStateMachineContext> = expoUpdates.latestContext ?? {};

subscription = expoUpdates.addUpdatesStateChangeListener((event: UpdatesNativeStateChangeEvent) => {
const ctx = event.context;
handleStateChange(previousContext, ctx);
previousContext = ctx;
});
Copy link

Choose a reason for hiding this comment

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

Listener callback lacks try/catch, risking app crash

Medium Severity

The addListener callback from expo-updates is not wrapped in a try/catch. If event.context is unexpectedly null/undefined, or if any getData function throws (e.g., the non-null assertion ctx.rollback!.commitTime at line 98, or the as Error cast at lines 82/90), the exception propagates unhandled into expo-updates internals and could crash the host app. Per project rules, SDK instrumentation errors must never crash the host application — dangerous paths need try/catch with graceful fallback. This violates the rule from the rules file requiring error paths to be handled explicitly.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be valid 👍

});

client.on('close', () => {
subscription?.remove();
subscription = undefined;
});
}

return {
name: INTEGRATION_NAME,
setup,
};
};

/**
* Compares previous and current state machine contexts and emits
* breadcrumbs for meaningful transitions (falsy→truthy).
*
* @internal Exposed for testing purposes
*/
export function handleStateChange(
previous: Partial<UpdatesNativeStateMachineContext>,
current: UpdatesNativeStateMachineContext,
): void {
for (const transition of STATE_TRANSITIONS) {
if (!previous[transition.field] && current[transition.field]) {
addBreadcrumb({
category: BREADCRUMB_CATEGORY,
message: transition.message,
level: transition.level,
data: transition.getData?.(current),
});
}
}
}
Loading
Loading