SpecFlow.VisualStudio icon indicating copy to clipboard operation
SpecFlow.VisualStudio copied to clipboard

Visual Studio integration can deadlock VisualStudio

Open jamill opened this issue 8 years ago • 0 comments

There is a logic pattern in the Visual Studio integration that lends itself to deadlocking Visual Studio.

The dispose method in IdleTaskProcessingQueue will block on background threads. When a Solution is closed, this Dispose method is run on the UI via the event handler for OnSolutionClosed (from ProjectScopeFactory). If there are background threads running, and they try and access the UI thread (for example, if a background thread is attempting to access some VS Project COM API, it will be marshalled onto the UI thread) while the UI thread is blocked here, then this will result in a deadlock. The UI thread is blocked waiting for background threads to complete and background threads are blocked on access to UI thread.

Have to be careful that no background thread is accessing the UI thread when this lock is held (or maybe look at a way to handle the OnSolutionClosedEvent without blocking the UI thread). Hope this is helpful - please let me know if there are other questions I might be able to help out with.

As this is a race condition, I don't know how easy it is to reproduce (you could reproduce under a debugger by forcing the right sequence of actions). One series of steps that can result deadlock is to have a solution opened in Visual Studio that is under Git source control. Next, switch to a new Git branch via the Git integration inside of Visual Studio where one or more project files are different than in your current branch. As the project files are modified, the solution will be closed / reopened as part of the branch switch operation. If a background thread (that the UI thread is waiting on) needs access to the UI at some point while the dispose is happening, deadlock will occur.

If there is some other way I can assist here, please let me know.

jamill avatar Sep 21 '17 13:09 jamill