简体   繁体   中英

SignalR hubs Invoke deadlock

I have some hubProxy.Invoke("Method", param) methods which are executing asynchronously. All of them calling the same method but with different args. The problem is that all of them executing properly on some PCs but another faces problem that only first method call is executed while others are executing forever. Method looks like

if (!connectingTask.IsCompleted)
{
  logger.Debug("awaiting for SignalR connection");
  await connectingtTask;
  logger.Debug("SignalR connected");
}
await hubProxy.Invoke("MethodName", args);
logger.Debug("MethodName invoked");

Where connectingTask = Connection.Start(); and Connection is a HubConnection instance.

When all methods (2, for example) are executed output looks like

Method 1: awaiting for SignalR connection
Method 2: awaiting for SignalR connection

Method 1: SignalR connected
Method 1: MethodName invoked

Method 2: SignalR connected
Method 2: MethodName invoked

And when only first method is executed output looks like

Method 1: awaiting for SignalR connection
Method 2: awaiting for SignalR connection

Method 1: SignalR connected
Method 2: SignalR connected

Method 1: MethodName invoked

And Method 2 is deadlocked. There is no blocks in the Connection.StateChanged callback and no awits after Invoke() methods. So what am I doing wrong and how to Invoke the same method with different args asynchronously properly? Should I have some queue or something?

Edit Or can it be because I'm sending Invoke after await connectionTask ? Can't check it now because on my PC everything works fine. Link:

https://forums.asp.net/t/2024936.aspx?C+Client+Hangs+Deadlocks+Never+Returns

Edit And if relates the method on my hub I invoke has Task return type.

Edit Here is my hub method:

public Task Attach(string groupName)
{
    return Groups.Add(Context.ConnectionId, groupName);
}

I'm not able to turn on tracing because on my PC usually all right but once I was able to recreate same issue. Not remember how, I'll try to find later but the problem is that I'm not receiving answer when invoking hubs Attach method which is simply attaches user to group. So now instead of await hubProxy.Invoke("Attach", groupName) I'm using hubProxy.Invoke("Attach", groupName) . Can't understand why my first invokation is always successfull and I see some answer in trace but after that all invokations of the same method fails. But if I wait for some time (about 10+ seconds) and Invoke this method I'm again getting response. So looks like I'm able to Invoke hubs method only once per ~10 sec.

I'll try to recreate this issue again and add full trace.

Instead of using

await hubProxy.Invoke("MethodName", args);

try

hubProxy.Invoke("MethodName", args).Wait(500);

or whatever timeout you would like

I had the same issue and waiting seemed to have fixed it, I know await is the preferred method, but sometimes SignalR doesn't return properly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM