Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A StreamingResponseBody export fails after 30 seconds with an AsyncRequestTimeoutException, even though the code is still writing. What is happening and how do you fix it?

StreamingResponseBody runs as asynchronous request processing, and async requests have a total timeout that counts from the start of the request, not from the last byte written. When Spring MVC has no timeout configured, the servlet container's default applies, which is 30 seconds in Tomcat, so the export is cut off at 30 seconds however busy it is. We fix it by setting spring.mvc.async.request-timeout (or configuring it in WebMvcConfigurer), checking the load balancer and proxy idle timeouts too, and moving very long exports to a background job with a download link.

Why this question is asked

This bug is confusing because the code looks healthy: the log shows rows being written, the database is fine, and yet every large export dies at exactly the same moment. The exact number is the clue. Interviewers like this question because solving it requires knowing that StreamingResponseBody is async under the hood, where its timeout comes from, and which other timeouts sit between us and the user. In this answer, we will recap async request processing, reproduce the failure, find where the 30 seconds come from, fix it in Spring, check the proxies and load balancers, and decide when streaming is the wrong tool.

1A Quick Recap of Async Request Processing

In a classic Spring MVC request, one servlet container thread runs the controller and writes the response, and the request ends when the method returns. Some return types change that. When a controller returns Callable, DeferredResult, SseEmitter or StreamingResponseBody, Spring calls request.startAsync(), a Servlet API feature that keeps the response open after the container thread leaves. The work then continues on another thread, and the request ends when that work completes.

Every async request has a timeout, set on the AsyncContext. It is a deadline for the whole async phase: if the work has not completed when it expires, the container fires a timeout event, Spring raises AsyncRequestTimeoutException, and the request is ended. Notice the word whole. It is not an idle timeout that resets when we write bytes.

2Reproducing the Failure

Here is an export that writes steadily for about two minutes.

Java
@GetMapping("/export")
StreamingResponseBody export() {
return out -> {
for (int i = 0; i < 120_000; i++) {
out.write(("row " + i + "\n").getBytes()); // writing all the time
if (i % 1000 == 0) Thread.sleep(1000); // simulates a slow query: about 2 minutes
}
};
}
// at about 00:00:30:
// WARN Async request timed out
// org.springframework.web.context.request.async.AsyncRequestTimeoutException
// client: file ends after ~30,000 rows, or connection closed mid-download
Step throughWhy writing does not prevent the timeout
ClientTomcatSpring MVCExport thread
GET /export
dispatch to controller
startAsync, timeout not set: container default
run writeTo(out)
rows 1..30,000 streamed
t = 30 s: async timeout event
AsyncRequestTimeoutException
response ended, file truncated

8 messages in order. Play them, or step through them one at a time.

The writes do not matter; at 30 seconds the request is timed out. Because the status 200 and the headers were already sent, Spring cannot change the response to an error status, so the client usually just receives a truncated file. That is worse than an error, because a user may not notice that half the rows are missing.

3Where the 30 Seconds Come From

Spring MVC uses its own configured async timeout if there is one. If there is none, it does not set a timeout on the AsyncContext and the servlet container's default applies. In Tomcat, that default is 30 seconds. So the export did not fail because of anything in our code; it failed because a value nobody chose was used.

When debugging, the exact duration is the best evidence. A failure at 30 seconds points to the async default. A failure at 60 seconds often points to a load balancer idle timeout, and one at a round number we configured somewhere points to that setting. Checking the timestamps of the request start and the exception, and the stack trace mentioning AsyncRequestTimeoutException, tells us which layer acted.

4Fixing It in Spring MVC

The direct fix is to set an explicit async timeout that fits the longest export we are willing to serve synchronously. We can do it with a property or in code.

Java
# application.properties
spring.mvc.async.request-timeout=10m
// or in configuration code
@Configuration
class AsyncConfig implements WebMvcConfigurer {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer c) {
c.setDefaultTimeout(Duration.ofMinutes(10).toMillis()); // 600000 ms
}
}

Some async return types also accept a per-request timeout: WebAsyncTask takes one in its constructor, as do DeferredResult and SseEmitter. A StreamingResponseBody uses the configured default, so for exports we set a sensible global value or give the export endpoints their own configuration. We should not set the timeout to unlimited; a bounded value, such as 10 minutes, still protects us from exports that hang forever holding a database connection.

