|
3 | 3 | container, |
4 | 4 | logger as defaultLogger, |
5 | 5 | Logger, |
| 6 | + ReplicationAbortedError, |
6 | 7 | ReplicationAssertionError |
7 | 8 | } from '@powersync/lib-services-framework'; |
8 | 9 | import { |
@@ -345,16 +346,33 @@ export class WalStream { |
345 | 346 | try { |
346 | 347 | this.initPromise = this.initReplication(); |
347 | 348 | await this.initPromise; |
348 | | - streamPromise = this.streamChanges(); |
349 | | - loopPromise = this.snapshotter.replicationLoop(); |
350 | | - await Promise.race([loopPromise, streamPromise]); |
| 349 | + // These Promises are both expected to run until aborted or error. |
| 350 | + streamPromise = this.streamChanges().finally(() => { |
| 351 | + this.abortController.abort(); |
| 352 | + }); |
| 353 | + loopPromise = this.snapshotter.replicationLoop().finally(() => { |
| 354 | + this.abortController.abort(); |
| 355 | + }); |
| 356 | + const results = await Promise.allSettled([loopPromise, streamPromise]); |
| 357 | + // First, prioritize non-aborted errors |
| 358 | + for (let result of results) { |
| 359 | + if (result.status == 'rejected' && !(result.reason instanceof ReplicationAbortedError)) { |
| 360 | + throw result.reason; |
| 361 | + } |
| 362 | + } |
| 363 | + // Then include aborted errors |
| 364 | + for (let result of results) { |
| 365 | + if (result.status == 'rejected') { |
| 366 | + throw result.reason; |
| 367 | + } |
| 368 | + } |
| 369 | + // If we get here, both Promises completed successfully, which is unexpected. |
| 370 | + throw new ReplicationAssertionError(`Replication loop exited unexpectedly`); |
351 | 371 | } catch (e) { |
352 | 372 | await this.storage.reportError(e); |
353 | 373 | throw e; |
354 | 374 | } finally { |
355 | 375 | this.abortController.abort(); |
356 | | - // Wait for both to finish, to ensure proper cleanup. |
357 | | - await Promise.all([loopPromise?.catch((_) => {}), streamPromise?.catch((_) => {})]); |
358 | 376 | } |
359 | 377 | } |
360 | 378 |
|
|
0 commit comments