Skip to content

fix: node fetch timeout - #1217

Open
vachmara wants to merge 36 commits into
googleapis:mainfrom
vachmara:fix/node-fetch-timeout
Open

vachmara wants to merge 36 commits into
googleapis:mainfrom
vachmara:fix/node-fetch-timeout

Conversation

@vachmara

@vachmara vachmara commented Dec 27, 2025 •

Copy link
Copy Markdown

Fix #712

This pull request updates how the undici HTTP client library is managed and integrated in the project, moving it from a direct dependency to a peer dependency, and adds improved Node.js HTTP timeout handling using undici's Agent. It also introduces corresponding test coverage for this new behavior.

Dependency management:

  • Put undici and undici-types to peerDependencies in package.json, and marked them as optional in peerDependenciesMeta. This makes them optional peer dependencies instead of always-installed direct dependencies.

Node.js HTTP request improvements:

  • In src/_api_client.ts, updated the request logic to dynamically import undici and set a custom Agent with timeout options for Node.js environments, improving HTTP timeout handling.

Testing:

  • Added a new unit test in test/unit/api_client_test.ts to verify that the dispatcher with timeouts is set on HTTP requests in Node.js, and imported the required Agent and types from undici for testing.

Shout out to @yulisunny for spotting the issue, I tested this on vercel it's working!

@google-cla

google-cla Bot commented Dec 27, 2025

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@shmishra99

Copy link
Copy Markdown
Contributor

@vachmara, Thank you for raising this PR. I have created an internal bug for the review.

@shmishra99 shmishra99 added the type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. label Jan 20, 2026
Comment thread package.json Resolved
Comment thread package.json Resolved
Comment thread src/_api_client.ts Resolved
@vachmara
vachmara requested a review from hkt74 January 23, 2026 11:04

@hkt74 hkt74 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for resolve the last comments quickly, I did a second review using Gemini

Comment thread src/_api_client.ts Resolved
Comment thread package.json Resolved
@vachmara
vachmara requested a review from hkt74 January 27, 2026 09:29
@vachmara

Copy link
Copy Markdown
Author

Thanks for resolve the last comments quickly, I did a second review using Gemini

Perfect, thank you! Let me know if you need anything else

@hkt74 hkt74 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did more tests locally with your change and triggered the github actions

  1. Need to add 'undici' in rollup.config.mjs - externalDeps
  2. fix the github actions

Comment thread src/_api_client.ts Resolved
@vachmara

Copy link
Copy Markdown
Author

Not sure the coverage CI is related to this change, is everything still okay?

@vachmara
vachmara requested a review from hkt74 January 27, 2026 23:23

@hkt74 hkt74 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the coverage CI related to the change in the package.json file?

/home/runner/work/js-genai/js-genai/dist/src/cross/sentencepiece/sentencepiece_model.pb.js

Comment thread package.json Resolved
Comment thread package.json Resolved
@yulisunny

Copy link
Copy Markdown

Looks to be fixed and released by Google 850f680

@vachmara

Copy link
Copy Markdown
Author

Looks to be fixed and released by Google 850f680

Yes you are right, I'm closing this then. We should probably also close #712

@vachmara vachmara closed this May 17, 2026
@yulisunny

Copy link
Copy Markdown

Actually I don't think she did it right. After removing my own jank patch, I am getting the default 5 mins timeout again instead of the custom timeout I set at 30 mins.

[TypeError: fetch failed] {
  [cause]: InformationalError: HTTP/2: "stream timeout after 300000"
      at ClientHttp2Stream.onTimeout (node:internal/deps/undici/undici:8872:21)
      at ClientHttp2Stream.emit (node:events:509:20)
      at callTimeout (node:internal/http2/core:2565:8)
      at ClientHttp2Stream._onTimeout (node:internal/http2/core:2200:5)
      at listOnTimeout (node:internal/timers:605:17)
      at process.processTimers (node:internal/timers:541:7) {
    code: 'UND_ERR_INFO'
  }
}

@vachmara vachmara reopened this May 22, 2026
@vachmara

Copy link
Copy Markdown
Author

Actually I don't think she did it right. After removing my own jank patch, I am getting the default 5 mins timeout again instead of the custom timeout I set at 30 mins.

