9

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.

Here's a simplified example:

Index.cshtml:

    @model Int32?
    <!DOCTYPE html>
    <head>
        <title></title>
<script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"/>
<script src="@Url.Content("~/Scripts/jquery.signalR-2.0.3.min.js")"
    type="text/javascript" />
<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"/>
    </head>

    <script>
        _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 < @(Model.HasValue ? Model.Value : 1 ); i++) {
                  addToGroupAndBroadcast(i);
            }
        };

        function addToGroupAndBroadcast(aGroupName) {
            _testHub.server.addToGroupAndBroadcast(aGroupName)
                .fail(function (desc) {
                    console.info("Error: " + desc);
                });
        };

    </script>

SignalRTestController.cs:

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
    }
}

TestHub.cs:

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<TestHub>().Groups.Add(Context.ConnectionId, aGroupName).Wait();
            Clients.Group(aGroupName).sayHello("Hello", aGroupName);
        }

        #endregion
    }
}

Startup.cs:

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);
        }
    }
}

The controller takes an integer as input which is the number of Add's to do. For example, if you call:

http://yourip.com/WebApplicationName/SignalRTest/Index/1 one Groups.Add works fine http://yourip.com/WebApplicationName/SignalRTest/Index/2 two Groups.Add works fine http://yourip.com/WebApplicationName/SignalRTest/Index/3 three Groups.Add works fine http://yourip.com/WebApplicationName/SignalRTest/Index/4 four Groups.Add works fine http://yourip.com/WebApplicationName/SignalRTest/Index/5 five Groups.Add works fine http://yourip.com/WebApplicationName/SignalRTest/Index/6 six Groups.Add broken http://yourip.com/WebApplicationName/SignalRTest/Index/7 seven Groups.Add broken http://yourip.com/WebApplicationName/SignalRTest/Index/8 eight Groups.Add broken http://yourip.com/WebApplicationName/SignalRTest/Index/9 nine Groups.Add broken http://yourip.com/WebApplicationName/SignalRTest/Index/10 ten Groups.Add broken

...

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! :)


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.

5
  • Firstly, I am not seeing the tie between your version of VS and why you can't upgrade to the latest version of SignalR which is open source. Secondly, when I implmented SignalR, I didn't require any of the code that you've posted. Where did you get the code you are using? Did you write it yourself? Jun 17, 2014 at 12:21
  • I was under the impression from google searching that SignalR versions greater than 1.2.1 required .net 4.5 and Visual Studio 2010 did not support that. One person mentioned you could rebuild SignalR yourself from source but we thought that option was risky.
    – Adam
    Jun 17, 2014 at 12:35
  • Yes I wrote that code myself. This js file was a lot simpler initially. We followed the microsoft SignalR guide down to a tee but when we released our software into the wild we had issues. It had to be modified due to real world exceptional cases. Our software runs on a website that is shown 24/7 over an internal network with hundreds of signalR updates a second. Some of these issues may have been due to the older version of SignalR we were using (1.0.1 initially) and we may have fixed things that are no longer bugs in the newest version of SignalR.
    – Adam
    Jun 17, 2014 at 12:42
  • Additionally, some of the code in the JS file above is application specific. I tried to reduce the JS file to the minimum keeping everything relevant to this question present.
    – Adam
    Jun 17, 2014 at 12:48
  • You should probably post more code related to your Service.ClientServices.Registration.Register method and what runtime values are passed to DashboardHub.Register(String aToken). Can you actively debug and see the exception or does it just get logged into a file? It looks like there is a related issue that has been fixed here github.com/SignalR/SignalR/issues/1155. If that's it, you would have to pull the code and recompile your own DLL. Jun 17, 2014 at 12:58

2 Answers 2

2
+50

You can have more than 5 groups with long-polling, but you'll have to work around the connection issue by not adding to groups in a loop.

The task returned from Groups.Add only completes when it receives an ACK message from the message bus indicating that the message for the client (notifying it of the group add) has been sent. If there are more than 5 (or whatever number is enforced by the browser) pending AJAX requests tied to the addToGroupAndBroadcast call from the client, then the message to the client can't be sent. If you attach a done callback to your client call:

_testHub.server.addToGroupAndBroadcast(groupName).done(function() {
    console.log("complete");
});

you'll see the first call completes almost immediately.

This is when the first Groups.Add call returns. The message to sayHello doesn't get sent anymore because it would require another open polling request, which the client is creating, but won't get handled since there are multiple other pending requests from the client that have been enqueued first (and those are all waiting on addToGroupAndBroadcast to complete).

So after the first call to Groups.Add, the server is unable to send anything to the client, the MessageHub can't send its ACK which would resolve the remaining Groups.Add promises. As a result, the TaskCanceledException gets thrown for those Tasks because of a timeout.

As a work-around, I would suggest to create a hub method which accepts an array of group names:

public async Task AddToGroups(string[] names)
{
    foreach (var name in names)
    {
        await Groups.Add(Context.ConnectionId, name);
        Clients.Group(name).sayHello("Hello", name);
    }
}
0

It looks like there is a related issue that has been fixed here https://github.com/SignalR/SignalR/issues/1155. If that's it, you would have to pull the code and recompile your own DLL. Also, your bug could also be caused by a timeout.

This could also be relevant to your problem:

... because the connection id that you are trying to remove might no longer be available. In that case, TaskCanceledException is thrown after the request times out.

Ref. http://www.asp.net/signalr/overview/signalr-20/hubs-api/working-with-groups

15
  • It seems like this could be related. We just received VS 2013 today so i'm in the process of building the project against .net 4.5 with the latest version of SignalR and if the bug is fixed the bounty is yours.
    – Adam
    Jun 17, 2014 at 13:46
  • Good luck. If possible, I would try to avoid asynchronous methods such as .Wait() Jun 17, 2014 at 13:54
  • Unfortunately the problem was still reproducable with the latest version of SignalR 2.0.3 and .net 4.5.
    – Adam
    Jun 17, 2014 at 17:46
  • So, it must be related to your specific code. Any thoughts on removing the .Wait()? I don't have any such method in my implementation. If, in fact, you are using the latest version, could you now just follow the latest documentation and "recreate" your current functionality this time; however, in the "recommended" way? Jun 17, 2014 at 19:49
  • I'm hesitant to remove the functionality the way it is until I can guarantee the bugs we saw in the field are fixed. The reason why we have to wait on the Groups.Add is because there is a postcondition that relies on the client being added to the group later in the register function. The way it works is a client is added to the group, then provided initial data, all in the same function. I could rewrite this a different way but i'd rather get at the bottom of this problem. I've began downloading the SignalR source to find out why the Task is being cancelled through the debugger.
    – Adam
    Jun 18, 2014 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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