Skip to content

Commit bed68be

Browse files
Avoid broken stream reader error
1 parent 3f5f489 commit bed68be

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

.changeset/angry-planets-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': patch
3+
---
4+
5+
Improved potential race condition when closing HTTP stream connections.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import PACKAGE from '../../../../package.json' with { type: 'json' };
66
import { AbortOperation } from '../../../utils/AbortOperation.js';
77
import { DataStream } from '../../../utils/DataStream.js';
88
import { PowerSyncCredentials } from '../../connection/PowerSyncCredentials.js';
9-
import { StreamingSyncRequest } from './streaming-sync-types.js';
109
import { WebsocketClientTransport } from './WebsocketClientTransport.js';
10+
import { StreamingSyncRequest } from './streaming-sync-types.js';
11+
;
1112

1213
export type BSONImplementation = typeof BSON;
1314

@@ -557,9 +558,11 @@ export abstract class AbstractRemote {
557558
// Create a new stream splitting the response at line endings while also handling cancellations
558559
// by closing the reader.
559560
const reader = res.body.getReader();
561+
let readerReleased = false;
560562
// This will close the network request and read stream
561563
const closeReader = async () => {
562564
try {
565+
readerReleased = true;
563566
await reader.cancel();
564567
} catch (ex) {
565568
// an error will throw if the reader hasn't been used yet
@@ -581,6 +584,9 @@ export abstract class AbstractRemote {
581584

582585
const l = stream.registerListener({
583586
lowWater: async () => {
587+
if (stream.closed || abortSignal?.aborted || readerReleased) {
588+
return
589+
}
584590
try {
585591
let didCompleteLine = false;
586592
while (!didCompleteLine) {

0 commit comments

Comments
 (0)