Skip to content

Commit 9faf34d

Browse files
committed
merge: wait for auth before starting realtime timeout (#119131)
2 parents b1f2aae + a7cf1ba commit 9faf34d

5 files changed

Lines changed: 88 additions & 12 deletions

File tree

‎extensions/openai/realtime-voice-provider.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,9 @@ class OpenAIRealtimeVoiceBridge implements RealtimeVoiceBridge {
787787
attempt.resolve();
788788
return;
789789
}
790+
// Auth preparation owns its own timeout. Start the socket deadline only
791+
// after connection parameters are available.
792+
attempt.startTimeout();
790793
const url = resolvedConnection.url;
791794
this.connectionUrl = resolvedConnection.url;
792795
const debugProxy = resolveDebugProxySettings();

‎extensions/xai/realtime-voice-bridge.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ export class XaiRealtimeVoiceBridge extends XaiRealtimeVoiceEvents implements Re
160160
attempt.resolve();
161161
return;
162162
}
163+
// Credential refresh has its own bounded lifecycle. Arm the socket timeout
164+
// only once valid connection parameters are ready.
165+
attempt.startTimeout();
163166
const { url, headers } = resolvedConnection;
164167
this.connectionUrl = url;
165168
const proxyAgent = createDebugProxyWebSocketAgent(resolveDebugProxySettings());

‎extensions/xai/realtime-voice-provider.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,60 @@ describe("buildXaiRealtimeVoiceProvider", () => {
286286
await waitForRealtimeState(() => expect(FakeWebSocket.instances).toHaveLength(0));
287287
});
288288

289+
it("starts the socket timeout after async credential resolution", async () => {
290+
vi.useFakeTimers();
291+
let resolveCredentials: ((value: { apiKey: string | undefined }) => void) | undefined;
292+
resolveApiKeyForProviderMock.mockImplementation(
293+
() =>
294+
new Promise<{ apiKey: string | undefined }>((resolve) => {
295+
resolveCredentials = resolve;
296+
}),
297+
);
298+
const bridge = createTestBridge({
299+
cfg: {} as never,
300+
providerConfig: {},
301+
});
302+
303+
const connecting = bridge.connect();
304+
await vi.advanceTimersByTimeAsync(0);
305+
expect(resolveApiKeyForProviderMock).toHaveBeenCalledOnce();
306+
307+
await vi.advanceTimersByTimeAsync(10_000);
308+
expect(FakeWebSocket.instances).toHaveLength(0);
309+
310+
resolveCredentials?.({ apiKey: "xai-oauth" }); // pragma: allowlist secret
311+
await vi.advanceTimersByTimeAsync(0);
312+
const socket = requireSocket();
313+
socket.open();
314+
socket.emitServer({ type: "session.updated" });
315+
await connecting;
316+
317+
const options = socket.args[1] as { headers?: Record<string, string> } | undefined;
318+
expect(options?.headers?.Authorization).toBe("Bearer xai-oauth");
319+
bridge.close();
320+
});
321+
322+
it("retains the socket timeout after async credential resolution", async () => {
323+
vi.useFakeTimers();
324+
resolveApiKeyForProviderMock.mockResolvedValue({ apiKey: "xai-oauth" });
325+
const bridge = createTestBridge({
326+
cfg: {} as never,
327+
providerConfig: {},
328+
});
329+
330+
const connecting = bridge.connect();
331+
await vi.advanceTimersByTimeAsync(0);
332+
const socket = requireSocket();
333+
const timeoutAssertion = expect(connecting).rejects.toThrow(
334+
"xAI realtime voice connection timeout",
335+
);
336+
337+
await vi.advanceTimersByTimeAsync(10_000);
338+
await timeoutAssertion;
339+
expect(socket.terminated).toBe(true);
340+
expect(bridge.isConnected()).toBe(false);
341+
});
342+
289343
it("starts a replacement immediately after canceling pending credentials", async () => {
290344
let resolveFirstCredentials: ((value: { apiKey: string | undefined }) => void) | undefined;
291345
resolveApiKeyForProviderMock

‎src/plugin-sdk/realtime-voice.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe("RealtimeVoiceSessionLifecycle", () => {
164164
expect(openTransport).not.toHaveBeenCalled();
165165
});
166166

167-
it("settles connect attempts once and releases timeout and abort ownership", async () => {
167+
it("starts connect timeouts explicitly and releases timeout and abort ownership", async () => {
168168
vi.useFakeTimers();
169169
try {
170170
const lifecycle = new RealtimeVoiceSessionLifecycle("Test");
@@ -179,6 +179,7 @@ describe("RealtimeVoiceSessionLifecycle", () => {
179179
timeoutMs: 10,
180180
});
181181

182+
attempt.startTimeout();
182183
attempt.resolve(true);
183184
attempt.reject(new Error("late failure"));
184185
await expect(attempt.promise).resolves.toBeUndefined();
@@ -201,6 +202,11 @@ describe("RealtimeVoiceSessionLifecycle", () => {
201202
});
202203
const rejected = expect(timeoutAttempt.promise).rejects.toThrow("timed out");
203204
await vi.advanceTimersByTimeAsync(10);
205+
expect(onTimeout).not.toHaveBeenCalled();
206+
207+
timeoutAttempt.startTimeout();
208+
timeoutAttempt.startTimeout();
209+
await vi.advanceTimersByTimeAsync(10);
204210
await rejected;
205211

206212
expect(timeoutAttempt.startupFailed).toBe(true);

‎src/talk/realtime-session-lifecycle.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type RealtimeVoiceConnectAttempt = {
8888
reject: (error: Error) => void;
8989
rejectStartup: (error: Error) => boolean;
9090
resolve: (providerReady?: boolean) => void;
91+
startTimeout: () => void;
9192
};
9293

9394
type RealtimeVoiceConnectAttemptOptions = {
@@ -174,8 +175,11 @@ export class RealtimeVoiceSessionLifecycle {
174175
rejectPromise = reject;
175176
});
176177
let removeAbortListener = () => {};
178+
let timeout: ReturnType<typeof setTimeout> | undefined;
177179
const cleanup = () => {
178-
clearTimeout(timeout);
180+
if (timeout) {
181+
clearTimeout(timeout);
182+
}
179183
removeAbortListener();
180184
};
181185
const resolve = (providerReady = false) => {
@@ -203,17 +207,22 @@ export class RealtimeVoiceSessionLifecycle {
203207
reject(error);
204208
return true;
205209
};
206-
const timeout = setTimeout(() => {
207-
if (
208-
this.isCurrent(options.connection) &&
209-
!ready &&
210-
this.terminalOutcome(options.connection) !== "completed"
211-
) {
212-
startupFailed = true;
213-
options.onTimeout();
214-
reject(options.timeoutError());
210+
const startTimeout = () => {
211+
if (settled || timeout) {
212+
return;
215213
}
216-
}, options.timeoutMs);
214+
timeout = setTimeout(() => {
215+
if (
216+
this.isCurrent(options.connection) &&
217+
!ready &&
218+
this.terminalOutcome(options.connection) !== "completed"
219+
) {
220+
startupFailed = true;
221+
options.onTimeout();
222+
reject(options.timeoutError());
223+
}
224+
}, options.timeoutMs);
225+
};
217226
const onAbort = () => {
218227
const outcome = this.terminalOutcome(options.connection);
219228
options.onAbort(outcome);
@@ -243,6 +252,7 @@ export class RealtimeVoiceSessionLifecycle {
243252
reject,
244253
rejectStartup,
245254
resolve,
255+
startTimeout,
246256
};
247257
}
248258

0 commit comments

Comments
 (0)