Deadlock in cancelled build
- Dominant language
- C#
- Stars
- 5.5k
- Forks
- 1.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 139
Description
_Originally reported as https://developercommunity.visualstudio.com/content/problem/377146/vs2017-hangs-when-building-a-project.html. Dump analysis by @tmeschter._
It's possible to deadlock MSBuild when the main thread has a lock on `BuildManager._syncLock`, and is waiting for the logging event queue to finish up:
```
[0x23] mscorlib_ni!System.Threading.Tasks.Task.Wait(Int32, System.Threading.CancellationToken) + 0x35
[0x24] Microsoft_Build_ni!Microsoft.Build.BackEnd.Logging.LoggingService.TerminateLoggingEventQueue() + 0x45
[0x25] Microsoft_Build_ni!Microsoft.Build.BackEnd.Logging.LoggingService.WaitForThreadToProcessEvents() + 0x19
[0x26] Microsoft_Build_ni!Microsoft.Build.Execution.BuildSubmission.CompleteLogging(Boolean) + 0x3a
[0x27] Microsoft_Build_ni!Microsoft.Build.Execution.BuildManager.ExecuteSubmission(Microsoft.Build.Execution.BuildSubmission, Boolean) + 0x4c0
[0x28] Microsoft_Build_ni!Microsoft.Build.Execution.BuildSubmission.ExecuteAsync(Microsoft.Build.Execution.BuildSubmissionCompleteCallback, System.Object, Boolean) + 0x4f
```
```
[0xc] clr!JITutil_MonReliableContention + 0x8a
[0xd] Microsoft_Build_ni!Microsoft.Build.Execution.BuildManager.OnProjectFinished(System.Object, Microsoft.Build.Framework.ProjectFinishedEventArgs) + 0x3e
[0xe] Microsoft_Build_ni!Microsoft.Build.BackEnd.Logging.LoggingService.TryRaiseProjectFinishedEvent(Microsoft.Build.Framework.BuildEventArgs) + 0x2e
[0xf] Microsoft_Build_ni!Microsoft.Build.BackEnd.Logging.LoggingService.RouteBuildEvent(System.Collections.Generic.KeyValuePair`2) + 0x4a
[0x10] Microsoft_Build_ni!Microsoft.Build.BackEnd.Logging.LoggingService.RouteBuildEvent(System.Object) + 0x154
[0x11] Microsoft_Build_ni!Microsoft.Build.BackEnd.Logging.LoggingService.LoggingEventProcessor(System.Object) + 0x85
[0x12] Microsoft_Build_ni!Microsoft.Build.BackEnd.Logging.LoggingService.b__115_0(System.Object) + 0xa
```
This codepath will only be traveled if `_shuttingDown` (that is, the build has been cancelled):
https://github.com/Microsoft/msbuild/blob/0f8106a6ef9b6347e43815cc842130d2ed349ef5/src/Build/BackEnd/BuildManager/BuildManager.cs#L929-L937
Otherwise, work is posted to the work queue, but the lock is released before executing it.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.