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
4 changes: 4 additions & 0 deletions hive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.19.3

- IsolatedHive: Handles stale send ports on hot restart

## 2.19.2

- Updates `isolate_channel` to `0.6.0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:hive_ce/src/isolate/isolated_box_impl/isolated_box_impl_vm.dart'
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate.dart';
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate_name.dart';
import 'package:hive_ce/src/registry/type_registry_impl.dart';
import 'package:hive_ce/src/util/debug_utils.dart';
import 'package:hive_ce/src/util/logger.dart';
import 'package:hive_ce/src/util/type_utils.dart';
import 'package:isolate_channel/isolate_channel.dart';
Expand Down Expand Up @@ -38,8 +39,10 @@ class IsolatedHiveImpl extends TypeRegistryImpl
);

@override
void onConnect(SendPort send) =>
_isolateNameServer?.registerPortWithName(send, hiveIsolateName);
void onConnect(SendPort send) {
_isolateNameServer?.removePortNameMapping(hiveIsolateName);
_isolateNameServer?.registerPortWithName(send, hiveIsolateName);
}

@override
void onExit() => _isolateNameServer?.removePortNameMapping(hiveIsolateName);
Expand All @@ -63,9 +66,22 @@ class IsolatedHiveImpl extends TypeRegistryImpl
final send =
_isolateNameServer?.lookupPortByName(hiveIsolateName) as SendPort?;

final IsolateConnection connection;
IsolateConnection connection;
if (send != null) {
connection = await connectToIsolate(send);
try {
var connectFuture = connectToIsolate(send);

// Sometimes the INS does not get cleared on a hot restart
// This results in the send port being stale
// This would be unsafe in release mode
if (kDebugMode) {
connectFuture =
connectFuture.timeout(const Duration(milliseconds: 250));
}
connection = await connectFuture;
} on TimeoutException {
connection = await _spawnHiveIsolate();
}
} else {
connection = await _spawnHiveIsolate();
}
Expand Down
2 changes: 1 addition & 1 deletion hive/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: hive_ce
description: Hive Community Edition - A spiritual continuation of Hive v2
version: 2.19.2
version: 2.19.3
homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive

topics:
Expand Down
20 changes: 20 additions & 0 deletions hive/test/integration/isolate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:hive_ce/hive_ce.dart';
import 'package:hive_ce/src/hive_impl.dart';
import 'package:hive_ce/src/isolate/handler/isolate_entry_point.dart';
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate.dart';
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate_name.dart';
import 'package:hive_ce/src/isolate/isolated_hive_impl/isolated_hive_impl.dart';
import 'package:hive_ce/src/util/logger.dart';
import 'package:isolate_channel/isolate_channel.dart';
Expand Down Expand Up @@ -366,6 +367,25 @@ void main() {
);
expect(await box.get('key'), 'value');
});

test('Stale send port', () async {
final dir = await getTempDir();
final hive = IsolatedHiveImpl();
addTearDown(hive.close);

final ins = TestIns();
ins.registerPortWithName(ReceivePort().sendPort, hiveIsolateName);

var spawned = false;
(hive as HiveIsolate).spawnHiveIsolate = () {
spawned = true;
return spawnIsolate(isolateEntryPoint);
};

await hive.init(dir.path, isolateNameServer: ins);

expect(spawned, isTrue);
});
},
onPlatform: {
'chrome': Skip('Isolates are not supported on web'),
Expand Down