Skip to content

Commit

Permalink
Fix retry when downloading clang tools
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.

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 dotnet#57196
  • Loading branch information
jakobbotsch committed Sep 30, 2021
1 parent 72d643d commit 4f2331d
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions eng/formatting/download-tools.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

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

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

if (-not $(ls $downloadOutputPath | Where-Object {$_.Name -eq "$toolName.exe"}))
{
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)
{
Write-Output "Attempting download of '$baseUri/$toolName.exe'"
try {
# Pass -PassThru as otherwise Invoke-WebRequest leaves a corrupted file if the download fails. With -PassThru the download is buffered first.
$null = Invoke-WebRequest -Uri "$baseUri/$toolName.exe" -OutFile $(Join-Path $downloadOutputPath -ChildPath "$toolName.exe") -PassThru
$success = $true
break
} else {
echo "Download attempt $($i+1) failed. Trying again in $($baseBackoffSeconds + $baseBackoffSeconds * $i) seconds"
Start-Sleep -Seconds $($baseBackoffSeconds + $baseBackoffSeconds * $i)
}
catch {
Write-Output $_
}

Write-Output "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 $success) {
Write-Output "Failed to download $toolName"
throw [System.IO.IOException] "Could not download $toolName"
}
}
}
Expand Down

0 comments on commit 4f2331d

Please sign in to comment.