Waiting for Long Requests with Bun.serve idleTimeout
An API built with Bun.serve cuts off requests that take a long time to respond, unless you configure it otherwise. According to the official documentation, a connection is closed when there is no sending or receiving for 10 seconds. In my test, the request was cut off at 12 seconds. If you raise the server-wide idleTimeout, or extend the wait only for slow routes with server.timeout(req, seconds), the server can wait until the work finishes and then send the response.
This applies to APIs where a single request can take tens of seconds, such as report or image generation or slow external API calls. This article covers how to fix the case where curl fails with "Empty reply from server" or fetch fails with ECONNRESET. I measured the specified seconds and the actual seconds until the connection was cut on Ubuntu 24.04 with Bun 1.4.4.
The test environment was Ubuntu 24.04 (x86_64) with Bun 1.4.4. I used Bun.sleep() to stand in for long-running work, and sent requests with curl and Bun's fetch. I did not test requests from a browser, HTTPS or HTTP/2, or setups with a reverse proxy such as nginx in front. The official documentation links point to the files at the tag bun-v1.4.4 on GitHub, which is the version I used.
Without a setting, requests longer than 10 seconds are cut off
According to the official documentation on idleTimeout, Bun.serve closes a connection that has no sending or receiving for 10 seconds. A request whose handler is still running and has not written any bytes of the response also enters this no-activity state.
To confirm this, I created a route /slow/:sec that waits for the given number of seconds before responding. If the connection is cut while waiting, req.signal is aborted, so the log also records that time.
// Return after a long process (simulated with Bun.sleep)
async function slowJob(req: Request, label: string, ms: number) {
const start = performance.now();
req.signal.addEventListener("abort", () => {
console.log(`${label}: ${((performance.now() - start) / 1000).toFixed(1)}秒で req.signal が abort`);
});
await Bun.sleep(ms);
console.log(`${label}: ${((performance.now() - start) / 1000).toFixed(1)}秒で処理が終わった(aborted=${req.signal.aborted})`);
return new Response(`${label}: ${ms / 1000}秒の処理が終わりました\n`);
}
Bun.serve({
port: 3000,
routes: {
"/slow/:sec": (req) => slowJob(req, "slow", Number(req.params.sec) * 1000),
},
});
I started the server without idleTimeout and called the 5-second and 15-second jobs with curl. The 5-second job returned, but the 15-second job was cut off at 12 seconds.
$ curl -sS localhost:3000/slow/5
slow: 5秒の処理が終わりました
HTTP 200、5.002704秒
(curl の終了コード 0)
$ curl -sS localhost:3000/slow/15
curl: (52) Empty reply from server
HTTP 000、12.003477秒
(curl の終了コード 52)
The server log printed one warning line.
slow: 5.0秒で処理が終わった(aborted=false)
warn: Bun.serve() timed out a request after 10 seconds. Pass `idleTimeout` to configure.
slow: 12.0秒で req.signal が abort
slow: 15.0秒で処理が終わった(aborted=true)
As the last line shows, Bun.sleep() kept running after the connection was closed, and it finished at 15 seconds. The response it tried to send never reached anyone.
When I called the same 15-second job from Bun's fetch, it threw an exception at 12.0 seconds.
12.0秒で例外: TypeError code=ECONNRESET message=ECONNRESET: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()
Raise the server-wide idleTimeout
If you pass idleTimeout (in seconds) to Bun.serve, the wait applies to every request.
Bun.serve({
port: 3000,
idleTimeout: 30, // Wait up to 30 seconds with no sending or receiving (default is 10)
routes: {
"/slow/:sec": (req) => slowJob(req, "slow", Number(req.params.sec) * 1000),
},
});
After restarting with idleTimeout: 30, the 15-second job returned in full.
$ curl -sS localhost:3000/slow/15
slow: 15秒の処理が終わりました
HTTP 200、15.002776秒
(curl の終了コード 0)
According to the documentation, the maximum value is 255 seconds, and passing 0 disables the timeout. With idleTimeout: 0, a 40-second job returned without being cut off. A value above the limit or a decimal caused Bun.serve() to throw, and the server did not start.
$ IDLE=255 bun server.ts
listening idleTimeout=255
$ IDLE=256 bun server.ts
TypeError: Bun.serve expects idleTimeout to be 255 or less
$ IDLE=300 bun server.ts
TypeError: Bun.serve expects idleTimeout to be 255 or less
$ IDLE=1.5 bun server.ts
TypeError: Bun.serve expects idleTimeout to be an integer
The test server.ts passes the value of the environment variable IDLE to idleTimeout.
The cut comes a little after the specified seconds
With the default 10 seconds, the connection was actually cut at 12 seconds. So I also set idleTimeout to 3, 5, and 30, called a 100-second job, and measured the seconds until the cut.

