From 5714570ad2074a34d2dd7a6de66a3d2e002e05e8 Mon Sep 17 00:00:00 2001 From: Tomas Kracmar Date: Fri, 6 Nov 2026 09:16:55 +0100 Subject: [PATCH] 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/'). --- src/Elysium/Private/Storage.ps1 | 7 +++++++ tests/Elysium.Tests.ps1 | 9 +++++++++ tests/FakeStorage.ps1 | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/src/Elysium/Private/Storage.ps1 b/src/Elysium/Private/Storage.ps1 index 507281f..8924128 100644 --- a/src/Elysium/Private/Storage.ps1 +++ b/src/Elysium/Private/Storage.ps1 @@ -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)" } diff --git a/tests/Elysium.Tests.ps1 b/tests/Elysium.Tests.ps1 index d6eec7d..a68e0f0 100644 --- a/tests/Elysium.Tests.ps1 +++ b/tests/Elysium.Tests.ps1 @@ -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)' { diff --git a/tests/FakeStorage.ps1 b/tests/FakeStorage.ps1 index fbc1705..4a70b84 100644 --- a/tests/FakeStorage.ps1 +++ b/tests/FakeStorage.ps1 @@ -3,6 +3,8 @@ # - GET / serves $Root//, 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 " /"; 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