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

Dispatcher-based JoinableTaskFactory should post messages more conservatively #1272

Open
AArnott opened this issue Jan 9, 2024 · 0 comments

Comments

@AArnott
Copy link
Member

AArnott commented Jan 9, 2024

In Visual Studio, we have case where 500MB or more in memory is occupied with already-executed SingleExecuteProtector objects in the WPF Dispatcher's queue. To avoid deadlocks in cases of modal dialogs, we always post every request to the UI thread via the JTF as well as a queue backed by Windows or some GUI framework. This provides at least two avenues for every main thread work item to be completed, often leaving the lower-level queue filled with dead work items.

We want to reduce the strain this puts on memory and CPU to hold and eventually process and dump these dead objects.

The proposal is for the JoinableTaskFactory.PostToUnderlyingSynchronizationContext method to enqueue to its own private queue, from which it can trim dead work items opportunistically. It will necessarily also queue a work item to whatever the lower-level queue is as it does today, except that it will take care to only do this if there isn't already such a work item in that queue. When that work item is serviced, that JTF object will process some or all of its private queue. If it yields control while its queue is non-empty, it will post another item to the lower level queue to request more time later.

The most aggravated case is that of the WPF Dispatcher, which is enqueued here:

this.dispatcher.BeginInvoke(this.priority, callback, state);

We should consider adding the private queue and management functions to the base class, such that all derived classes can opt into using it easily. The default implementation and this WPF override are likely to both benefit. The derived type in Visual Studio that schedule work to TaskSchedulers may want to participate as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant