The best way to control Claude Code, Codex and more from any computer or phone
fix(desktop): voice input recovers after Linux audio restarts (#32376)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Lakshmi Tanmay committed
Nov 15, 2026 at 10:51 UTC
16a70050f479435f4022390d93765e1443f4bbaf
12 files changed
+265
-20
apps/desktop/src/electron/ElectronAudio.test.ts
new
+60
@@ -0,0 +1,60 @@
1
+import * as Effect from "effect/Effect";
2
+import * as HostProcess from "@t3tools/shared/HostProcess";
3
+import { app } from "electron";
4
+import { expect, vi } from "vite-plus/test";
5
+import { it } from "@effect/vitest";
6
+import * as Fiber from "effect/Fiber";
7
+
8
+const { getAppMetrics, kill } = vi.hoisted(() => ({ getAppMetrics: vi.fn(), kill: vi.fn() }));
9
+vi.mock("node:process", () => ({ kill }));
10
+vi.mock("electron", async () => {
11
+ const { EventEmitter } = await import("node:events");
12
+ return { app: Object.assign(new EventEmitter(), { getAppMetrics }) };
13
+});
14
+
15
+import { restart } from "./ElectronAudio.ts";
16
+
17
+const audio = { pid: 42, type: "Utility", serviceName: "audio.mojom.AudioService" };
18
+
19
+it.effect("coalesces recovery and waits for its own audio utility to exit", () =>
20
+ Effect.gen(function* () {
21
+ getAppMetrics.mockReturnValue([
22
+ { pid: 1, type: "Utility", serviceName: "network.mojom.NetworkService" },
23
+ { pid: 2, type: "Browser", serviceName: audio.serviceName },
24
+ audio,
25
+ ]);
26
+ const started = Promise.withResolvers<void>();
27
+ kill.mockReset().mockImplementation(() => {
28
+ started.resolve();
29
+ });
30
+ const pending = yield* Effect.forkChild(
31
+ Effect.all([restart(), restart()], { concurrency: "unbounded" }),
32
+ );
33
+ yield* Effect.promise(() => started.promise);
34
+ app.emit(
35
+ "child-process-gone",
36
+ {},
37
+ { type: "Utility", serviceName: "network.mojom.NetworkService" },
38
+ );
39
+ expect(app.listenerCount("child-process-gone")).toBe(1);
40
+ app.emit("child-process-gone", {}, audio);
41
+ expect(yield* Fiber.join(pending)).toEqual([true, true]);
42
+ expect(kill.mock.calls).toEqual([[42, "SIGTERM"]]);
43
+ expect(app.listenerCount("child-process-gone")).toBe(0);
44
+ }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
45
+);
46
+
47
+it.effect.each(["linux", "darwin", "win32"] as const)(
48
+ "leaves other processes alone on %s",
49
+ (platform) =>
50
+ Effect.gen(function* () {
51
+ getAppMetrics.mockReturnValue([
52
+ { pid: 1, type: "Utility", serviceName: "network.mojom.NetworkService" },
53
+ ]);
54
+ kill.mockReset();
55
+ expect(yield* restart().pipe(Effect.provideService(HostProcess.Platform, platform))).toBe(
56
+ false,
57
+ );
58
+ expect(kill).not.toHaveBeenCalled();
59
+ }),
60
+);
apps/desktop/src/electron/ElectronAudio.ts
new
+62
@@ -0,0 +1,62 @@
1
+// @effect-diagnostics globalTimers:off -- The shared native restart deadline must outlive individual IPC callers.
2
+// @effect-diagnostics nodeBuiltinImport:off -- Only Electron's main process can restart its Chromium audio utility.
3
+import * as NodeProcess from "node:process";
4
+import * as Effect from "effect/Effect";
5
+import * as Schema from "effect/Schema";
6
+import * as HostProcess from "@t3tools/shared/HostProcess";
7
+import { app, type Details, type Event } from "electron";
8
+
9
+class AudioServiceRestartError extends Schema.TaggedError<AudioServiceRestartError>()(
10
+ "AudioServiceRestartError",
11
+ { cause: Schema.Defect() },
12
+) {}
13
+
14
+let pendingRestart: Promise<boolean> | undefined;
15
+
16
+export const restart = Effect.fn("ElectronAudio.restart")(function* () {
17
+ if ((yield* HostProcess.Platform) !== "linux") return false;
18
+ return yield* Effect.tryPromise({
19
+ try: () => {
20
+ if (pendingRestart) return pendingRestart;
21
+ const audio = app
22
+ .getAppMetrics()
23
+ .find(
24
+ (metric) =>
25
+ metric.type === "Utility" && metric.serviceName === "audio.mojom.AudioService",
26
+ );
27
+ if (!audio) return Promise.resolve(false);
28
+ // A failed PulseAudio context survives new getUserMedia calls; Chromium reconnects when its utility exits.
29
+ pendingRestart = new Promise<boolean>((resolve, reject) => {
30
+ const cleanup = () => {
31
+ clearTimeout(timer);
32
+ app.removeListener("child-process-gone", onGone);
33
+ };
34
+ const onGone = (_event: Event, details: Details) => {
35
+ if (details.type === "Utility" && details.serviceName === audio.serviceName) {
36
+ cleanup();
37
+ resolve(true);
38
+ }
39
+ };
40
+ const timer = setTimeout(() => {
41
+ cleanup();
42
+ resolve(false);
43
+ }, 5_000);
44
+ app.on("child-process-gone", onGone);
45
+ try {
46
+ NodeProcess.kill(audio.pid, "SIGTERM");
47
+ } catch (cause) {
48
+ cleanup();
49
+ reject(cause);
50
+ }
51
+ }).finally(() => {
52
+ pendingRestart = undefined;
53
+ });
54
+ return pendingRestart;
55
+ },
56
+ catch: (cause) => new AudioServiceRestartError({ cause }),
57
+ }).pipe(
58
+ Effect.catch((cause) =>
59
+ Effect.logWarning("Could not restart the audio service", cause).pipe(Effect.as(false)),
60
+ ),
61
+ );
62
+});
apps/desktop/src/ipc/DesktopIpcHandlers.ts
+2
@@ -44,6 +44,7 @@ import {
44
getWindowFullscreenState,
45
openExternal,
46
readMedia,
47
+ restartAudioService,
48
openSystemSettings,
49
checkSystemPermission,
50
pasteAsText,
@@ -109,6 +110,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
110
yield* ipc.handle(getHostEnvironmentId);
111
yield* ipc.handle(getLocalEnvironmentBearerToken);
112
113
+ yield* ipc.handle(restartAudioService);
114
yield* ipc.handle(getClientSettings);
115
yield* ipc.handle(setClientSettings);
116
yield* ipc.handle(getConnectionCatalog);
apps/desktop/src/ipc/channels.ts
+2
@@ -140,3 +140,5 @@ export const RECEIVE_PROVIDER_AUTH_CALLBACK_CHANNEL = "desktop:receive-provider-
140
export const CANCEL_PROVIDER_AUTH_CALLBACK_CHANNEL = "desktop:cancel-provider-auth-callback";
141
export const TAKE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:take-legacy-local-storage";
142
export const COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:complete-legacy-local-storage";
143
+
144
+export const RESTART_AUDIO_SERVICE_CHANNEL = "desktop:restart-audio-service";
apps/desktop/src/ipc/methods/window.ts
+8
@@ -37,6 +37,7 @@ import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
37
import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
38
import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
39
import * as ElectronApp from "../../electron/ElectronApp.ts";
40
+import * as ElectronAudio from "../../electron/ElectronAudio.ts";
41
import * as ElectronDialog from "../../electron/ElectronDialog.ts";
42
import * as ElectronMenu from "../../electron/ElectronMenu.ts";
43
import * as ElectronShell from "../../electron/ElectronShell.ts";
@@ -595,3 +596,10 @@ export const checkSystemPermission = DesktopIpc.makeIpcMethod({
596
return yield* Effect.promise(check);
597
}),
598
});
599
+
600
+export const restartAudioService = DesktopIpc.makeIpcMethod({
601
+ channel: IpcChannels.RESTART_AUDIO_SERVICE_CHANNEL,
602
+ payload: Schema.Void,
603
+ result: Schema.Boolean,
604
+ handler: ElectronAudio.restart,
605
+});
apps/desktop/src/preload.ts
+1
@@ -115,6 +115,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
115
ipcRenderer.sendSync(IpcChannels.GET_RELAY_TELEMETRY_ENABLED_CHANNEL) === true,
116
getPathForFile: (file: File) => webUtils.getPathForFile(file),
117
getClientPlatform: () => clientPlatform,
118
+ restartAudioService: () => ipcRenderer.invoke(IpcChannels.RESTART_AUDIO_SERVICE_CHANNEL),
119
setNotificationBadge: (badge) =>
120
ipcRenderer.invoke(IpcChannels.SET_NOTIFICATION_BADGE_CHANNEL, badge),
121
onNotificationBadgeClear: (listener) => {
apps/web/src/components/settings/MicrophoneTest.tsx
+2
-2
@@ -3,7 +3,7 @@ import { MicIcon, PauseIcon, PlayIcon, RotateCcwIcon, SquareIcon, XIcon } from "
3
import { useEffect, useRef, useState } from "react";
4
5
import { useStableCallback } from "~/hooks/useStableCallback";
6
-import { openMicrophone } from "../../speech/browserPcmCapture";
6
+import { requestMicrophone } from "../../speech/requestMicrophone";
7
import workletUrl from "../../speech/pcmWorklet.ts?worker&url";
8
import { VoiceWaveform } from "../chat/VoiceInputPill";
9
import { cn } from "../../lib/utils";
@@ -92,7 +92,7 @@ export function MicrophoneTest({
92
const current: Capture = {};
93
capture.current = current;
94
try {
95
- const stream = await openMicrophone(microphoneId);
95
+ const stream = await requestMicrophone(microphoneId);
96
if (capture.current !== current) {
97
stream.getTracks().forEach((track) => track.stop());
98
return;
apps/web/src/speech/browserPcmCapture.browser.test.ts
+4
-3
@@ -1,5 +1,6 @@
1
import { expect, it, vi } from "vite-plus/test";
2
-import { createBrowserPcmCapture, openMicrophone } from "./browserPcmCapture";
2
+import { createBrowserPcmCapture } from "./browserPcmCapture";
3
+import { requestMicrophone } from "./requestMicrophone";
4
5
it("records from the chosen microphone, cancels an unanswered prompt, and stops a late stream", async () => {
6
let grantPermission!: (media: MediaStream) => void;
@@ -42,11 +43,11 @@ it("falls back to the system default when the saved microphone is gone, and repo
43
.spyOn(navigator.mediaDevices, "getUserMedia")
44
.mockRejectedValueOnce(new DOMException("gone", "OverconstrainedError"))
45
.mockResolvedValueOnce(media);
45
- await expect(openMicrophone("blue-yeti")).resolves.toBe(media);
46
+ await expect(requestMicrophone("blue-yeti")).resolves.toBe(media);
47
expect(microphone).toHaveBeenLastCalledWith({ audio: true });
48
49
const denied = new DOMException("denied", "NotAllowedError");
50
microphone.mockRejectedValueOnce(denied);
50
- await expect(openMicrophone("blue-yeti")).rejects.toBe(denied);
51
+ await expect(requestMicrophone("blue-yeti")).rejects.toBe(denied);
52
expect(microphone).toHaveBeenCalledTimes(3);
53
});
apps/web/src/speech/browserPcmCapture.ts
+2
-15
@@ -4,26 +4,13 @@ import type {
4
VoiceStreamingSession,
5
VoiceTranscriptionOptions,
6
} from "@t3tools/client-runtime/voice-input";
7
+import { requestMicrophone } from "./requestMicrophone";
8
import workletUrl from "./pcmWorklet.ts?worker&url";
9
10
type PreparedBrowserTranscription = PreparedVoiceTranscription & {
11
readonly streaming?: VoiceStreamingSession & { readonly feed: (pcm: Float32Array) => void };
12
};
13
13
-/** Opens a microphone. A saved device the browser can no longer find falls back to System default. */
14
-export async function openMicrophone(microphoneId: string): Promise<MediaStream> {
15
- if (!microphoneId) return navigator.mediaDevices.getUserMedia({ audio: true });
16
- try {
17
- return await navigator.mediaDevices.getUserMedia({
18
- audio: { deviceId: { exact: microphoneId } },
19
- });
20
- } catch (error) {
21
- const name = typeof error === "object" && error !== null && "name" in error ? error.name : "";
22
- if (name !== "OverconstrainedError" && name !== "NotFoundError") throw error;
23
- return navigator.mediaDevices.getUserMedia({ audio: true });
24
- }
25
-}
26
-
14
export function createBrowserPcmCapture(
15
input: {
16
readonly getMicrophoneId: () => string | Promise<string>;
@@ -77,7 +64,7 @@ export function createBrowserPcmCapture(
64
captureSignal.throwIfAborted();
65
const captured = await new Promise<MediaStream>((resolve, reject) => {
66
const onAbort = () => reject(captureSignal.reason);
80
- const permission = openMicrophone(microphoneId);
67
+ const permission = requestMicrophone(microphoneId, captureSignal);
68
captureSignal.addEventListener("abort", onAbort, { once: true });
69
void permission.then(
70
(media) => {
apps/web/src/speech/requestMicrophone.test.ts
new
+83
@@ -0,0 +1,83 @@
1
+import { expect, it, vi } from "vite-plus/test";
2
+import { requestMicrophone } from "./requestMicrophone";
3
+
4
+function setup(input: {
5
+ readonly getUserMedia: ReturnType<typeof vi.fn>;
6
+ readonly platform?: string;
7
+ readonly restartAudioService?: () => Promise<boolean>;
8
+}) {
9
+ const navigator = vi.spyOn(globalThis, "navigator", "get").mockReturnValue({
10
+ mediaDevices: { getUserMedia: input.getUserMedia },
11
+ } as unknown as Navigator);
12
+ vi.stubGlobal("window", {
13
+ desktopBridge: {
14
+ getClientPlatform: () => input.platform ?? "linux",
15
+ ...(input.restartAudioService ? { restartAudioService: input.restartAudioService } : {}),
16
+ },
17
+ });
18
+ return {
19
+ [Symbol.dispose]() {
20
+ navigator.mockRestore();
21
+ vi.unstubAllGlobals();
22
+ },
23
+ };
24
+}
25
+
26
+it.each(["NotFoundError", "NotReadableError"])(
27
+ "reopens the Linux desktop microphone once after %s",
28
+ async (name) => {
29
+ const stream = {} as MediaStream;
30
+ const getUserMedia = vi
31
+ .fn()
32
+ .mockRejectedValueOnce(new DOMException("Disconnected", name))
33
+ .mockResolvedValue(stream);
34
+ const restartAudioService = vi.fn().mockResolvedValue(true);
35
+ using _env = setup({ getUserMedia, restartAudioService });
36
+ expect(await requestMicrophone("")).toBe(stream);
37
+ expect(getUserMedia.mock.calls).toEqual([[{ audio: true }], [{ audio: true }]]);
38
+ expect(restartAudioService).toHaveBeenCalledOnce();
39
+ },
40
+);
41
+
42
+it.each([
43
+ { name: "NotAllowedError", platform: "linux", recovered: true, calls: 1 },
44
+ { name: "OverconstrainedError", platform: "linux", recovered: true, calls: 1 },
45
+ { name: "NotFoundError", platform: "darwin", recovered: true, calls: 1 },
46
+ { name: "NotFoundError", platform: "linux", recovered: false, calls: 1 },
47
+ { name: "NotFoundError", platform: "linux", recovered: undefined, calls: 1 },
48
+ { name: "NotReadableError", platform: "linux", recovered: true, calls: 2 },
49
+])(
50
+ "keeps $name on $platform (recovered: $recovered)",
51
+ async ({ name, platform, recovered, calls }) => {
52
+ const error = new DOMException("Unavailable", name);
53
+ const getUserMedia = vi.fn().mockRejectedValue(error);
54
+ using _env = setup({
55
+ getUserMedia,
56
+ platform,
57
+ ...(recovered === undefined ? {} : { restartAudioService: async () => recovered }),
58
+ });
59
+ await expect(requestMicrophone("")).rejects.toBe(error);
60
+ expect(getUserMedia).toHaveBeenCalledTimes(calls);
61
+ },
62
+);
63
+
64
+it("keeps the system-default fallback across one Linux audio recovery", async () => {
65
+ const missing = new DOMException("Disconnected", "NotFoundError");
66
+ const stream = {} as MediaStream;
67
+ const getUserMedia = vi
68
+ .fn()
69
+ .mockRejectedValueOnce(missing)
70
+ .mockRejectedValueOnce(missing)
71
+ .mockRejectedValueOnce(missing)
72
+ .mockResolvedValueOnce(stream);
73
+ const restartAudioService = vi.fn().mockResolvedValue(true);
74
+ using _env = setup({ getUserMedia, restartAudioService });
75
+ expect(await requestMicrophone("missing-mic")).toBe(stream);
76
+ expect(getUserMedia.mock.calls).toEqual([
77
+ [{ audio: { deviceId: { exact: "missing-mic" } } }],
78
+ [{ audio: true }],
79
+ [{ audio: { deviceId: { exact: "missing-mic" } } }],
80
+ [{ audio: true }],
81
+ ]);
82
+ expect(restartAudioService).toHaveBeenCalledOnce();
83
+});
apps/web/src/speech/requestMicrophone.ts
new
+38
@@ -0,0 +1,38 @@
1
+export async function requestMicrophone(
2
+ microphoneId: string,
3
+ signal?: AbortSignal,
4
+): Promise<MediaStream> {
5
+ const constraints = { audio: microphoneId ? { deviceId: { exact: microphoneId } } : true };
6
+ const open = async () => {
7
+ let stream: MediaStream;
8
+ try {
9
+ stream = await navigator.mediaDevices.getUserMedia(constraints);
10
+ } catch (error) {
11
+ const name = typeof error === "object" && error !== null && "name" in error ? error.name : "";
12
+ if (!microphoneId || (name !== "OverconstrainedError" && name !== "NotFoundError"))
13
+ throw error;
14
+ signal?.throwIfAborted();
15
+ stream = await navigator.mediaDevices.getUserMedia({ audio: true });
16
+ }
17
+ if (signal?.aborted) {
18
+ stream.getTracks().forEach((track) => track.stop());
19
+ signal.throwIfAborted();
20
+ }
21
+ return stream;
22
+ };
23
+ try {
24
+ return await open();
25
+ } catch (error) {
26
+ const desktop = window.desktopBridge;
27
+ if (
28
+ signal?.aborted ||
29
+ !(error instanceof DOMException) ||
30
+ (error.name !== "NotFoundError" && error.name !== "NotReadableError") ||
31
+ desktop?.getClientPlatform() !== "linux" ||
32
+ !(await desktop.restartAudioService?.().catch(() => false))
33
+ )
34
+ throw error;
35
+ signal?.throwIfAborted();
36
+ return open();
37
+ }
38
+}
packages/contracts/src/ipc.ts
+1
@@ -1060,6 +1060,7 @@ export interface DesktopBridge {
1060
getRelayTelemetryEnabled: () => boolean;
1061
getPathForFile: (file: File) => string;
1062
getClientPlatform: () => string;
1063
+ restartAudioService?: () => Promise<boolean>;
1064
setNotificationBadge: (badge: { count: number; image: string | null }) => Promise<void>;
1065
onNotificationBadgeClear: (listener: () => void) => () => void;
1066
onTrackpadScrollBegin: (listener: () => void) => () => void;