@@ -9,6 +9,7 @@ import 'package:hive_ce/src/isolate/isolated_box_impl/isolated_box_impl_vm.dart'
99import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate.dart' ;
1010import 'package:hive_ce/src/isolate/isolated_hive_impl/hive_isolate_name.dart' ;
1111import 'package:hive_ce/src/registry/type_registry_impl.dart' ;
12+ import 'package:hive_ce/src/util/debug_utils.dart' ;
1213import 'package:hive_ce/src/util/logger.dart' ;
1314import 'package:hive_ce/src/util/type_utils.dart' ;
1415import '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 }
0 commit comments