Next.js API Proxy 502 Errors Fixed by Replacing Undici Fetch with Node HTTP Module
Developers using Next.js as a Backend-for-Frontend proxy discovered that long-running ML inference requests — such as audio generation on CPU taking several minutes — were failing with 502 errors due to undici's default header timeout of around 300 seconds. The inference service itself continued running normally, but the Next.js proxy layer was cutting off the connection before a response arrived. Attempts to override undici's timeout settings proved unreliable and environment-dependent, making them an inadequate long-term fix. The team resolved the issue by rewriting the proxy using Node's built-in http/https modules, which offer granular control over timeout behavior. Their solution enforces a 30-second connection timeout to catch unreachable services quickly, while removing any response timeout entirely to allow indefinitely long inference jobs to complete.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in