Skip to content

Clear fetch abort timeout #8663

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

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/four-baboons-behave.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions packages/vertexai/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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;
34 changes: 15 additions & 19 deletions packages/vertexai/src/requests/request.ts
Original file line number Diff line number Diff line change
@@ -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';
@@ -116,7 +117,6 @@ export async function constructRequest(
return {
url: url.toString(),
fetchOptions: {
...buildFetchOptions(requestOptions),
method: 'POST',
headers: await getHeaders(url),
body
@@ -134,6 +134,7 @@ export async function makeRequest(
): Promise<Response> {
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 +144,15 @@ export async function makeRequest(
body,
requestOptions
);
// Timeout is 180s by default
const timeoutMillis =
requestOptions?.timeout != null && requestOptions.timeout >= 0
? requestOptions.timeout
: DEFAULT_FETCH_TIMEOUT_MS;
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 +221,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;
}