Skip to content

Commit 14bfcf7

Browse files
committed
fix: ignore engine closed events for data track packets which are in flight / un delivered on engine close
I have noticed in running the data track end to end test, that "engine closed" gets thrown fairly often. I think the move away from the while/sleep loop in waitForBufferStatusLow (which wasn't in my data track subscription updates PR) has newly resulted in more waitForBufferStatusLow s being in flight at once so there's a much higher likelihood that the data channel won't be drained upon engine close. To fix this, I've added a param to waitForBufferStatusLow to allow ignoring engine closed events. Not sure if this is the best thing to do but it fixes the tests, and I think matches the ethos of "send packets as fast as possible" / doesn't introduce any sort of explicit locking.
1 parent 6696692 commit 14bfcf7

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/room/RTCEngine.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
15191519
// buffer status to not be low before continuing.
15201520
switch (bufferStatusLowBehavior) {
15211521
case 'wait':
1522-
await this.waitForBufferStatusLow(kind);
1522+
await this.waitForBufferStatusLow(kind, 'ignore');
15231523
break;
15241524
case 'drop':
15251525
// this.log.warn(`dropping lossy data channel message`, this.logContext);
@@ -1580,9 +1580,12 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
15801580
}
15811581
};
15821582

1583-
async waitForBufferStatusLow(kind: DataChannelKind) {
1583+
async waitForBufferStatusLow(
1584+
kind: DataChannelKind,
1585+
engineCloseBehavior: 'throw' | 'ignore' = 'throw',
1586+
) {
15841587
return new TypedPromise<void, UnexpectedConnectionState>(async (resolve, reject) => {
1585-
if (this.isClosed) {
1588+
if (this.isClosed && engineCloseBehavior === 'throw') {
15861589
reject(new UnexpectedConnectionState('engine closed'));
15871590
}
15881591
if (this.isBufferStatusLow(kind)) {
@@ -1593,7 +1596,9 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
15931596
reject(new UnexpectedConnectionState(`DataChannel not found, kind: ${kind}`));
15941597
return;
15951598
}
1596-
this.bufferStatusLowClosingFuture.promise.catch((e) => reject(e));
1599+
if (engineCloseBehavior === 'throw') {
1600+
this.bufferStatusLowClosingFuture.promise.catch((e) => reject(e));
1601+
}
15971602
dc.addEventListener('bufferedamountlow', () => resolve(), {
15981603
once: true,
15991604
});

0 commit comments

Comments
 (0)