title: SignalR LongPolling multiple Groups.Add for a single client Exception
body: <p>I've been struggling with this one for a while. We're using the latest SignalR 2.0.3. The problem happens when we add to multiple SignalR groups. The exception is only thrown when multiple adds happen from the same connectionId with different group names. The exception is only thrown if the LongPolling transport is selected. The exception is only thrown if you add to 6+ unique group names, 5 or less and it works fine.</p>
  
  <p>Here's a simplified example:</p>
  
  <p>Index.cshtml:</p>
  
  <pre><code>    @model Int32?
      &lt;!DOCTYPE html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;/title&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"/&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.signalR-2.0.3.min.js")"
      type="text/javascript" /&gt;
  &lt;script src="@Url.Content("~/signalr/hubs")" type="text/javascript"/&gt;
      &lt;/head&gt;
  
      &lt;script&gt;
          _testHub = $.connection.testHub;
          _testHub.client.sayHello = sayHello;
          $.connection.hub.start({ transport: 'longPolling' })
              .done(function() {
                  addAllGroups();
              });
  
          function sayHello(aMessage, aGroupName) {
              console.info("GroupName: " + aGroupName 
      + " Message Sent:" + aMessage);
          };
  
          function addAllGroups() {
              for (var i = 0; 
                i &lt; @(Model.HasValue ? Model.Value : 1 ); i++) {
                    addToGroupAndBroadcast(i);
              }
          };
  
          function addToGroupAndBroadcast(aGroupName) {
              _testHub.server.addToGroupAndBroadcast(aGroupName)
                  .fail(function (desc) {
                      console.info("Error: " + desc);
                  });
          };
  
      &lt;/script&gt;
  </code></pre>
  
  <p>SignalRTestController.cs:</p>
  
  <pre><code>using System;
  using System.Web.Mvc;
  
  
  namespace Instrumar.ProductionDashboard.Controllers
  {
      public class SignalRTestController : Controller
      {
          #region Public Members
  
          public ActionResult Index()
          {
              return View((int?)Convert.ToInt32(Request.Url.Segments[4]));
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>TestHub.cs:</p>
  
  <pre><code>using System.Threading.Tasks;
  using Microsoft.AspNet.SignalR;
  
  
  namespace Instrumar.ProductionDashboard.Hubs
  {
      public class TestHub : Hub
      {
          #region Public Members
  
          public void AddToGroupAndBroadcast(string aGroupName)
          {
              GlobalHost.ConnectionManager.GetHubContext&lt;TestHub&gt;().Groups.Add(Context.ConnectionId, aGroupName).Wait();
              Clients.Group(aGroupName).sayHello("Hello", aGroupName);
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>Startup.cs:</p>
  
  <pre><code>using Microsoft.AspNet.SignalR;
  using Microsoft.Owin;
  using Owin;
  
  [assembly: OwinStartup(typeof(SignalRChat.Startup))]
  namespace SignalRChat
  {
  
      public class Startup
      {
          public void Configuration(IAppBuilder app)
          {
              var lHubConfiguration = new HubConfiguration {EnableDetailedErrors = true};
              app.UseErrorPage();
              app.MapSignalR(lHubConfiguration);
          }
      }
  }
  </code></pre>
  
  <p>The controller takes an integer as input which is the number of Add's to do. For example, if you call:</p>
  
  <p><a href="http://yourip.com/WebApplicationName/SignalRTest/Index/1" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/1</a> one Groups.Add <strong>works fine</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/2" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/2</a> two Groups.Add <strong>works fine</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/3" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/3</a> three Groups.Add <strong>works fine</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/4" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/4</a> four Groups.Add <strong>works fine</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/5" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/5</a> five Groups.Add <strong>works fine</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/6" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/6</a> six Groups.Add <strong>broken</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/7" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/7</a> seven Groups.Add <strong>broken</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/8" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/8</a> eight Groups.Add <strong>broken</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/9" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/9</a> nine Groups.Add <strong>broken</strong>
  <a href="http://yourip.com/WebApplicationName/SignalRTest/Index/10" rel="nofollow">http://yourip.com/WebApplicationName/SignalRTest/Index/10</a> ten Groups.Add <strong>broken</strong></p>
  
  <p>...</p>
  
  <p>When I say broken, I get back a "System.Threading.Tasks.TaskCanceledException" in TestHub.cs on the line waiting for the task to complete. Everything works fine with ServerSentEvents but for LongPolling the problem exists. What am I doing wrong? Can I not have more than 5 SignalR groups with LongPolling? Help me! :)</p>
  
  <hr>
  
  <p>Update: Putting a 1 millisecond sleep using setTimeout between clientside calls fixed the problem. This seems to alleviate the number of pending connections in the network tab. Maybe something happens to the ability to add to a group when you reach the single browser connection limit. It would be nice to know exactly why this doesn't work though.</p>