Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SignalR MaximumParallelInvocationsPerClient only run on one thread #54836

Closed
quytrieu37 opened this issue Mar 29, 2024 Discussed in #54835 · 2 comments
Closed

SignalR MaximumParallelInvocationsPerClient only run on one thread #54836

quytrieu37 opened this issue Mar 29, 2024 Discussed in #54835 · 2 comments
Labels
area-signalr Includes: SignalR clients and servers

Comments

@quytrieu37
Copy link

Discussed in #54835

Originally posted by quytrieu37 March 29, 2024
I'm currently using one HubConnection to execute multiple tasks on a hub. I've configured MaximumParallelInvocationsPerClient = 4 in the hub settings. It seems that when tasks complete quickly, they run concurrently. However, if there is a long-running task, it blocks other tasks from running.
To investigate this issue, I logged the thread IDs and found that all tasks from one connection use the same thread. Therefore, when a long-running task occurs, it blocks other tasks.
I utilized Thread.Sleep to simulate a long-running task.

image

image

As observed in the provided screenshot, all tasks are executed on the same thread. Consequently, when a long-running task occurs, it causes other tasks to be blocked.
image

To address this issue, I used Task.Run to execute my long-running task in the background, and everything worked fine.
image

image

However, I'm wondering if there is a configuration option to run concurrent tasks from one connection in multiple threads?
And Is the MaximumParallelInvocationsPerClient only allow each client call multiple hub method at same time but in the same thread?
Thank you!

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-signalr Includes: SignalR clients and servers label Mar 29, 2024
@BrennanConroy
Copy link
Member

However, I'm wondering if there is a configuration option to run concurrent tasks from one connection in multiple threads?

No, you can do this by calling Task.Run inside your method or await Task.Yield();. It would be needlessly expensive to do this for every one.

And Is the MaximumParallelInvocationsPerClient only allow each client call multiple hub method at same time but in the same thread?

No. You are blocking the thread by calling Task.Sleep or by running a long synchronous operation. As soon as you go async via Task.Yield or fire a new task and exit the method via Task.Run then you unblock SignalR and allow it to call more methods on the same client.

@quytrieu37
Copy link
Author

Thank you for your response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-signalr Includes: SignalR clients and servers
Projects
None yet
Development

No branches or pull requests

2 participants