In these tests, every cut came later than the specified value, at a multiple of 4 seconds. With the default 10 seconds, I sent requests at offsets of 0, 1.3, and 2.7 seconds, and all three were cut at 12.0 seconds. Across these tests with idleTimeout set between 3 and 30 seconds, no connection was ever cut earlier than the specified value.
The warning in the server log printed only once, when the first cut happened on a server instance. Later cuts on the same instance did not print it. The absence of the warning does not prove that a connection was not cut.
Extend only the long route with server.timeout
If you raise the server-wide idleTimeout, the same limit applies to routes that finish quickly. If you know which routes are slow, you can use server.timeout(req, seconds) to change the wait for just that request.
Bun.serve({
port: 3000, // Do not set idleTimeout (keep the default 10 seconds)
routes: {
"/slow/:sec": (req) => slowJob(req, "slow", Number(req.params.sec) * 1000),
// Set the wait time to 60 seconds for this request only
"/report/:sec": (req, server) => {
server.timeout(req, 60);
return slowJob(req, "report", Number(req.params.sec) * 1000);
},
},
});
/report/15 returned the full 15-second job. On the same server, /slow/15 was cut at 12 seconds, as in the first test.
$ curl -sS localhost:3000/report/15
report: 15秒の処理が終わりました
HTTP 200、15.002552秒
(curl の終了コード 0)
server.timeout() can also shorten the wait. A request with 3 seconds was cut at 4 seconds, the same as with idleTimeout: 3. A request with 0 returned a 20-second job in full.
The warning text printed in this case still said "after 10 seconds", not the value set by server.timeout(). Do not decide which setting caused a cut from the seconds in the log alone.
I also tried a value above 255 seconds. I used a test route /custom/:t/:sec that calls server.timeout(req, t) and then waits sec seconds. I sent a request with t set to 300 and sec set to 280.
$ curl -sS localhost:3000/custom/300/280
curl: (52) Empty reply from server
HTTP 000、256.073752秒
(curl の終了コード 52)
Passing a value above 255 seconds to server.timeout(req, 300) did not throw an exception. However, the 280-second job was cut at 256 seconds. If you need to wait longer than 255 seconds, pass 0 to disable the timeout.
When the connection is cut, stop the work inside too
As shown earlier, the work inside the handler does not stop when the connection is closed. If you also want to stop that work, use req.signal.
The following /proxy/:sec route calls another slow service (here, /backend/:sec on the same server) with fetch and returns its result. If you pass signal: req.signal to fetch, the inner fetch is aborted when the connection to your server is closed.
"/proxy/:sec": async (req, server) => {
const start = performance.now();
try {
const res = await fetch(`http://localhost:${server.port}/backend/${req.params.sec}`, {
signal: req.signal,
});
return new Response(await res.text());
} catch (e) {
console.log(`proxy: ${((performance.now() - start) / 1000).toFixed(1)}秒で fetch を中止(${(e as Error).name})`);
throw e;
}
},
With idleTimeout left at the default, I sent curl a request to /proxy/20, which calls /backend/20 (a 20-second job). On the /backend/:sec side, I set server.timeout(req, 0) to disable the timeout and logged the time when req.signal was aborted.
$ curl -sS localhost:3000/proxy/20
curl: (52) Empty reply from server
HTTP 000、12.004315秒
(curl の終了コード 52)
In the server log, the fetch in /proxy was aborted with AbortError at 12.0 seconds, and req.signal was also aborted in the called /backend route.
warn: Bun.serve() timed out a request after 10 seconds. Pass `idleTimeout` to configure.
backend: 12.0秒で req.signal が abort
proxy: 12.0秒で fetch を中止(AbortError)
For work that you write yourself, such as a loop, check req.signal.aborted along the way and exit when it is true.
Streaming responses (such as Server-Sent Events) are also cut when the data stops for longer than idleTimeout (official documentation note). For how to handle that case, including how to send heartbeats, see my article on sending Server-Sent Events with Bun.serve.





