rabbitmq / rabbitmq-dotnet-client

RabbitMQ .NET client for .NET Standard 2.0+ and .NET 4.6.2+
https://www.rabbitmq.com/dotnet.html
Other
2.07k stars 579 forks source link

6.5.0 can cause application freeze due to TaskScheduler change #1357

Closed brandonagr closed 11 months ago

brandonagr commented 1 year ago

Describe the bug

This change https://github.com/rabbitmq/rabbitmq-dotnet-client/pull/1321 replaced Task.Run with a call to Task.Factory.StartNew with some TaskCreationOptions, but it didn't specify the TaskScheduler, which actually changed the behavior from using TaskScheduler.Default to TaskScheduler.Current

https://devblogs.microsoft.com/pfxteam/task-run-vs-task-factory-startnew/

The very confusingly named TaskScheduler.Default is not the default when calling Task.Factory.StartNew, TaskScheduler.Current is the default. To preserve existing behavior the line should be updated to _mainLoopTask = Task.Factory.StartNew(MainLoop, CancellationToken.None, TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

The change in 6.5.0 caused my UI application to freeze on startup after some code to connect to rabbitmq in a background task called into rabbitmq code, the scenario is best described here in this 11 year old question predicting this issue being frequent due to the design of the task api: https://stackoverflow.com/questions/6800705/why-is-taskscheduler-current-the-default-taskscheduler

Reproduction steps

From UI thread, use Task.Factory.StartNew(action) to run code that then initiates a rabbitmq connection. This works in 6.4 but freezes in 6.5

Expected behavior

MainLoop should always run on TaskScheduler.Default

Additional context

No response

lukebakken commented 11 months ago

Fixed by #1358