@@ -191,13 +191,24 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
191
191
/// Throttle time between CRUD operations
192
192
/// Defaults to 10 milliseconds.
193
193
Duration crudThrottleTime = const Duration (milliseconds: 10 )}) async {
194
- _connectMutex.lock (() =>
195
- _connect (connector: connector, crudThrottleTime: crudThrottleTime));
194
+ Zone current = Zone .current;
195
+
196
+ Future <void > reconnect () {
197
+ return _connectMutex.lock (() => _connect (
198
+ connector: connector,
199
+ crudThrottleTime: crudThrottleTime,
200
+ // The reconnect function needs to run in the original zone,
201
+ // to avoid recursive lock errors.
202
+ reconnect: current.bindCallback (reconnect)));
203
+ }
204
+
205
+ await reconnect ();
196
206
}
197
207
198
208
Future <void > _connect (
199
209
{required PowerSyncBackendConnector connector,
200
- required Duration crudThrottleTime}) async {
210
+ required Duration crudThrottleTime,
211
+ required Future <void > Function () reconnect}) async {
201
212
await initialize ();
202
213
203
214
// Disconnect if connected
@@ -266,7 +277,9 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
266
277
logger.severe ('Sync Isolate error' , message);
267
278
268
279
// Reconnect
269
- connect (connector: connector);
280
+ // Use the param like this instead of directly calling connect(), to avoid recursive
281
+ // locks in some edge cases.
282
+ reconnect ();
270
283
});
271
284
272
285
disconnected () {
@@ -532,7 +545,7 @@ Future<void> _powerSyncDatabaseIsolate(
532
545
533
546
CommonDatabase ? db;
534
547
final mutex = args.dbRef.mutex.open ();
535
- StreamingSyncImplementation ? _sync ;
548
+ StreamingSyncImplementation ? openedStreamingSync ;
536
549
537
550
rPort.listen ((message) async {
538
551
if (message is List ) {
@@ -548,7 +561,7 @@ Future<void> _powerSyncDatabaseIsolate(
548
561
updateController.close ();
549
562
upstreamDbClient.close ();
550
563
// Abort any open http requests, and wait for it to be closed properly
551
- await _sync ? .abort ();
564
+ await openedStreamingSync ? .abort ();
552
565
// No kill the Isolate
553
566
Isolate .current.kill ();
554
567
}
@@ -596,7 +609,7 @@ Future<void> _powerSyncDatabaseIsolate(
596
609
uploadCrud: uploadCrud,
597
610
updateStream: updateController.stream,
598
611
retryDelay: args.retryDelay);
599
- _sync = sync ;
612
+ openedStreamingSync = sync ;
600
613
sync .streamingSync ();
601
614
sync .statusStream.listen ((event) {
602
615
sPort.send (['status' , event]);
0 commit comments