Skip to content

Commit 10fdca5

Browse files
committed
Fix deadlock on immediate connect call
1 parent d560134 commit 10fdca5

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

packages/powersync_core/lib/src/database/native/native_powersync_database.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ class PowerSyncDatabaseImpl
122122
required AbortController abort,
123123
Map<String, dynamic>? params,
124124
}) async {
125-
await initialize();
126125
final dbRef = database.isolateConnectionFactory();
127126

128127
bool triedSpawningIsolate = false;

packages/powersync_core/lib/src/database/powersync_db_mixin.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
264264
Duration crudThrottleTime = const Duration(milliseconds: 10),
265265
Map<String, dynamic>? params,
266266
}) async {
267+
// The initialization process acquires a sync connect lock (through
268+
// updateSchema), so ensure the database is ready before we try to acquire
269+
// the lock for the connection.
270+
await initialize();
271+
267272
clientParams = params;
268273
var thisConnectAborter = AbortController();
269274

packages/powersync_core/lib/src/database/web/web_powersync_database.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ class PowerSyncDatabaseImpl
118118
required AbortController abort,
119119
Map<String, dynamic>? params,
120120
}) async {
121-
await initialize();
122-
123121
final crudStream =
124122
database.onChange(['ps_crud'], throttle: crudThrottleTime);
125123

packages/powersync_core/test/streaming_sync_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ void main() {
6161
server.close();
6262
});
6363

64+
test('can connect as initial operation', () async {
65+
final server = await createServer();
66+
final ignoreLogger = Logger.detached('powersync.test');
67+
68+
final pdb = await testUtils.setupPowerSync(
69+
path: path, logger: ignoreLogger, initialize: false);
70+
pdb.retryDelay = Duration(milliseconds: 5000);
71+
72+
await pdb.connect(connector: TestConnector(() async {
73+
return PowerSyncCredentials(endpoint: server.endpoint, token: 'token');
74+
}));
75+
76+
await expectLater(
77+
pdb.statusStream,
78+
emitsThrough(
79+
isA<SyncStatus>().having((e) => e.connected, 'connected', isTrue)),
80+
);
81+
});
82+
6483
test('full powersync reconnect', () async {
6584
// Test repeatedly creating new PowerSync connections, then disconnect
6685
// and close the connection.

packages/powersync_core/test/utils/abstract_test_utils.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,18 @@ abstract class AbstractTestUtils {
9494
SqliteOptions options = const SqliteOptions.defaults()});
9595

9696
/// Creates a SqliteDatabaseConnection
97-
Future<PowerSyncDatabase> setupPowerSync(
98-
{String? path, Schema? schema, Logger? logger}) async {
97+
Future<PowerSyncDatabase> setupPowerSync({
98+
String? path,
99+
Schema? schema,
100+
Logger? logger,
101+
bool initialize = true,
102+
}) async {
99103
final db = PowerSyncDatabase.withFactory(await testFactory(path: path),
100104
schema: schema ?? defaultSchema,
101105
logger: logger ?? _makeTestLogger(name: _testName));
102-
await db.initialize();
106+
if (initialize) {
107+
await db.initialize();
108+
}
103109
addTearDown(db.close);
104110
return db;
105111
}

packages/powersync_core/test/utils/web_test_utils.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ class TestUtils extends AbstractTestUtils {
7676

7777
@override
7878
Future<PowerSyncDatabase> setupPowerSync(
79-
{String? path, Schema? schema, Logger? logger}) async {
79+
{String? path,
80+
Schema? schema,
81+
Logger? logger,
82+
bool initialize = true}) async {
8083
await _isInitialized;
81-
return super.setupPowerSync(path: path, schema: schema, logger: logger);
84+
return super.setupPowerSync(
85+
path: path, schema: schema, logger: logger, initialize: initialize);
8286
}
8387

8488
@override

0 commit comments

Comments
 (0)