Skip to content

Commit

Permalink
Fix retry when downloading clang tools (#59806)
Browse files Browse the repository at this point in the history
Invoke-WebRequest throws an exception when the download fails, so we
should use try-catch instead of status code to check failure. We can use
the generic Retry function from tools.ps1 to do this.

In addition pass -PassThru to avoid leaving a corrupted download file in
case the download fails. This will buffer the download and write it once
at the end.

Fix #57196
  • Loading branch information
jakobbotsch authored Sep 30, 2021
1 parent 909cddf commit 5e4bd40
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions eng/formatting/download-tools.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
. "$PSScriptRoot/../common/tools.ps1" # for Retry function

function DownloadClangTool
{
function DownloadClangTool {
param (
[string]
$toolName,
Expand All @@ -10,28 +10,14 @@ function DownloadClangTool

$baseUri = "https://clrjit.blob.core.windows.net/clang-tools/windows"

if (-not $(ls $downloadOutputPath | Where-Object {$_.Name -eq "$toolName.exe"}))
{
$baseBackoffSeconds = 2;

$success = $false
for ($i = 0; $i -lt 5; $i++) {
echo "Attempting download of '$baseUri/$toolName.exe'"
$status = Invoke-WebRequest -Uri "$baseUri/$toolName.exe" -OutFile $(Join-Path $downloadOutputPath -ChildPath "$toolName.exe")
if ($status.StatusCode -lt 400)
{
$success = $true
break
} else {
echo "Download attempt $($i+1) failed. Trying again in $($baseBackoffSeconds + $baseBackoffSeconds * $i) seconds"
Start-Sleep -Seconds $($baseBackoffSeconds + $baseBackoffSeconds * $i)
}
}
if (-not $success)
{
Write-Output "Failed to download clang-format"
return 1
}
if (-not $(ls $downloadOutputPath | Where-Object { $_.Name -eq "$toolName.exe" })) {

Retry({
Write-Output "Downloading '$baseUri/$toolName.exe'"
# Pass -PassThru as otherwise Invoke-WebRequest leaves a corrupted file if the download fails. With -PassThru the download is buffered first.
# -UseBasicParsing is necessary for older PowerShells when Internet Explorer might not be installed/configured
$null = Invoke-WebRequest -Uri "$baseUri/$toolName.exe" -OutFile $(Join-Path $downloadOutputPath -ChildPath "$toolName.exe") -PassThru -UseBasicParsing
})
}
}

Expand Down

0 comments on commit 5e4bd40

Please sign in to comment.