Skip to content

Commit 720ad7a

Browse files
committed
Reformat and changeset
1 parent 25efd50 commit 720ad7a

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

.changeset/violet-falcons-build.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': patch
3+
---
4+
5+
Fix a race condition causing sync changes during uploads not to be applied.

packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -717,21 +717,24 @@ The next upload iteration will be delayed.`);
717717

718718
private async applyCheckpoint(checkpoint: Checkpoint, abort: AbortSignal) {
719719
let result = await this.options.adapter.syncLocalDatabase(checkpoint);
720+
const pending = this.pendingCrudUpload;
721+
720722
if (!result.checkpointValid) {
721723
this.logger.debug('Checksum mismatch in checkpoint, will reconnect');
722724
// This means checksums failed. Start again with a new checkpoint.
723725
// TODO: better back-off
724726
await new Promise((resolve) => setTimeout(resolve, 50));
725727
return { applied: false, endIteration: true };
726-
} else if (!result.ready) {
728+
} else if (!result.ready && pending != null) {
727729
// We have pending entries in the local upload queue or are waiting to confirm a write
728-
// checkpoint. See if that is happening right now.
729-
const pending = this.pendingCrudUpload;
730-
if (pending != null) {
731-
await Promise.race([pending, onAbortPromise(abort)]);
732-
}
733-
734-
if (abort.aborted || pending == null) {
730+
// checkpoint, which prevented this checkpoint from applying. Wait for that to complete and
731+
// try again.
732+
this.logger.debug(
733+
'Could not apply checkpoint due to local data. Waiting for in-progress upload before retrying.'
734+
);
735+
await Promise.race([pending, onAbortPromise(abort)]);
736+
737+
if (abort.aborted) {
735738
return { applied: false, endIteration: true };
736739
}
737740

@@ -752,7 +755,7 @@ The next upload iteration will be delayed.`);
752755

753756
return { applied: true, endIteration: false };
754757
} else {
755-
this.logger.debug('Could not apply checkpoint even after waiting for uploads. Waiting for next sync complete line.');
758+
this.logger.debug('Could not apply checkpoint. Waiting for next sync complete line.');
756759
return { applied: false, endIteration: false };
757760
}
758761
}

packages/common/src/utils/async.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ export function onAbortPromise(signal: AbortSignal): Promise<void> {
5656
} else {
5757
signal.onabort = () => resolve();
5858
}
59-
})
59+
});
6060
}

packages/web/tests/stream.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ describe('Streaming', { sequential: true }, () => {
5454
let resolveUploadPromise: () => void;
5555
let resolveUploadStartedPromise: () => void;
5656
const completeUploadPromise = new Promise<void>((resolve) => {
57-
resolveUploadPromise = resolve
57+
resolveUploadPromise = resolve;
5858
});
5959
const uploadStartedPromise = new Promise<void>((resolve) => {
60-
resolveUploadStartedPromise = resolve
60+
resolveUploadStartedPromise = resolve;
6161
});
6262

6363
async function expectUserRows(amount: number) {
64-
const row = await powersync.get<{r: number}>('SELECT COUNT(*) AS r FROM users');
64+
const row = await powersync.get<{ r: number }>('SELECT COUNT(*) AS r FROM users');
6565
expect(row.r).toBe(amount);
6666
}
6767

@@ -90,7 +90,7 @@ describe('Streaming', { sequential: true }, () => {
9090
remote.generateCheckpoint.mockImplementation(() => {
9191
return {
9292
data: {
93-
write_checkpoint: '1',
93+
write_checkpoint: '1'
9494
}
9595
};
9696
});
@@ -114,7 +114,7 @@ describe('Streaming', { sequential: true }, () => {
114114
object_id: '2',
115115
object_type: 'users',
116116
data: '{"id": "test1", "name": "additional entry"}'
117-
},
117+
}
118118
]
119119
}
120120
});

packages/web/tests/utils/MockStreamOpenFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class MockRemote extends AbstractRemote {
4747
this.generateCheckpoint = vi.fn(() => {
4848
return {
4949
data: {
50-
write_checkpoint: '1000',
50+
write_checkpoint: '1000'
5151
}
5252
};
5353
});

0 commit comments

Comments
 (0)