Skip to content

Commit d3ec164

Browse files
authored
Merge pull request #289 from IO-Design-Team/feature/hot-restart-stale-send-port
Handle stale send port on hot restart
2 parents cd85b3d + 8da288d commit d3ec164

4 files changed

Lines changed: 45 additions & 5 deletions

File tree

hive/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.19.3
2+
3+
- IsolatedHive: Handles stale send ports on hot restart
4+
15
## 2.19.2
26

37
- Updates `isolate_channel` to `0.6.0`

hive/lib/src/isolate/isolated_hive_impl/impl/isolated_hive_impl_vm.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:hive_ce/src/isolate/isolated_box_impl/isolated_box_impl_vm.dart'
99
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate.dart';
1010
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate_name.dart';
1111
import 'package:hive_ce/src/registry/type_registry_impl.dart';
12+
import 'package:hive_ce/src/util/debug_utils.dart';
1213
import 'package:hive_ce/src/util/logger.dart';
1314
import 'package:hive_ce/src/util/type_utils.dart';
1415
import 'package:isolate_channel/isolate_channel.dart';
@@ -38,8 +39,10 @@ class IsolatedHiveImpl extends TypeRegistryImpl
3839
);
3940

4041
@override
41-
void onConnect(SendPort send) =>
42-
_isolateNameServer?.registerPortWithName(send, hiveIsolateName);
42+
void onConnect(SendPort send) {
43+
_isolateNameServer?.removePortNameMapping(hiveIsolateName);
44+
_isolateNameServer?.registerPortWithName(send, hiveIsolateName);
45+
}
4346

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

66-
final IsolateConnection connection;
69+
IsolateConnection connection;
6770
if (send != null) {
68-
connection = await connectToIsolate(send);
71+
try {
72+
var connectFuture = connectToIsolate(send);
73+
74+
// Sometimes the INS does not get cleared on a hot restart
75+
// This results in the send port being stale
76+
// This would be unsafe in release mode
77+
if (kDebugMode) {
78+
connectFuture =
79+
connectFuture.timeout(const Duration(milliseconds: 250));
80+
}
81+
connection = await connectFuture;
82+
} on TimeoutException {
83+
connection = await _spawnHiveIsolate();
84+
}
6985
} else {
7086
connection = await _spawnHiveIsolate();
7187
}

hive/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: hive_ce
22
description: Hive Community Edition - A spiritual continuation of Hive v2
3-
version: 2.19.2
3+
version: 2.19.3
44
homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive
55

66
topics:

hive/test/integration/isolate_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:hive_ce/hive_ce.dart';
88
import 'package:hive_ce/src/hive_impl.dart';
99
import 'package:hive_ce/src/isolate/handler/isolate_entry_point.dart';
1010
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate.dart';
11+
import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate_name.dart';
1112
import 'package:hive_ce/src/isolate/isolated_hive_impl/isolated_hive_impl.dart';
1213
import 'package:hive_ce/src/util/logger.dart';
1314
import 'package:isolate_channel/isolate_channel.dart';
@@ -366,6 +367,25 @@ void main() {
366367
);
367368
expect(await box.get('key'), 'value');
368369
});
370+
371+
test('Stale send port', () async {
372+
final dir = await getTempDir();
373+
final hive = IsolatedHiveImpl();
374+
addTearDown(hive.close);
375+
376+
final ins = TestIns();
377+
ins.registerPortWithName(ReceivePort().sendPort, hiveIsolateName);
378+
379+
var spawned = false;
380+
(hive as HiveIsolate).spawnHiveIsolate = () {
381+
spawned = true;
382+
return spawnIsolate(isolateEntryPoint);
383+
};
384+
385+
await hive.init(dir.path, isolateNameServer: ins);
386+
387+
expect(spawned, isTrue);
388+
});
369389
},
370390
onPlatform: {
371391
'chrome': Skip('Isolates are not supported on web'),

0 commit comments

Comments
 (0)