It also helps to register a timeout handler that logs which export was cut off, and to make sure the client can tell a complete file from a truncated one, for example by writing a final trailer line or by publishing the row count.

5Checking the Load Balancer and Proxies

After the Spring fix, the next layers can still close the connection. Load balancers and reverse proxies usually have an idle timeout: if no bytes pass for that long, they close the connection. A cloud load balancer may default to 60 seconds, and nginx's proxy_read_timeout defaults to 60 seconds too; check the actual values of your infrastructure. An export that runs a 90-second sorting query before writing its first row sends nothing during that time and is cut off by the proxy even though Spring's timeout is now 10 minutes.

Two things help. First, start writing immediately and flush regularly, for example the header line right away and a flush every 1,000 rows, so the connection is never idle. Second, check that the proxy does not buffer the whole response before forwarding it: with buffering on, the proxy may hold the bytes and the client sees nothing until the end. For nginx, the response header X-Accel-Buffering: no or proxy_buffering off for the export location fixes that.

6When to Stop Streaming and Use a Job

Raising timeouts works for exports that take a few minutes. It does not scale to exports of 30 minutes or more: every running export holds a connection and a thread, a deploy kills them all, and the user must keep the browser tab open. For those, the right design is a background job. The client calls POST /exports, receives 202 Accepted with a job id, a worker writes the file to object storage, and the client is notified or polls until a signed download link is ready. The download itself is then a plain file transfer that object storage handles well, with no timeouts in our application at all.

Step throughChoosing the fix
  1. Fails at exactly 30 s?Tomcat's default async timeout
    YesSet spring.mvc.async.request-timeoutNoCheck other layers
  2. Fails at the LB idle time?e.g. 60 s with no bytes sent
    YesWrite early, flush often, disable bufferingNoCheck client timeouts
  3. Export longer than minutes?
    YesBackground job + download linkNoKeep streaming

3 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Why can't Spring return a 503 after the timeout?

Because the response is already committed. The status line and headers were sent with the first bytes of the CSV, and HTTP has no way to change the status afterwards. Spring's default handling turns AsyncRequestTimeoutException into 503 only when the response has not been committed. For a streamed export, the connection is closed and the client gets a truncated body.

Would virtual threads fix this problem?

No. Virtual threads change which threads run the work and make blocking cheaper, but the async timeout is a deadline on the request, not a thread limit. With spring.threads.virtual.enabled the export still fails at 30 seconds unless the timeout is configured.

How do you test that the timeout is configured correctly?

Write an integration test that starts the application with a short timeout, say 2 seconds, calls an export that takes 5, and asserts on the outcome, then the reverse with a longer timeout. It is also worth asserting the configured value from the bean so a refactoring cannot silently drop the property.

8Conclusion

In this answer, we saw that a StreamingResponseBody export is async request processing with a total deadline, not an idle timeout, so writing does not keep it alive. With no Spring MVC timeout configured, Tomcat's 30-second default ends the request and the client gets a truncated file. We fixed it with spring.mvc.async.request-timeout or configureAsyncSupport, checked load balancer and proxy idle timeouts and buffering, and moved very long exports to background jobs with a download link.

What to say in the interviewAbout 80 seconds aloud

Open with

The key fact is that StreamingResponseBody is async request processing. The controller returns, Spring calls startAsync, and writeTo runs on another thread, and the async context has a timeout for the whole async phase.

Then, if they want more

It is not an idle timeout, so writing bytes does not reset it. If spring.mvc.async.request-timeout is not set, Spring leaves the container default in place, and Tomcat's default is 30 seconds, which matches the symptom exactly. When it expires, Spring raises AsyncRequestTimeoutException, and since the headers are already sent, the client usually just gets a truncated file.

The fix is to set a deliberate value, for example ten minutes, with the property or with configureAsyncSupport in a WebMvcConfigurer; types like WebAsyncTask, DeferredResult and SseEmitter can also take a per-request timeout. I would keep it bounded, not infinite.

Then I would check the next layers: load balancers and proxies have idle timeouts, often 60 seconds, so I make sure the export writes early and flushes regularly, and that the proxy is not buffering the response.

And for exports that run for many minutes, I would stop streaming entirely and generate the file in a background job, then give the user a signed download link.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A StreamingResponseBody export fails after 30 seconds with an...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/c3e3785fae/