Skip to content

6.5.0 can cause application freeze due to TaskScheduler change #1357

@brandonagr

Description

@brandonagr

Describe the bug

This change #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions