Skip to content

Commit

Permalink
Fixed LateInitializationError (#526)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Sartori <[email protected]>
  • Loading branch information
ueman and HugoSart authored Jul 26, 2021
1 parent 3e7deaa commit bca59ab
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

* Fix: Re-initialization of Flutter SDK (#526)
* Enhancement: Call `toString()` on all non-serializable fields (#528)
* Fix: Always call `Flutter.onError` in order to not swallow messages (#533)
* Bump: Android SDK to 5.1.0-beta.6 (#535)
Expand Down
15 changes: 8 additions & 7 deletions dart/lib/src/isolate_error_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import 'sentry_options.dart';
import 'throwable_mechanism.dart';

class IsolateErrorIntegration extends Integration {
late RawReceivePort _receivePort;
RawReceivePort? _receivePort;

@override
FutureOr<void> call(Hub hub, SentryOptions options) async {
_receivePort = _createPort(hub, options);

Isolate.current.addErrorListener(_receivePort.sendPort);

final safeReceivePort = _receivePort = _createPort(hub, options);
Isolate.current.addErrorListener(safeReceivePort.sendPort);
options.sdk.addIntegration('isolateErrorIntegration');
}

@override
void close() {
_receivePort.close();
Isolate.current.removeErrorListener(_receivePort.sendPort);
if (_receivePort != null) {
final safeReceivePort = _receivePort!;
safeReceivePort.close();
Isolate.current.removeErrorListener(safeReceivePort.sendPort);
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions dart/test/initialization_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@TestOn('vm')

import 'package:sentry/sentry.dart';
import 'package:test/test.dart';

import 'mocks.dart';

// Tests for the following issue
// https://github.com/getsentry/sentry-dart/issues/508
// There are no asserts, test are succesfull if no exceptions are thrown.
void main() {
tearDown(() async {
await Sentry.close();
});

test('async re-initilization', () async {
await Sentry.init((options) {
options.dsn = fakeDsn;
});

await Sentry.close();

await Sentry.init((options) {
options.dsn = fakeDsn;
});
});

// This is the failure from
// https://github.com/getsentry/sentry-dart/issues/508
test('re-initilization', () {
Sentry.init((options) {
options.dsn = fakeDsn;
});

Sentry.close();

Sentry.init((options) {
options.dsn = fakeDsn;
});
});
}
4 changes: 2 additions & 2 deletions flutter/lib/src/default_integrations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
NativeSdkIntegration(this._channel);

final MethodChannel _channel;
late SentryFlutterOptions _options;
SentryFlutterOptions? _options;

@override
FutureOr<void> call(Hub hub, SentryFlutterOptions options) async {
Expand Down Expand Up @@ -243,7 +243,7 @@ class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
try {
await _channel.invokeMethod<void>('closeNativeSdk');
} catch (exception, stackTrace) {
_options.logger(
_options?.logger(
SentryLevel.fatal,
'nativeSdkIntegration failed to be closed',
exception: exception,
Expand Down
42 changes: 42 additions & 0 deletions flutter/test/initialization_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@TestOn('vm')

import 'package:flutter_test/flutter_test.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

import 'mocks.dart';

// Tests for the following issue
// https://github.com/getsentry/sentry-dart/issues/508
// There are no asserts, test are succesfull if no exceptions are thrown.
void main() {
tearDown(() async {
await Sentry.close();
});

test('async re-initilization', () async {
await SentryFlutter.init((options) {
options.dsn = fakeDsn;
});

await Sentry.close();

await SentryFlutter.init((options) {
options.dsn = fakeDsn;
});
});

// This is the failure from
// https://github.com/getsentry/sentry-dart/issues/508
test('re-initilization', () {
SentryFlutter.init((options) {
options.dsn = fakeDsn;
});

Sentry.close();

SentryFlutter.init((options) {
options.dsn = fakeDsn;
});
});
}

0 comments on commit bca59ab

Please sign in to comment.