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

Try to fix a problem that hardcoded thread schedule defeats the effort to prioritize UI blocking tasks. #1159

Merged
merged 11 commits into from
Mar 1, 2023

Conversation

lifengl
Copy link
Member

@lifengl lifengl commented Feb 26, 2023

The reader/writer lock implementation always spin off a new task with hardcoded task scheduler to call PrepareResources. It happens in the critical path to access a project in the project system implementation. Most of the time, when evaluation is up to date, this is expected to be a quick check, but the extra thread pool scheduling leads it to be sensitive to thread pool issues. Because of this hardcoded scheduler usage, it defeats the effort to use a priority queue to finish UI blocking tasks when thread pool has been scheduled for many background works.

The change is to prevent changing the behavior when GetResourceAsync is called on the UI thread, as it may lead new kind of deadlocks.

This is considered to be the reason (at least one reason) why priority queue doesn't seem to work in a 15s UI delay trace provided by David.

Lifeng Lu added 3 commits February 25, 2023 18:40
Preparing resource is a common routine to access evaluated project, most of the time, it is close to no-op when the evaluation is up to date. Because the code always schedules the call to the thread pool, it defeats the very reason to have a priority queue
for high priority work when thread pool is too busy. It also makes it hard to throttle project evaluations through special task scheduler.

The change is to try not to change the behavior, when the code is called on UI thread (as it can lead into new problems), but try to use the current task scheduler if possible.
@lifengl lifengl requested review from davkean and AArnott February 26, 2023 03:02
@lifengl lifengl marked this pull request as ready for review February 27, 2023 03:31
Copy link
Member

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm approving as your scenario is the primary user of this code, so my concerns being stated, you can decide how to act on them. :)

@@ -768,6 +769,7 @@ private Task PrepareResourceAsync(TResource resource, CancellationToken cancella
AsyncReaderWriterResourceLock<TMoniker, TResource>.Helper.ResourceState finalState = forConcurrentUse ? ResourceState.Concurrent : ResourceState.Exclusive;

Task? preparationTask = null;
TaskScheduler taskScheduler = TaskScheduler.Current.MaximumConcurrencyLevel > 1 ? TaskScheduler.Current : TaskScheduler.Default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me very nervous. There are other TaskSchedulers that may be in the system besides the ones you're thinking of. ReSharper for example has their own custom schedulers. I don't know how they set their MaximumConcurrencyLevel property, but in the past, folks who have used TaskScheduler.Current (usually by accident) often find themselves in deadlocks when their continuations inadvertently rely on a Resharper scheduler which has unknown and unwanted semantics.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I am not fan of this logic, but have trouble to provide an alternative solution. Another possibility is to create a new virtual method to let the lock to potentially provide the TaskScheduler for PrepareResource work, and this can leave using TaskScheduler.Default, but the project lock service can choose the set of TaskScheduler to be compatible here. Of course, this would introduce a hard-to-explain contract. Let me know if you think that is a better solution here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that sounds safer. And I'm a fan of as simple as possible, but no simpler. So in lieu of a simpler idea that is still safe, I like your virtual method idea. I'm guessing your override will do an actual concrete type check on the TaskScheduler to make sure it's the one you have in mind as safe and performant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, and it could make it much easier to apply a throttling of evaluations by using a special scheduler, instead of TaskScheduler.Default if we want to, and use a priority one for UI block/critical responsiveness path there if necessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR, and please take a look.

Copy link
Member

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@lifengl lifengl merged commit 6be02d2 into main Mar 1, 2023
@lifengl lifengl deleted the dev/lifengl/schedulingIssues branch March 1, 2023 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants