Skip to content

Commit 86ad06c

Browse files
author
Kasper Overgård Nielsen
authored
RDART-1028: Use Zone.current.bindUnaryCallbackGuarded for RawReceivePort.handler (#1683)
* Use Zone.current.bindUnaryCallbackGuarded for RawReceivePort.handler * Update CHANGELOG * Add comment explaining fix
1 parent 2dd3bff commit 86ad06c

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Fixed
1010
* Private fields did not work with default values. (Issue [#1663](https://github.com/realm/realm-dart/issues/1663))
11+
* Invoke scheduler callback on Zone.current. (Issue [#1676](https://github.com/realm/realm-dart/issues/1676))
1112

1213
* Having links in a nested collections would leave the file inconsistent if the top object is removed. (Core 14.7.0)
1314

packages/realm_dart/lib/src/scheduler.dart

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2022 MongoDB, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import 'dart:async';
45
import 'dart:ffi';
56
import 'dart:isolate';
67
import 'package:realm_dart/src/logging.dart';
@@ -20,25 +21,36 @@ class Scheduler {
2021

2122
Scheduler._() {
2223
_receivePortFinalizer.attach(this, _receivePort, detach: this);
23-
24-
_receivePort.handler = (dynamic message) {
25-
if (message is List) {
26-
// currently the only `message as List` is from the logger.
27-
final category = LogCategory.fromString(message[0] as String);
28-
final level = LogLevel.values[message[1] as int];
29-
final text = message[2] as String;
30-
Realm.logger.raise((category: category, level: level, message: text));
31-
} else if (message is int) {
32-
realmCore.invokeScheduler(message);
33-
} else {
34-
Realm.logger.log(LogLevel.error, 'Unexpected Scheduler message type: ${message.runtimeType} - $message');
35-
}
36-
};
37-
24+
// There be dragons here!!!
25+
//
26+
// As of Dart 3.4 (Flutter 3.22) we started seeing uncaught exceptions on
27+
// the receivePort handler (issue #1676), stating that:
28+
// "argument value for 'return_value' is null" in
29+
// RealmLibrary.realm_scheduler_perform_work, but obviously a void method
30+
// don't return anything, so this is really a Dart issue.
31+
//
32+
// However, by ensuring the callback happens in the current zone (as it
33+
// rightfully should), and using bindUnaryCallbackGuarded, we can avoid
34+
// these.
35+
_receivePort.handler = Zone.current.bindUnaryCallbackGuarded(_handle);
3836
final sendPort = _receivePort.sendPort;
3937
handle = realmCore.createScheduler(Isolate.current.hashCode, sendPort.nativePort);
4038
}
4139

40+
void _handle(dynamic message) {
41+
if (message is List) {
42+
// currently the only `message as List` is from the logger.
43+
final category = LogCategory.fromString(message[0] as String);
44+
final level = LogLevel.values[message[1] as int];
45+
final text = message[2] as String;
46+
Realm.logger.raise((category: category, level: level, message: text));
47+
} else if (message is int) {
48+
realmCore.invokeScheduler(message);
49+
} else {
50+
Realm.logger.log(LogLevel.error, 'Unexpected Scheduler message type: ${message.runtimeType} - $message');
51+
}
52+
}
53+
4254
void stop() {
4355
if (handle.released) {
4456
return;

0 commit comments

Comments
 (0)