Digiaru

Digiaru started this conversation 1 week ago.

Undici fetch fails with HeadersTimeoutError even after customizing agent durations

I configured a custom Undici Agent with very large timeouts (e.g. headersTimeout: 3,000,000), but long-running fetch calls still fail after several minutes with TypeError: fetch failed caused by HeadersTimeoutError.

Kar

Posted 1 week ago

Undici imposes multiple timeout stages—connect, headers parsing, and body. Even if you increase one timeout, other stages (e.g. headers parsing) may still hit defaults and cancel the request, resulting in errors like UND_ERR_HEADERS_TIMEOUT GitHub+7GitHub+7GitHub+7. 🛠️ Fix Options: • Set all relevant timeout values when creating the agent: js Copy code const agent = new Agent({ connect: { timeout: 20000 }, headersTimeout: 600_000, bodyTimeout: 600_000 }); fetch(url, { dispatcher: agent }); • If using Node’s fetch, ensure you use Undici agent and not default dispatcher.