[TypeError: fetch failed] {
  [cause]: InformationalError: HTTP/2: "stream timeout after 300000"
      at ClientHttp2Stream.onTimeout (node:internal/deps/undici/undici:8872:21)
      at ClientHttp2Stream.emit (node:events:509:20)
      at callTimeout (node:internal/http2/core:2565:8)
      at ClientHttp2Stream._onTimeout (node:internal/http2/core:2200:5)
      at listOnTimeout (node:internal/timers:605:17)
      at process.processTimers (node:internal/timers:541:7) {
    code: 'UND_ERR_INFO'
  }
}

Yes, I confirm that the error is still here.

@yulisunny

Copy link
Copy Markdown

Actually I don't think she did it right. After removing my own jank patch, I am getting the default 5 mins timeout again instead of the custom timeout I set at 30 mins.

[TypeError: fetch failed] {
  [cause]: InformationalError: HTTP/2: "stream timeout after 300000"
      at ClientHttp2Stream.onTimeout (node:internal/deps/undici/undici:8872:21)
      at ClientHttp2Stream.emit (node:events:509:20)
      at callTimeout (node:internal/http2/core:2565:8)
      at ClientHttp2Stream._onTimeout (node:internal/http2/core:2200:5)
      at listOnTimeout (node:internal/timers:605:17)
      at process.processTimers (node:internal/timers:541:7) {
    code: 'UND_ERR_INFO'
  }
}

Yes, I confirm that the error is still here.

🤦‍♂️ If only she just adopted this PR instead of the global fetch symbol manipulation she was trying to do.

@yulisunny

Copy link
Copy Markdown

8268806 @vachmara You think they finally fixed it, a year after it was first reported, 8 months after you put out your PR that fixes it? 😂🤦‍♂️

@Varun-S10 Varun-S10 assigned Varun-S10 and unassigned shmishra99 Aug 31, 2026
@Varun-S10

Copy link
Copy Markdown

Hi @vachmara, Thank you for your patience. It looks like this branch currently has merge conflicts that need to be resolved. Could you please update the branch to resolve these conflicts? This will allow us to complete our verification.

@Varun-S10 Varun-S10 added the status:awaiting user response issues requiring a response from the user label Aug 31, 2026
@vachmara

Copy link
Copy Markdown
Author

Hi @vachmara, Thank you for your patience. It looks like this branch currently has merge conflicts that need to be resolved. Could you please update the branch to resolve these conflicts? This will allow us to complete our verification.

Hi, on it

@vachmara

Copy link
Copy Markdown
Author

Updated and tested with both Undici versions on Node v24.13.0 using scripts/manual_test_long_timeout.ts (365s delayed headers, 420s SDK timeout):

  • Undici 7.25.0: passed after 365.5s
  • Undici 8.10.0 using Dispatcher1Wrapper: passed after 367.3s
  • Focused tests: 6 specs, 0 failures

Both successfully exceeded Node fetch’s default 300-second timeout. Run with:

npx tsx scripts/manual_test_long_timeout.ts

I’ll also test the generated .tgz package with Undici 8 in our main application to validate the fix in a real integration environment. @Varun-S10

@Varun-S10 Varun-S10 removed the status:awaiting user response issues requiring a response from the user label Aug 31, 2026
@Varun-S10

Copy link
Copy Markdown

Hi @vachmara, Thank you for your contribution. The changes look good, but npx tsc is currently failing with a TypeScript compilation error in test/unit/api_client_test.ts. Could you please look into it ? Please let us know once updated, and the PR will be reviewed.

@Varun-S10 Varun-S10 added the status:awaiting user response issues requiring a response from the user label Aug 31, 2026
@vachmara

Copy link
Copy Markdown
Author

oh thx @Varun-S10 should be fixed now

@Varun-S10 Varun-S10 removed the status:awaiting user response issues requiring a response from the user label Sep 1, 2026
@Varun-S10

Copy link
Copy Markdown

Hi @vachmara, Thank you for your contribution. We have checked the code changes. I will escalate this to the team, and the PR will be reviewed. Thank you for your patience and understanding.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api:gemini-api ready to pull type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

timeout (server timeout) option isn't work.

5 participants