15

Given an async controller:

public class MyController : AsyncController 
{
    [NoAsyncTimeout]
    public void MyActionAsync() { ... }

    public void MyActionCompleted() { ... }
}

Assume MyActionAsync kicks off a process that takes several minutes. If the user now goes to the MyAction action, the browser will wait with the connection open. If the user closes his browser, the connection is closed. Is it possible to detect when that happens on the server (preferably inside the controller)? If so, how? I've tried overriding OnException but that never fires in this scenario.

Note: I do appreciate the helpful answers below, but the key aspect of this question is that I'm using an AsyncController. This means that the HTTP requests are still open (they are long-lived like COMET or BOSH) which means it's a live socket connection. Why can't the server be notified when this live connection is terminated (i.e. "connection reset by peer", the TCP RST packet)?

4 Answers 4

28

I realise this question is old, but it turned up frequently in my search for the same answer. The details below only apply to .Net 4.5

HttpContext.Response.ClientDisconnectedToken is what you want. That will give you a CancellationToken you can pass to your async/await calls.

public async Task<ActionResult> Index()
{
    //The Connected Client 'manages' this token. 
    //HttpContext.Response.ClientDisconnectedToken.IsCancellationRequested will be set to true if the client disconnects
    try
    {
        using (var client = new System.Net.Http.HttpClient())
        {
            var url = "http://google.com";
            var html = await client.GetAsync(url,  HttpContext.Response.ClientDisconnectedToken);
        }
    }
    catch (TaskCanceledException e)
    {
        //The Client has gone
        //you can handle this and the request will keep on being processed, but no one is there to see the resonse
    }
    return View();
}

You can test the snippet above by putting a breakpoint at the start of the function then closing your browser window.


And another snippet, not directly related to your question but useful all the same...

You can also put a hard limit on the amount of time an action can execute for by using the AsyncTimeout attribute. To use this use add an additional parameter of type CancellationToken. This token will allow ASP.Net to time-out the request if execution takes too long.

[AsyncTimeout(500)] //500ms
public async Task<ActionResult> Index(CancellationToken cancel)
{
    //ASP.Net manages the cancel token.
    //cancel.IsCancellationRequested will be set to true after 500ms
    try
    {
        using (var client = new System.Net.Http.HttpClient())
        {
            var url = "http://google.com";
            var html = await client.GetAsync(url, cancel);
        }
    }
    catch (TaskCanceledException e)
    {
        //ASP.Net has killed the request
        //Yellow Screen Of Death with System.TimeoutException
        //the return View() below wont render
    }
    return View();
}

You can test this one by putting a breakpoint at the start of the function (thus making the request take more than 500ms when the breakpoint is hit) then letting it run out.

2
  • For me, I got the cancellation token through System.Web.HttpContext.Current.Response.ClientDisconnectedToken
    – Tony
    Jul 17, 2014 at 23:20
  • 1
    I wonder, how to make it work with OWIN self-hosted appliation?
    – greatvovan
    Jul 5, 2017 at 18:53
7

Does not Response.IsClientConnected work fairly well for this? I have just now tried out to in my case cancel large file uploads. By that I mean if a client abort their (in my case Ajax) requests I can see that in my Action. I am not saying it is 100% accurate but my small scale testing shows that the client browser aborts the request, and that the Action gets the correct response from IsClientConnected.

1
  • 1
    Yes, this is the best answer here. IsClientConnected will be false in the middle of an async request if the client closes their browser.
    – eoleary
    Apr 18, 2013 at 0:24
5

It's just as @Darin says. HTTP is a stateless protocol which means that there are no way (by using HTTP) to detect if the client is still there or not. HTTP 1.0 closes the socket after each request, while HTTP/1.1 can keep it open for a while (a keep alive timeout can be set as a header). That a HTTP/1.1 client closes the socket (or the server for that matter) doesn't mean that the client has gone away, just that the socket hasn't been used for a while.

There are something called COMET servers which are used to let client/server continue to "chat" over HTTP. Search for comet here at SO or on the net, there are several implementations available.

10
  • 3
    @jgauffin, thanks for the response. It's interesting you mention COMET because that is essentially waht I am trying to use. When using an AsyncController, you are engaging in long-lived HTTP connections -- just like COMET. Therefore, if the user closes the browser while the connection is still open, why can't the server know about it and inform me?
    – Kirk Woll
    Jan 23, 2011 at 15:33
  • @Kirk, when using an AsyncController the client is not engaging in any long lived connections. It's just the same as a normal Controller in terms of the HTTP protocol. The only difference is that the ASP.NET will begin the request on one worker thread and could finish it on another and in-between it could use IOCP if your API supports it. But looking at the protocol level exactly the same stuff is exchange if you weren't using an async controller. Jan 23, 2011 at 15:37
  • 2
    @Darin, I believe you are mistaken. In firebug or even just the browser directly, you can see that the connection is open. You can hit the escape key and it will terminate the connection. It's exactly like a request that is just taking a very long time to process. (or to put it another way, you are correct -- it's exactly like a normal controller that is taking a very long time to service an action -- which is indeed a long-lived HTTP connection.)
    – Kirk Woll
    Jan 23, 2011 at 15:38
  • 1
    @Darin, What about the TCP RST packet? I know it's not guaranteed, but I thought most well behaving socket implementations will send that over in this scenario.
    – Kirk Woll
    Jan 23, 2011 at 15:45
  • 1
    @jgauffin -- I think I see what I was missing now. You and Darin are obviously correct that after the request is finished the client can't send any packet, RST or otherwise. However, I do disagree with your conention that polling is better than long-lived HTTP connections. (i.e. AsyncController) BOSH and COMET use what are essentially use this technique to great effect. Anyway, I see why what I want is impossible. Bummer. Will mark this as correct.
    – Kirk Woll
    Jan 23, 2011 at 15:58
-2

For obvious reasons the server cannot be notified that the client has closed his browser. Or that he went to the toilet :-) What you could do is have the client continuously poll the server with AJAX requests at regular interval (window.setInterval) and if the server detects that it is no longer polled it means the client is no longer there.

6
  • 1
    I appreciate the response, but I do realize that HTTP does not allow me to know when a user closes the browser after the response is delivered. However, that is not what I asked. Part and parcel of an AsyncController is that the request is not finished -- it remains open. In principle, it seems possible to know that there is no client to send the repsonse to.
    – Kirk Woll
    Jan 23, 2011 at 15:32
  • 1
    @Kirk, in terms of the HTTP protocol an AsyncController is not any different than a normal Controller. The protocol always stays stateless. Jan 23, 2011 at 15:35
  • 1
    it's stateless, and it's request/response. But that transaction can still take a very long time. This is exactly how COMET and BOSH works (or frankly, even Gmail when you have chat enabled), and I don't understand why the termination of that live connection (it is a live connection, that's why you can send a response back at the end) isn't recognizable.
    – Kirk Woll
    Jan 23, 2011 at 15:41
  • 2
    -1 HTTP sits on top of TCP and when the user closes the browser, the TCP connection is closed. The 'statelessness' of HTTP is between subsequent request/response pairs, it does not effect whether the connection has state or other states within serving the response ( e.g. reading from the tcp pipe, anything up to a double CR LF are headers - there's a state machine right bang in the middle of the HTTP protocol for you). Sep 26, 2013 at 10:32
  • 1
    As long as you have TCP KeepAlive, or the server attempts to start writing a response, or the client’s OS cleanly closes the connection, IIS knows that there is no longer a client. Other answers have actually described the ASP.NET API’s exposure of this knowledge.
    – binki
    Jan 30, 2015 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.