From e9ae353e380023f982dcbbd36cece3ddb0595928 Mon Sep 17 00:00:00 2001 From: Daniel La Rocque Date: Tue, 3 Dec 2024 13:44:47 -0500 Subject: [PATCH 1/3] Clear fetch abort timeout --- .changeset/four-baboons-behave.md | 5 ++++ packages/vertexai/src/requests/request.ts | 33 ++++++++++------------- 2 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 .changeset/four-baboons-behave.md diff --git a/.changeset/four-baboons-behave.md b/.changeset/four-baboons-behave.md new file mode 100644 index 00000000000..63c51a44c7e --- /dev/null +++ b/.changeset/four-baboons-behave.md @@ -0,0 +1,5 @@ +--- +'@firebase/vertexai': patch +--- + +Clear fetch timeout after request completion. Fixes an issue that caused Node scripts to hang due to a pending timeout. diff --git a/packages/vertexai/src/requests/request.ts b/packages/vertexai/src/requests/request.ts index 411bad859f0..98c3d7273dd 100644 --- a/packages/vertexai/src/requests/request.ts +++ b/packages/vertexai/src/requests/request.ts @@ -116,7 +116,6 @@ export async function constructRequest( return { url: url.toString(), fetchOptions: { - ...buildFetchOptions(requestOptions), method: 'POST', headers: await getHeaders(url), body @@ -134,6 +133,7 @@ export async function makeRequest( ): Promise { const url = new RequestUrl(model, task, apiSettings, stream, requestOptions); let response; + let fetchTimeoutId: string | number | NodeJS.Timeout | undefined; try { const request = await constructRequest( model, @@ -143,6 +143,15 @@ export async function makeRequest( body, requestOptions ); + // Timeout is 180s by default + const timeoutMillis = + requestOptions?.timeout !== undefined + ? requestOptions.timeout + : 180 * 1000; + const abortController = new AbortController(); + fetchTimeoutId = setTimeout(() => abortController.abort(), timeoutMillis); + request.fetchOptions.signal = abortController.signal; + response = await fetch(request.url, request.fetchOptions); if (!response.ok) { let message = ''; @@ -211,24 +220,10 @@ export async function makeRequest( } throw err; + } finally { + if (fetchTimeoutId) { + clearTimeout(fetchTimeoutId); + } } return response; } - -/** - * Generates the request options to be passed to the fetch API. - * @param requestOptions - The user-defined request options. - * @returns The generated request options. - */ -function buildFetchOptions(requestOptions?: RequestOptions): RequestInit { - const fetchOptions = {} as RequestInit; - let timeoutMillis = 180 * 1000; // default: 180 s - if (requestOptions?.timeout && requestOptions?.timeout >= 0) { - timeoutMillis = requestOptions.timeout; - } - const abortController = new AbortController(); - const signal = abortController.signal; - setTimeout(() => abortController.abort(), timeoutMillis); - fetchOptions.signal = signal; - return fetchOptions; -} From f12e7c8d0415bb69b29d74beda66ab82986e0e6b Mon Sep 17 00:00:00 2001 From: Daniel La Rocque Date: Tue, 3 Dec 2024 15:28:12 -0500 Subject: [PATCH 2/3] Add timeout constant and bring back minimum timeout value --- packages/vertexai/src/constants.ts | 2 ++ packages/vertexai/src/requests/request.ts | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/vertexai/src/constants.ts b/packages/vertexai/src/constants.ts index 3ff894f354b..357e6c4e77c 100644 --- a/packages/vertexai/src/constants.ts +++ b/packages/vertexai/src/constants.ts @@ -28,3 +28,5 @@ export const DEFAULT_API_VERSION = 'v1beta'; export const PACKAGE_VERSION = version; export const LANGUAGE_TAG = 'gl-js'; + +export const DEFAULT_FETCH_TIMEOUT_MS = 180 * 1000; diff --git a/packages/vertexai/src/requests/request.ts b/packages/vertexai/src/requests/request.ts index 98c3d7273dd..28b7a2d64b4 100644 --- a/packages/vertexai/src/requests/request.ts +++ b/packages/vertexai/src/requests/request.ts @@ -21,6 +21,7 @@ import { ApiSettings } from '../types/internal'; import { DEFAULT_API_VERSION, DEFAULT_BASE_URL, + DEFAULT_FETCH_TIMEOUT_MS, LANGUAGE_TAG, PACKAGE_VERSION } from '../constants'; @@ -145,9 +146,9 @@ export async function makeRequest( ); // Timeout is 180s by default const timeoutMillis = - requestOptions?.timeout !== undefined + requestOptions?.timeout !== undefined && requestOptions.timeout >= 0 ? requestOptions.timeout - : 180 * 1000; + : DEFAULT_FETCH_TIMEOUT_MS; const abortController = new AbortController(); fetchTimeoutId = setTimeout(() => abortController.abort(), timeoutMillis); request.fetchOptions.signal = abortController.signal; From 02b9fc3053aeafed4cebfdd09c7530f93b2e8893 Mon Sep 17 00:00:00 2001 From: Daniel La Rocque Date: Wed, 4 Dec 2024 12:35:39 -0500 Subject: [PATCH 3/3] Make timeout condition check if null or undefined --- packages/vertexai/src/requests/request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vertexai/src/requests/request.ts b/packages/vertexai/src/requests/request.ts index 28b7a2d64b4..f81b40635e3 100644 --- a/packages/vertexai/src/requests/request.ts +++ b/packages/vertexai/src/requests/request.ts @@ -146,7 +146,7 @@ export async function makeRequest( ); // Timeout is 180s by default const timeoutMillis = - requestOptions?.timeout !== undefined && requestOptions.timeout >= 0 + requestOptions?.timeout != null && requestOptions.timeout >= 0 ? requestOptions.timeout : DEFAULT_FETCH_TIMEOUT_MS; const abortController = new AbortController();