-
Notifications
You must be signed in to change notification settings - Fork 327
fix(avatar): handle clear-buffer RPC failures #2045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smorimoto
wants to merge
4
commits into
livekit:main
Choose a base branch
from
smorimoto:fix/avatar-clear-buffer-rpc-failures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
518cc6a
fix(avatar): handle clear buffer RPC failures
smorimoto 804786c
test(avatar): use typed Room fixture for RPC failure behaviour
smorimoto 9c913ad
fix(avatar): settle playout after clear-buffer RPC failure
smorimoto 9d162b4
refactor(avatar): simplify clear-buffer failure handling
smorimoto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@livekit/agents': patch | ||
| --- | ||
|
|
||
| Prevent rejected avatar clear-buffer RPCs from emitting unhandled rejections or stranding | ||
| playout waiters. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // SPDX-FileCopyrightText: 2026 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { AudioFrame, ByteStreamWriter, Room } from '@livekit/rtc-node'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { AudioOutput, type PlaybackFinishedEvent } from '../io.js'; | ||
| import { DataStreamAudioOutput } from './datastream_io.js'; | ||
|
|
||
| const { logger } = vi.hoisted(() => ({ | ||
| logger: { | ||
| debug: vi.fn(), | ||
| warn: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../log.js', () => ({ | ||
| log: () => logger, | ||
| })); | ||
|
|
||
| function createByteStreamWriter(): ByteStreamWriter { | ||
| return new ByteStreamWriter(new WritableStream<Uint8Array>(), { | ||
| streamId: 'test-stream', | ||
| mimeType: 'application/octet-stream', | ||
| topic: 'lk.audio_stream', | ||
| timestamp: 0, | ||
| name: 'audio', | ||
| }); | ||
| } | ||
|
|
||
| function createRoom(performRpcImpl: () => Promise<string>) { | ||
| const avatar = { identity: 'avatar' }; | ||
| const room = new Room(); | ||
| const performRpc = vi.fn(performRpcImpl); | ||
|
|
||
| Object.defineProperties(room, { | ||
| isConnected: { value: true }, | ||
| localParticipant: { | ||
| value: { | ||
| performRpc, | ||
| registerRpcMethod: vi.fn(), | ||
| streamBytes: vi.fn(async () => createByteStreamWriter()), | ||
| }, | ||
| }, | ||
| remoteParticipants: { value: new Map([[avatar.identity, avatar]]) }, | ||
| }); | ||
|
|
||
| return { room, performRpc }; | ||
| } | ||
|
|
||
| function createFrame(): AudioFrame { | ||
| return new AudioFrame(new Int16Array(160), 8000, 1, 160); | ||
| } | ||
|
|
||
| function nextTick(): Promise<void> { | ||
| return new Promise((resolve) => setImmediate(resolve)); | ||
| } | ||
|
|
||
| describe('DataStreamAudioOutput.clearBuffer', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| DataStreamAudioOutput._playbackFinishedRpcRegistered = false; | ||
| DataStreamAudioOutput._playbackFinishedHandlers = {}; | ||
| }); | ||
|
|
||
| it('settles pending playout when the clear-buffer RPC rejects', async () => { | ||
| const error = new Error('Failed to send'); | ||
| const { room, performRpc } = createRoom(() => Promise.reject(error)); | ||
| const output = new DataStreamAudioOutput({ | ||
| room, | ||
| destinationIdentity: 'avatar', | ||
| }); | ||
|
|
||
| await output.captureFrame(createFrame()); | ||
| output.flush(); | ||
|
|
||
| const playout = output.waitForPlayout(); | ||
| output.clearBuffer(); | ||
|
|
||
| await expect(playout).resolves.toEqual({ | ||
| playbackPosition: 0, | ||
| interrupted: true, | ||
| }); | ||
| expect(performRpc).toHaveBeenCalledExactlyOnceWith({ | ||
| destinationIdentity: 'avatar', | ||
| method: 'lk.clear_buffer', | ||
| payload: '', | ||
| }); | ||
| expect(logger.warn).toHaveBeenCalledExactlyOnceWith( | ||
| { error, destinationIdentity: 'avatar' }, | ||
| 'failed to perform clear buffer rpc', | ||
| ); | ||
| }); | ||
|
|
||
| it('does not let a late rejection settle a newer segment', async () => { | ||
| let rejectRpc: (reason?: unknown) => void = () => {}; | ||
| const rpc = new Promise<string>((_resolve, reject) => { | ||
| rejectRpc = reject; | ||
| }); | ||
| const { room } = createRoom(() => rpc); | ||
| const output = new DataStreamAudioOutput({ | ||
| room, | ||
| destinationIdentity: 'avatar', | ||
| }); | ||
|
|
||
| await output.captureFrame(createFrame()); | ||
| output.flush(); | ||
| output.clearBuffer(); | ||
|
|
||
| output.onPlaybackFinished({ playbackPosition: 0.01, interrupted: true }); | ||
|
|
||
| await nextTick(); | ||
| await output.captureFrame(createFrame()); | ||
| output.flush(); | ||
| const secondPlayout = output.waitForPlayout(); | ||
|
|
||
| rejectRpc(new Error('Failed to send')); | ||
| await vi.waitFor(() => expect(logger.warn).toHaveBeenCalledOnce()); | ||
|
|
||
| const secondEvent = { playbackPosition: 0.02, interrupted: false }; | ||
| output.onPlaybackFinished(secondEvent); | ||
| await expect(secondPlayout).resolves.toEqual(secondEvent); | ||
| }); | ||
|
|
||
| it('settles every segment pending when clearBuffer was called', async () => { | ||
| const { room } = createRoom(() => Promise.reject(new Error('Failed to send'))); | ||
| const output = new DataStreamAudioOutput({ | ||
| room, | ||
| destinationIdentity: 'avatar', | ||
| }); | ||
| const playbackEvents: PlaybackFinishedEvent[] = []; | ||
| output.on(AudioOutput.EVENT_PLAYBACK_FINISHED, (event) => playbackEvents.push(event)); | ||
|
|
||
| await output.captureFrame(createFrame()); | ||
| output.flush(); | ||
| await nextTick(); | ||
| await output.captureFrame(createFrame()); | ||
| output.flush(); | ||
|
|
||
| const playout = output.waitForPlayout(); | ||
| output.clearBuffer(); | ||
| await expect(playout).resolves.toEqual({ playbackPosition: 0, interrupted: true }); | ||
|
|
||
| expect(playbackEvents).toEqual([ | ||
| { playbackPosition: 0, interrupted: true }, | ||
| { playbackPosition: 0, interrupted: true }, | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.