title: IIS App Pool cannot recycle when there is an open ASP.NET 4.5 Websocket
body: <p>I have run into an issue which can be replicated in the following way (you need IIS8 so must be on Windows 8+ or Windows Server 2012 R2+):</p>
  
  <p>Create a new website in IIS Manager, say TestWs on port 8881, pointing to a new folder, say C:\temp\testws, and add the following Web.config file in there</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
      &lt;compilation targetFramework="4.5"/&gt;
      &lt;httpRuntime targetFramework="4.5"/&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Now add the following WsHandler.ashx file in the same folder</p>
  
  <pre><code>&lt;%@ WebHandler Language="C#" Class="WsHandler" %&gt;
  
  using System;
  using System.Threading;
  using System.Web;
  
  public class WsHandler : IHttpHandler
  {
      public void ProcessRequest(HttpContext context)
      {
          context.AcceptWebSocketRequest(async webSocketContext =&gt;
          {
              while (true)
              {
                  await webSocketContext.WebSocket.ReceiveAsync(new ArraySegment&lt;byte&gt;(new byte[1024]), CancellationToken.None);
              }
          });
      }
  
      public bool IsReusable { get { return true; } }
  }
  </code></pre>
  
  <p>Then create a websocket from within the developer toolbar in your browser like so</p>
  
  <pre><code>var ws = new WebSocket("ws://localhost:8881/wshandler.ashx");
  ws.onclose = function() { console.log('closed'); };
  </code></pre>
  
  <p>In task manager you will see there is a w3wp.exe process for this application, if you kill it the client get the onclose event raised and the closed text will be printed.</p>
  
  <p>However if you create a websocket as described above and go to IIS manager and recycle the application pool, the websocket will not be closed, and there will now be two w3wp.exe processes.</p>
  
  <p>Closing the web socket <code>ws.close();</code> or refreshing the browser will cause the original w3wp.exe process to be shut down.</p>
  
  <p>It seems the presence of the open websocket is causing IIS to be unable to recycle the app pool correctly.</p>
  
  <p>Can anyone figure out what to change in my code or what to change in IIS to get this to work?</p>