views:

331

answers:

4

In a fictitious web application ...

  1. The user clicks a link
  2. The server starts to prepare the response, but it takes several seconds
  3. The user cancels the page load

What happens to the request? Does the server continue to prepare the response? Does the response arrive to the browser?

A: 

I would think that the actual TCP connection is closed by the browser and therefore the web-server will be unable to send data, and unless it is specifically programmed to detect broken connections whilst preparing the data, then the page will be fully processed even if the user cancels.

I have little knowledge on these things though, but that would be my guess.

dreamlax
+4  A: 

The server will continue to prepare the response. When it tries to send the response to the client, it'll fail. When this actually happens will probably depend on the actual application server implementation, whether the response is buffered etc.

In Java EE app servers (Tomcat and WebLogic, probably others as well), you'll get the following exception:

java.net.SocketException: Connection reset by peer: socket write error
Jack Leow
A: 

PHP understands three states of connection: NORMAL, ABORTED and TIMEOUT. You can change PHP's policy on ABORTED connections (by default, the script is terminated) with the ignore_user_abort() function. From the notes section:

"PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client."

Note that if your server's output is buffered, a send may not occur immediately.

See PHP's page on connection handling for more details.

Artelius
A: 

If the connection was broken before the request was completely sent to the server, the response processing won't occur.

If the request was sent completely, this triggers the server-side processing, and the response generation will continue despite the broken connection.

In ASP.NET, you can detect this by using Response.IsClientConnected to halt the processing if the client is not connected anymore, saving CPU time and returning the thread immediately to the thread pool.