From 3707e1a83243394dbb3f576fcf185b462303ab15 Mon Sep 17 00:00:00 2001
From: John Luo <johluo@microsoft.com>
Date: Thu, 13 Jun 2019 12:48:23 -0700
Subject: [PATCH] Clean up background processes on CI builds (#11151)

Addresses issues where background Java processes causes builds to hang.
Added diagnostics to capture running processes on CI builds.
---
 build.cmd                    |  3 ++-
 build.ps1                    | 21 +++++++++++++++++++++
 eng/scripts/cibuild.cmd      |  1 +
 eng/scripts/dump_process.ps1 |  9 +++++++++
 restore.cmd                  |  2 +-
 5 files changed, 34 insertions(+), 2 deletions(-)
 create mode 100644 eng/scripts/dump_process.ps1

diff --git a/build.cmd b/build.cmd
index a66a229fb00..a9377209270 100644
--- a/build.cmd
+++ b/build.cmd
@@ -1,3 +1,4 @@
 @ECHO OFF
 SETLOCAL
-PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = ''; try { & '%~dp0build.ps1' %*; exit $LASTEXITCODE } catch { write-host $_; exit 1 }"
+PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = ''; try { & '%~dp0build.ps1' %*; exit $LASTEXITCODE } catch { write-host $_; exit 1 }"
+ECHO build.cmd completed
\ No newline at end of file
diff --git a/build.ps1 b/build.ps1
index 77fc54f1673..145384aaf07 100644
--- a/build.ps1
+++ b/build.ps1
@@ -124,6 +124,9 @@ param(
     # MSBuild for .NET Core
     [switch]$ForceCoreMsbuild,
 
+    # Diagnostics
+    [switch]$DumpProcesses, # Capture all running processes and dump them to a file.
+
     # Other lifecycle targets
     [switch]$Help, # Show help
 
@@ -243,6 +246,12 @@ $env:DOTNET_HOME = $DotNetHome
 
 # Execute
 
+if ($DumpProcesses -or $CI)
+{
+    # Dump running processes
+    Start-Job -Name DumpProcesses -FilePath $PSScriptRoot\eng\scripts\dump_process.ps1 -ArgumentList $PSScriptRoot
+}
+
 $korebuildPath = Get-KoreBuild
 
 # Project selection
@@ -372,4 +381,16 @@ finally {
     Remove-Module 'KoreBuild' -ErrorAction Ignore
     Remove-Item env:DOTNET_HOME
     Remove-Item env:KOREBUILD_KEEPGLOBALJSON
+
+    if ($DumpProcesses -or $CI)
+    {
+        Stop-Job -Name DumpProcesses
+        Remove-Job -Name DumpProcesses
+    }
+
+    if ($CI) {
+        & "$PSScriptRoot/eng/scripts/KillProcesses.ps1"
+    }
+
+    Write-Host "build.ps1 completed"
 }
diff --git a/eng/scripts/cibuild.cmd b/eng/scripts/cibuild.cmd
index 1e7d47b39e8..45a4ddb9edd 100644
--- a/eng/scripts/cibuild.cmd
+++ b/eng/scripts/cibuild.cmd
@@ -1,3 +1,4 @@
 @ECHO OFF
 SET RepoRoot=%~dp0..\..
 %RepoRoot%\build.cmd -ci -all -pack -sign %*
+ECHO cibuild.cmd completed
\ No newline at end of file
diff --git a/eng/scripts/dump_process.ps1 b/eng/scripts/dump_process.ps1
new file mode 100644
index 00000000000..3c35ea29ee4
--- /dev/null
+++ b/eng/scripts/dump_process.ps1
@@ -0,0 +1,9 @@
+Set-Location $args[0]
+
+$timestamp = $(get-date -f MM-dd-HH-mm)
+
+while ($true) {
+    Get-Process > artifacts/log/runningProcesses.$timestamp.txt
+    Get-WmiObject Win32_Process | select name, processid, commandline > artifacts/log/runningProcessesCommandLine.$timestamp.txt
+    Start-Sleep -Seconds 300
+}
\ No newline at end of file
diff --git a/restore.cmd b/restore.cmd
index 5e09500d3ef..26e2fe2f215 100644
--- a/restore.cmd
+++ b/restore.cmd
@@ -1,3 +1,3 @@
 @ECHO OFF
 SETLOCAL
-PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%~dp0build.ps1' -all -nobuild -restore %*; exit $LASTEXITCODE"
+PowerShell -NoProfile -NoLogo -ExecutionPolicy ByPass -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%~dp0build.ps1' -all -nobuild -restore %*; exit $LASTEXITCODE"
-- 
GitLab