Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/fix-disconnect-promise-poison.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/shared-internals': patch
---

Fix a rejected `disconnect()` permanently poisoning later `disconnect()` and `connect()` calls on the same instance.
7 changes: 5 additions & 2 deletions packages/shared-internals/src/client/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {

this.disconnectingPromise = this.performDisconnect();

await this.disconnectingPromise;
this.disconnectingPromise = null;
try {
await this.disconnectingPromise;
} finally {
this.disconnectingPromise = null;
}
}

protected async performDisconnect() {
Expand Down
41 changes: 41 additions & 0 deletions packages/shared-internals/tests/client/ConnectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class MockStreamingSyncImplementation
{
isConnected = false;
isReady = false;
disconnectCalls = 0;
disconnectError: Error | null = null;
readonly receivedUpdates: SubscribedStream[][] = [];

constructor(
Expand All @@ -43,7 +45,11 @@ class MockStreamingSyncImplementation
}

async disconnect() {
this.disconnectCalls++;
this.isConnected = false;
if (this.disconnectError) {
throw this.disconnectError;
}
}

async getWriteCheckpoint() {
Expand Down Expand Up @@ -199,4 +205,39 @@ describe('ConnectionManager', () => {
await harness.manager.disconnect();
});
});

describe('failed disconnect', () => {
test('does not reuse a rejected disconnect for later disconnect and connect', async () => {
const harness = managerWithCreateHook();
await harness.manager.connect(connector, {}, {});
const error = new Error('transient teardown failure');
harness.sync.disconnectError = error;

await expect(harness.manager.disconnect()).rejects.toBe(error);
// performDisconnect already nulled the sync implementation, so a retry has nothing to tear down.
await expect(harness.manager.disconnect()).resolves.toBeUndefined();
expect(harness.syncs[0].disconnectCalls).toBe(1);

await harness.manager.connect(connector, {}, {});
expect(harness.syncs).toHaveLength(2);

await harness.manager.disconnect();
});

test('concurrent disconnects share the in-flight rejection, then a later call retries', async () => {
const harness = managerWithCreateHook();
await harness.manager.connect(connector, {}, {});
const error = new Error('transient teardown failure');
harness.sync.disconnectError = error;

const first = harness.manager.disconnect();
const second = harness.manager.disconnect();

await expect(first).rejects.toBe(error);
await expect(second).rejects.toBe(error);

await expect(harness.manager.disconnect()).resolves.toBeUndefined();
expect(harness.syncs[0].disconnectCalls).toBe(1);
});
});
});