Skip to content

Commit ac39f7c

Browse files
committed
chore: clean up event promises
1 parent f6c101e commit ac39f7c

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

packages/transport-webrtc/src/stream.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,30 @@ export class WebRTCStream extends AbstractStream {
207207
options?.signal?.throwIfAborted()
208208
this.receivedFinAck = Promise.withResolvers<void>()
209209

210+
// don't wait for FIN_ACK forever
211+
const signal = options?.signal ?? AbortSignal.timeout(this.finAckTimeout)
212+
213+
// allow cleaning up event promises
214+
const eventPromises = [
215+
pEvent(this.channel, 'close', {
216+
signal
217+
}),
218+
pEvent(this.channel, 'error', {
219+
signal
220+
})
221+
]
222+
210223
// wait for either:
211224
// 1. the FIN_ACK to be received
212225
// 2. the datachannel to close
213226
// 3. timeout
214227
await Promise.any([
215-
raceSignal(this.receivedFinAck.promise, options?.signal),
216-
pEvent(this.channel, 'close'),
217-
pEvent(this.channel, 'error'),
218-
pEvent(AbortSignal.timeout(this.finAckTimeout), 'abort')
228+
raceSignal(this.receivedFinAck.promise, signal),
229+
...eventPromises
219230
])
231+
.finally(() => {
232+
eventPromises.forEach(p => p.cancel())
233+
})
220234
}
221235

222236
async sendCloseRead (options?: AbortOptions): Promise<void> {

0 commit comments

Comments
 (0)