Julian Jelfs’ Blog

Interrupted XmlHttpRequests cause IE to hang

Posted in Javascript by julianjelfs on May 6, 2009

I ran into a problem using MS Ajax script services recently. I was getting reports of intermittent hanging in IE (6 and 7) with some newly developed pages. I had a vague feeling it was something to do with the Script Services and a quick search revealed that this is a relatively well known problem.

As described in one of the responses to that question, the issues is caused when a page closes after an asynchronous ajax request is started but before it completes. In this scenario one of the browser’s available connections (of which it has 2 by default) is tied up and not released. So if this happens a couple of times, the browser has no available connections to service normal requests and appears to hang.

The solution is to abort any pending XmlHttpRequest when the page unloads. It’s not totally obvious how to do this when you are using Script Services with the generated proxy, because the actual XmlHttpRequest object itself is buried in multiple layers of magic. After a bit of digging I came up with the following solution:

    var pending = null;
    Sys.Net.WebRequestManager.add_invokingRequest(function(executor, args){
        pending = args.get_webRequest().get_executor();

    Sys.Net.WebRequestManager.add_completedRequest(function(executor, args){
        pending = null;


So what this does is just track the executor of the currently executing web request and set it to null when the request completes. On unload, we check if there are any pending request executors, and if there are we call abort on them. This prevents the hanging issue for me in IE which is a big relief.

Obviously this mechanism may not be adequate for you if ajax calls are not queued because there may be more than one pending request. In my case, all service calls are channelled through a queue so that can’t happen. Otherwise you would have to implement some mechanism to correlate the invoked request with the completed request.

Tagged with: ,