Skip to content

Commit bca59ab

Browse files
uemanHugoSart
andauthored
Fixed LateInitializationError (#526)
Co-authored-by: Hugo Sartori <hugo_sart@hotmail.com>
1 parent 3e7deaa commit bca59ab

File tree

5 files changed

+94
-9
lines changed

5 files changed

+94
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
* Fix: Re-initialization of Flutter SDK (#526)
34
* Enhancement: Call `toString()` on all non-serializable fields (#528)
45
* Fix: Always call `Flutter.onError` in order to not swallow messages (#533)
56
* Bump: Android SDK to 5.1.0-beta.6 (#535)

dart/lib/src/isolate_error_integration.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import 'sentry_options.dart';
1010
import 'throwable_mechanism.dart';
1111

1212
class IsolateErrorIntegration extends Integration {
13-
late RawReceivePort _receivePort;
13+
RawReceivePort? _receivePort;
1414

1515
@override
1616
FutureOr<void> call(Hub hub, SentryOptions options) async {
17-
_receivePort = _createPort(hub, options);
18-
19-
Isolate.current.addErrorListener(_receivePort.sendPort);
20-
17+
final safeReceivePort = _receivePort = _createPort(hub, options);
18+
Isolate.current.addErrorListener(safeReceivePort.sendPort);
2119
options.sdk.addIntegration('isolateErrorIntegration');
2220
}
2321

2422
@override
2523
void close() {
26-
_receivePort.close();
27-
Isolate.current.removeErrorListener(_receivePort.sendPort);
24+
if (_receivePort != null) {
25+
final safeReceivePort = _receivePort!;
26+
safeReceivePort.close();
27+
Isolate.current.removeErrorListener(safeReceivePort.sendPort);
28+
}
2829
}
2930
}
3031

dart/test/initialization_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@TestOn('vm')
2+
3+
import 'package:sentry/sentry.dart';
4+
import 'package:test/test.dart';
5+
6+
import 'mocks.dart';
7+
8+
// Tests for the following issue
9+
// https://github.com/getsentry/sentry-dart/issues/508
10+
// There are no asserts, test are succesfull if no exceptions are thrown.
11+
void main() {
12+
tearDown(() async {
13+
await Sentry.close();
14+
});
15+
16+
test('async re-initilization', () async {
17+
await Sentry.init((options) {
18+
options.dsn = fakeDsn;
19+
});
20+
21+
await Sentry.close();
22+
23+
await Sentry.init((options) {
24+
options.dsn = fakeDsn;
25+
});
26+
});
27+
28+
// This is the failure from
29+
// https://github.com/getsentry/sentry-dart/issues/508
30+
test('re-initilization', () {
31+
Sentry.init((options) {
32+
options.dsn = fakeDsn;
33+
});
34+
35+
Sentry.close();
36+
37+
Sentry.init((options) {
38+
options.dsn = fakeDsn;
39+
});
40+
});
41+
}

flutter/lib/src/default_integrations.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
196196
NativeSdkIntegration(this._channel);
197197

198198
final MethodChannel _channel;
199-
late SentryFlutterOptions _options;
199+
SentryFlutterOptions? _options;
200200

201201
@override
202202
FutureOr<void> call(Hub hub, SentryFlutterOptions options) async {
@@ -243,7 +243,7 @@ class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
243243
try {
244244
await _channel.invokeMethod<void>('closeNativeSdk');
245245
} catch (exception, stackTrace) {
246-
_options.logger(
246+
_options?.logger(
247247
SentryLevel.fatal,
248248
'nativeSdkIntegration failed to be closed',
249249
exception: exception,

flutter/test/initialization_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@TestOn('vm')
2+
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:sentry/sentry.dart';
5+
import 'package:sentry_flutter/sentry_flutter.dart';
6+
7+
import 'mocks.dart';
8+
9+
// Tests for the following issue
10+
// https://github.com/getsentry/sentry-dart/issues/508
11+
// There are no asserts, test are succesfull if no exceptions are thrown.
12+
void main() {
13+
tearDown(() async {
14+
await Sentry.close();
15+
});
16+
17+
test('async re-initilization', () async {
18+
await SentryFlutter.init((options) {
19+
options.dsn = fakeDsn;
20+
});
21+
22+
await Sentry.close();
23+
24+
await SentryFlutter.init((options) {
25+
options.dsn = fakeDsn;
26+
});
27+
});
28+
29+
// This is the failure from
30+
// https://github.com/getsentry/sentry-dart/issues/508
31+
test('re-initilization', () {
32+
SentryFlutter.init((options) {
33+
options.dsn = fakeDsn;
34+
});
35+
36+
Sentry.close();
37+
38+
SentryFlutter.init((options) {
39+
options.dsn = fakeDsn;
40+
});
41+
});
42+
}

0 commit comments

Comments
 (0)