Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Jetty 12.1 ResourceHandler: how to know if client closed HTTP connection?

Hi,

This is the way TCP is designed: you cannot know if the peer closed the connection unless you read from or write to the socket, and HTTP/1.1 doesn't do anything to circumvent this.

If you know all the response headers before you create your big PDF, XLS, or CSV file, you could try responding with HTTP status 102 regularly until you finally respond with status 200, assuming all your clients do support this.
An alternative is to stream the response: write it bits by bits along your very long processing.

If you can change your API/website, a common workaround to this problem is to make one request to start the generation, then wait for an event (websocket or HTTP long polling) or regularly poll with HTTP GET requests figure out when the generation is done before attempting to download the generated resource with one final request.

Assuming you cannot do any of the above, you're unfortunately left only with Jetty's idle timeout to abort the request not knowing if the peer is still connected or not, or wait until all the data has been generated before attempting to write it and eventually detect that the peer is gone.

--
Ludovic




On Mon, Jul 13, 2026 at 12:09 PM Michał Niklas via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
Hi,

My application uses Jetty 12.1 and extends ResourceHandler to serve some
types of data from database.

Unfortunately some queries are very long: they create big PDF, XLS, or
CSV files and sometimes client closes browser, or Kubernetes monitor
resends query. I wanted to know if the client is still connected. I
tried technique like:

public boolean handle(Request request, Response response, Callback
callback){
     request.addFailureListener(failure -> {
         log.error("Failure during serving " + request.getHttpURI() + "
" + failure.toString() + " " + getState());
     });
     ...
}

My test query last few minutes so I can close browser during this
period. Every long lasting query after 30 seconds writes to log:

... java.util.concurrent.TimeoutException: Idle timeout expired:
30000/30000 ms STARTED

The query is completed regardless if I close browser (HTTP 1.1 by Chrome
or curl) or wait for whole response. If I close browser or Ctrl-C for
curl, there is no info that client closed connection.

I would like to know if client closed connection to simply stop querying
database and stop creating output. How can I do it?

PS I have asked this question also on StackOverflow:
https://stackoverflow.com/questions/79979650/jetty-12-1-resourcehandler-how-to-know-if-client-closed-http-connection


--
Best regards,
Michal Niklas
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users

Back to the top