Skip to content

fix(replay): dont set isTerminating on captureReplay for android #3037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 4, 2025
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Changelog

### Unreleased
## Unreleased

### Fixes

- Replay JNI usage with `SentryFlutterPlugin` ([#3036](https://github.com/getsentry/sentry-dart/pull/3036), [#3039](https://github.com/getsentry/sentry-dart/pull/3039))
- Do not set `isTerminating` on `captureReplay` for Android ([#3037](https://github.com/getsentry/sentry-dart/pull/3037))
- Previously segments might be missing on Android replays if an unhandled error happened

## 9.3.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class SentryFlutterPlugin :
"displayRefreshRate" -> displayRefreshRate(result)
"nativeCrash" -> crash()
"setReplayConfig" -> setReplayConfig(call, result)
"captureReplay" -> captureReplay(call.argument("isCrash"), result)
"captureReplay" -> captureReplay(result)
else -> result.notImplemented()
}
}
Expand Down Expand Up @@ -619,14 +619,9 @@ class SentryFlutterPlugin :
}

private fun captureReplay(
isCrash: Boolean?,
result: Result,
) {
if (isCrash == null) {
result.error("5", "Arguments are null", null)
return
}
replay!!.captureReplay(isCrash)
replay!!.captureReplay(isTerminating = false)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a small thin, but maybe we could safley unpack the otpional replay. If for some reason this becomes nil, we will crash here.

Copy link
Contributor Author

@buenaflor buenaflor Jul 4, 2025

Choose a reason for hiding this comment

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

if an error happens in the plugin it doesnt crash, Flutter will still catch it and propagate it to the flutter layer (where we catch it with the SentryNativeSafeInvoker

image

result.success(replay!!.getReplayId().toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class ReplayEventProcessor implements EventProcessor {
isErrorEvent || (isFeedbackEvent && !isWidgetFeedbackEvent);

if (shouldCaptureReplay) {
final isCrash =
event.exceptions?.any((e) => e.mechanism?.handled == false) ?? false;
final replayId = await _binding.captureReplay(isCrash);
final replayId = await _binding.captureReplay();
// If session replay is disabled, this is the first time we receive the ID.
_hub.configureScope((scope) {
// ignore: invalid_use_of_internal_member
Expand Down
2 changes: 1 addition & 1 deletion flutter/lib/src/native/c/sentry_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class SentryNative with SentryNativeSafeInvoker implements SentryNativeBinding {
}

@override
FutureOr<SentryId> captureReplay(bool isCrash) {
FutureOr<SentryId> captureReplay() {
_logNotSupported('capturing replay');
return SentryId.empty();
}
Expand Down
2 changes: 1 addition & 1 deletion flutter/lib/src/native/sentry_native_binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class SentryNativeBinding {

FutureOr<void> setReplayConfig(ReplayConfig config);

FutureOr<SentryId> captureReplay(bool isCrash);
FutureOr<SentryId> captureReplay();

/// Starts a new session.
///
Expand Down
7 changes: 3 additions & 4 deletions flutter/lib/src/native/sentry_native_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,9 @@ class SentryNativeChannel
});

@override
Future<SentryId> captureReplay(bool isCrash) =>
channel.invokeMethod('captureReplay', {
'isCrash': isCrash,
}).then((value) => SentryId.fromId(value as String));
Future<SentryId> captureReplay() => channel
.invokeMethod('captureReplay')
.then((value) => SentryId.fromId(value as String));

@override
FutureOr<void> captureSession() {
Expand Down
2 changes: 1 addition & 1 deletion flutter/lib/src/replay/integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ReplayIntegration extends Integration<SentryFlutterOptions> {

Future<void> captureReplay() async {
if (_native.supportsReplay && _options?.replay.isEnabled == true) {
final replayId = await _native.captureReplay(false);
final replayId = await _native.captureReplay();
_hub?.configureScope((scope) {
// ignore: invalid_use_of_internal_member
scope.replayId = replayId;
Expand Down
2 changes: 1 addition & 1 deletion flutter/lib/src/web/sentry_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class SentryWeb with SentryNativeSafeInvoker implements SentryNativeBinding {
}

@override
FutureOr<SentryId> captureReplay(bool isCrash) {
FutureOr<SentryId> captureReplay() {
throw UnsupportedError(
"$SentryWeb.captureReplay() not supported on this platform");
}
Expand Down
4 changes: 2 additions & 2 deletions flutter/test/feedback/sentry_feedback_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void main() {
return null;
});
final replayId = SentryId.fromId('1988bb1b6f0d4c509e232f0cb9aaeaea');
when(mockBinding.captureReplay(any)).thenAnswer((_) async => replayId);
when(mockBinding.captureReplay()).thenAnswer((_) async => replayId);

final replayIntegration = ReplayIntegration(mockBinding);
fixture.options.addIntegration(replayIntegration);
Expand All @@ -39,7 +39,7 @@ void main() {
);
// await tester.pumpAndSettle();

verify(mockBinding.captureReplay(any)).called(1);
verify(mockBinding.captureReplay()).called(1);
//ignore: invalid_use_of_internal_member
expect(fixture.hub.scope.replayId, replayId);
});
Expand Down
13 changes: 3 additions & 10 deletions flutter/test/mocks.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2043,18 +2043,11 @@ class MockSentryNativeBinding extends _i1.Mock
)) as _i11.FutureOr<void>);

@override
_i11.FutureOr<_i2.SentryId> captureReplay(bool? isCrash) =>
(super.noSuchMethod(
Invocation.method(
#captureReplay,
[isCrash],
),
_i11.FutureOr<_i2.SentryId> captureReplay() => (super.noSuchMethod(
Invocation.method(#captureReplay, []),
returnValue: _i11.Future<_i2.SentryId>.value(_FakeSentryId_5(
this,
Invocation.method(
#captureReplay,
[isCrash],
),
Invocation.method(#captureReplay, []),
)),
) as _i11.FutureOr<_i2.SentryId>);

Expand Down
56 changes: 32 additions & 24 deletions flutter/test/replay/replay_event_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,39 @@ void main() {
fixture = _Fixture();
});

for (var isHandled in [true, false]) {
test(
'captures replay for ${isHandled ? 'handled' : 'unhandled'} exceptions',
() async {
final event = await fixture.apply(isHandled: isHandled);
bool isCrash = verify(fixture.binding.captureReplay(captureAny))
.captured
.single as bool;
expect(isCrash, !isHandled);
expect(event, isNotNull);
});
test('captures replay for feedback event', () async {
final feedback = SentryFeedback(message: 'fixture-message');
final feedbackEvent = SentryEvent(
type: 'feedback',
contexts: Contexts(feedback: feedback),
);

test(
'sets scope replay ID for ${isHandled ? 'handled' : 'unhandled'} exceptions',
() async {
expect(fixture.scope.replayId, isNull);
await fixture.apply(isHandled: isHandled);
expect(fixture.scope.replayId, SentryId.fromId('42'));
});
}
expect(fixture.scope.replayId, isNull);

final processedEvent = await fixture.sut.apply(feedbackEvent, Hint());
expect(processedEvent, isNotNull);

await fixture.apply(hasException: false);

expect(fixture.scope.replayId, isNotNull);
});

test('sets scope replay ID for feedback event', () async {
final feedback = SentryFeedback(message: 'fixture-message');
final feedbackEvent = SentryEvent(
type: 'feedback',
contexts: Contexts(feedback: feedback),
);

final processedEvent = await fixture.sut.apply(feedbackEvent, Hint());
expect(processedEvent, isNotNull);

expect(fixture.scope.replayId, SentryId.fromId('42'));
});

test('does not capture replay for non-errors', () async {
await fixture.apply(hasException: false);
verifyNever(fixture.binding.captureReplay(any));
verifyNever(fixture.binding.captureReplay());
expect(fixture.scope.replayId, isNull);
});
}
Expand All @@ -51,7 +60,7 @@ class _Fixture {
Scope scope = Scope(defaultTestOptions());

_Fixture() {
when(binding.captureReplay(captureAny))
when(binding.captureReplay())
.thenAnswer((_) async => SentryId.fromId('42'));
when(hub.configureScope(any)).thenAnswer((invocation) async {
final callback = invocation.positionalArguments.first as FutureOr<void>
Expand All @@ -60,16 +69,15 @@ class _Fixture {
});
sut = ReplayEventProcessor(hub, binding);
}
Future<SentryEvent?> apply(
{bool hasException = true, bool isHandled = false}) {
Future<SentryEvent?> apply({bool hasException = true}) {
final event = SentryEvent(
eventId: SentryId.newId(),
exceptions: hasException
? [
SentryException(
type: 'type',
value: 'value',
mechanism: Mechanism(type: 'foo', handled: isHandled))
mechanism: Mechanism(type: 'foo'))
]
: [],
);
Expand Down
4 changes: 2 additions & 2 deletions flutter/test/sentry_native_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ void main() {
when(channel.invokeMethod('captureReplay', any))
.thenAnswer((_) => Future.value(sentryId.toString()));

final returnedId = await sut.captureReplay(true);
final returnedId = await sut.captureReplay();

verify(channel.invokeMethod('captureReplay', {'isCrash': true}));
verify(channel.invokeMethod('captureReplay'));
expect(returnedId, sentryId);
});

Expand Down
2 changes: 1 addition & 1 deletion flutter/test/web/sentry_web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void main() {

group('no-op or throwing methods', () {
test('captureReplay throws unsupported error', () {
expect(() => sut.captureReplay(false), throwsUnsupportedError);
expect(() => sut.captureReplay(), throwsUnsupportedError);
});

test('methods execute without calling JS binding', () {
Expand Down
Loading