fix(storage): detect a download that closed before Content-Length and retry
HttpClient's GetAwaiter().GetResult() on ReadAsStreamAsync returns once headers arrive; a connection that then closes early leaves CopyTo finishing normally with a short file instead of throwing. Compare the written length against the advertised Content-Length and treat a mismatch as a transient failure. FakeStorage.ps1 can now simulate this (keys containing '/short/').
This commit is contained in:
@@ -230,6 +230,13 @@ function Invoke-StorageDownload {
|
||||
$source = $response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
|
||||
$target = [System.IO.File]::Create($TargetPath)
|
||||
try { $source.CopyTo($target, 1048576) } finally { $target.Dispose(); $source.Dispose() }
|
||||
# HttpClient reports a success status before the body is finished. A connection that
|
||||
# closes early leaves a short file and CopyTo returns normally, so check the length
|
||||
# the server advertised and retry like any other transient failure.
|
||||
$expected = $response.Content.Headers.ContentLength
|
||||
if ($null -ne $expected -and (Get-Item -LiteralPath $TargetPath).Length -ne $expected) {
|
||||
throw "body ended at $((Get-Item -LiteralPath $TargetPath).Length) bytes, Content-Length was $expected"
|
||||
}
|
||||
return
|
||||
} catch {
|
||||
if ($permanent -or $attempt -ge 5) { throw "Download of '$Key' failed: $($_.Exception.Message)" }
|
||||
|
||||
@@ -438,6 +438,8 @@ Describe 'Storage transfers' {
|
||||
$remote = Join-Path $TestDrive 'store'
|
||||
New-Item -ItemType Directory -Path (Join-Path $remote 'bucket/flaky') -Force | Out-Null
|
||||
Set-Content -LiteralPath (Join-Path $remote 'bucket/flaky/object.txt') -Value 'eventually'
|
||||
New-Item -ItemType Directory -Path (Join-Path $remote 'bucket/short') -Force | Out-Null
|
||||
Set-Content -LiteralPath (Join-Path $remote 'bucket/short/object.txt') -Value 'complete-body' -NoNewline
|
||||
$server = Start-FakeStorage -Root $remote
|
||||
$s3 = Get-StorageConfig (New-S3Settings $server.Url)
|
||||
$body = [System.Text.Encoding]::UTF8.GetBytes('encrypted export bytes')
|
||||
@@ -484,6 +486,13 @@ Describe 'Storage transfers' {
|
||||
Get-Content -LiteralPath $target | Should -Be 'eventually'
|
||||
Should -Invoke Start-Sleep -Times 1 -Exactly
|
||||
}
|
||||
|
||||
It 'retries when a 200 response closes before Content-Length and keeps the complete body' {
|
||||
$target = Join-Path $TestDrive 'short.txt'
|
||||
Invoke-StorageDownload -Storage $s3 -Key 'short/object.txt' -TargetPath $target 3>$null
|
||||
[System.IO.File]::ReadAllBytes($target) | Should -Be ([System.Text.Encoding]::UTF8.GetBytes('complete-body'))
|
||||
Should -Invoke Start-Sleep -Times 1 -Exactly
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'Streaming KHDB merge (compiled KhdbMerger)' {
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
# - GET <bucket>/<key> serves $Root/<bucket>/<key>, 404 if missing.
|
||||
# - PUT stores the body only if it matches x-amz-content-sha256, else 400 BadDigest (as S3 does).
|
||||
# - Keys containing '/flaky/' answer 503 to their first GET; keys containing '/reject/' refuse PUTs.
|
||||
# - Keys containing '/short/' advertise the full Content-Length on their first GET, then close
|
||||
# after all but the last byte. That is a truncated body with a success status, not an HTTP error.
|
||||
# Requests are logged as "<METHOD> <bucket>/<key>"; Get-FakeStorageLog drains the log.
|
||||
|
||||
function Start-FakeStorage {
|
||||
@@ -30,6 +32,10 @@ function Start-FakeStorage {
|
||||
$gets[$key] = 1 + [int]$gets[$key]
|
||||
if ($key -like '*/flaky/*' -and $gets[$key] -eq 1) {
|
||||
$response.StatusCode = 503
|
||||
} elseif ($key -like '*/short/*' -and $gets[$key] -eq 1 -and (Test-Path -LiteralPath $path -PathType Leaf)) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($path)
|
||||
$response.ContentLength64 = $bytes.Length
|
||||
if ($bytes.Length -gt 1) { $response.OutputStream.Write($bytes, 0, $bytes.Length - 1) }
|
||||
} elseif (Test-Path -LiteralPath $path -PathType Leaf) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($path)
|
||||
$response.ContentLength64 = $bytes.Length
|
||||
|
||||
Reference in New Issue
Block a user