Skip to content

Commit 6934a95

Browse files
Fox32toubatbrian
andauthored
fix: handle disconnect during init (#1065)
Co-authored-by: Brian Yin <brian.yin@livekit.io>
1 parent 93cf081 commit 6934a95

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

.changeset/moody-ligers-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/agents": patch
3+
---
4+
5+
Handle participant disconnect during init

agents/src/voice/room_io/room_io.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { WritableStreamDefaultWriter } from 'node:stream/web';
2121
import { ATTRIBUTE_PUBLISH_ON_BEHALF, TOPIC_CHAT } from '../../constants.js';
2222
import { log } from '../../log.js';
2323
import { IdentityTransform } from '../../stream/identity_transform.js';
24-
import { Future, Task } from '../../utils.js';
24+
import { Future, Task, waitForAbort } from '../../utils.js';
2525
import { type AgentSession } from '../agent_session.js';
2626
import {
2727
AgentSessionEventTypes,
@@ -177,7 +177,10 @@ export class RoomIO {
177177
: this.inputOptions.participantIdentity ?? null;
178178
}
179179
private async init(signal: AbortSignal): Promise<void> {
180-
await this.roomConnectedFuture.await;
180+
await Promise.race([this.roomConnectedFuture.await, waitForAbort(signal)]);
181+
if (signal.aborted) {
182+
return;
183+
}
181184

182185
for (const participant of this.room.remoteParticipants.values()) {
183186
this.onParticipantConnected(participant);
@@ -186,7 +189,15 @@ export class RoomIO {
186189
return;
187190
}
188191

189-
const participant = await this.participantAvailableFuture.await;
192+
const participant = await Promise.race([
193+
this.participantAvailableFuture.await,
194+
waitForAbort(signal),
195+
]);
196+
197+
if (!participant) {
198+
return;
199+
}
200+
190201
this.setParticipant(participant.identity);
191202

192203
// init agent outputs

0 commit comments

Comments
 (0)