Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: cancel voice call audio on timeout (#5497)

[._.]/ Adam Eivye90d33f35dc4
diff --git a/client/src/pages/VoiceCallHost.jsx b/client/src/pages/VoiceCallHost.jsx
index 339500f91a..5232ca85ad 100644
--- a/client/src/pages/VoiceCallHost.jsx
+++ b/client/src/pages/VoiceCallHost.jsx
@@ -68,17 +68,18 @@ export default function VoiceCallHost() {
const [captureState, setCaptureState] = useState(null);
const [level, setLevel] = useState(0);
const [notice, setNotice] = useState(null);
- const audio = useRef({ context: null, stream: null, node: null, source: null, outputId: null, pending: [] });
+ const audio = useRef({ context: null, stream: null, node: null, source: null, playing: null, outputId: null, pending: [] });
const lockRelease = useRef(null);
const teardown = useCallback(() => {
const current = audio.current;
+ current.playing?.pause?.();
current.node?.port?.close?.();
current.node?.disconnect?.();
current.source?.disconnect?.();
current.stream?.getTracks?.().forEach((track) => track.stop());
current.context?.close?.();
- audio.current = { context: null, stream: null, node: null, source: null, outputId: current.outputId, pending: [] };
+ audio.current = { context: null, stream: null, node: null, source: null, playing: null, outputId: current.outputId, pending: [] };
setLevel(0);
}, []);
@@ -91,13 +92,19 @@ export default function VoiceCallHost() {
const element = new Audio();
const blob = new Blob([wav], { type: 'audio/wav' });
element.src = URL.createObjectURL(blob);
+ audio.current.playing = element;
try {
if (audio.current.outputId) await element.setSinkId(audio.current.outputId);
+ if (audio.current.playing !== element) return;
await element.play();
+ if (audio.current.playing !== element) element.pause();
} catch (error) {
- setNotice(`Could not play into the call: ${error.message}`);
+ if (audio.current.playing === element) setNotice(`Could not play into the call: ${error.message}`);
} finally {
- element.addEventListener('ended', () => URL.revokeObjectURL(element.src), { once: true });
+ element.addEventListener('ended', () => {
+ URL.revokeObjectURL(element.src);
+ if (audio.current.playing === element) audio.current.playing = null;
+ }, { once: true });
}
}, []);
@@ -196,7 +203,7 @@ export default function VoiceCallHost() {
};
source.connect(node);
- audio.current = { context, stream, node, source, outputId: output?.deviceId ?? null, pending: [] };
+ audio.current = { context, stream, node, source, playing: null, outputId: output?.deviceId ?? null, pending: [] };
// Two literal emits, not a computed event name — a computed
// `socket.emit(x ? 'a' : 'b')` is invisible to the static
// socket-event inventory scan (`server/lib/socketEventInventory.js`),
@@ -245,13 +252,20 @@ export default function VoiceCallHost() {
setAttached(Boolean(snapshot?.hostAttached));
};
const onTts = ({ wav }) => { if (wav) playToCall(wav); };
+ const onTtsCancel = () => {
+ const playing = audio.current.playing;
+ playing?.pause?.();
+ if (audio.current.playing === playing) audio.current.playing = null;
+ };
socket.on('voice:call:state', onCallState);
socket.on('voice:capture:state', onCaptureState);
socket.on('voice:call:tts', onTts);
+ socket.on('voice:tts:cancel', onTtsCancel);
return () => {
socket.off('voice:call:state', onCallState);
socket.off('voice:capture:state', onCaptureState);
socket.off('voice:call:tts', onTts);
+ socket.off('voice:tts:cancel', onTtsCancel);
if (modeRef.current === 'call') socket.emit('voice:call:detach');
else socket.emit('voice:capture:stop');
teardown();
diff --git a/client/src/pages/VoiceCallHost.test.jsx b/client/src/pages/VoiceCallHost.test.jsx
index fea9460b89..513a2bb25a 100644
--- a/client/src/pages/VoiceCallHost.test.jsx
+++ b/client/src/pages/VoiceCallHost.test.jsx
@@ -117,6 +117,28 @@ describe('VoiceCallHost', () => {
expect(screen.getByText('Detach call host')).toBeTruthy();
});
+ it('pauses call audio when the server cancels a timed-out voice turn', async () => {
+ const element = {
+ addEventListener: vi.fn(),
+ play: vi.fn(() => Promise.resolve()),
+ pause: vi.fn(),
+ setSinkId: vi.fn(() => Promise.resolve()),
+ src: '',
+ };
+ vi.stubGlobal('Audio', function FakeAudio() { return element; });
+ vi.stubGlobal('URL', {
+ createObjectURL: vi.fn(() => 'blob:call-audio'),
+ revokeObjectURL: vi.fn(),
+ });
+ render(<VoiceCallHost />, { wrapper: MemoryRouter });
+
+ act(() => socket.__fire('voice:call:tts', { wav: new ArrayBuffer(8) }));
+ await waitFor(() => expect(element.play).toHaveBeenCalledTimes(1));
+ act(() => socket.__fire('voice:tts:cancel'));
+
+ expect(element.pause).toHaveBeenCalledTimes(1);
+ });
+
it('detaches on unmount so a closed tab does not leave a phantom host', () => {
const { unmount } = render(<VoiceCallHost />, { wrapper: MemoryRouter });
unmount();
diff --git a/client/src/services/voiceClient.test.js b/client/src/services/voiceClient.test.js
index e24aa69716..a6451e7844 100644
--- a/client/src/services/voiceClient.test.js
+++ b/client/src/services/voiceClient.test.js
@@ -97,7 +97,8 @@ describe('voice playback cancellation', () => {
// The old queue can finish after cancellation. It must not decrement the
// newly reset depth or create the queued stale source.
activeSource.onended?.();
- await vi.waitFor(() => expect(audio.sources).toHaveLength(1));
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(audio.sources).toHaveLength(1);
});
it('drops a frame whose decode finishes after cancellation', async () => {