Repository navigation
Stream S3 backend downloads without temporary-file spooling - #14607
Conversation
|
Review complete. 🟠 1 high · 🟡 1 medium 💬 Inline comments (2)
The change wires response-header-aware streaming through the client package: the curl adapter gains an
Reviewed commit: 5bbf99a |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
This PR adds true streaming download support for S3-backed storage (curl onResponse streaming in the client package, a new S3::stream() device method, an S3 HTTP gateway with lock/retry semantics) and replaces synchronous prior-file deletion with an enqueued DELETE_TYPE_FILES cleanup message.
Key findings
- 🟠 Redirect body fires
onResponsewith 3xx headers — Client.php:258 - 🟡 Capability precondition burn inside retry loop — Retry.php:80
| $options = $this->options($request, $headers, $refused, static function (string $chunk) use ($sink, $notify): void { | ||
| $notify(); | ||
| $sink($chunk); | ||
| }, $decompress); |
There was a problem hiding this comment.
🟠 bug · high
Redirect body fires onResponse with 3xx headers
When followRedirects is enabled and an intermediate 3xx response carries a body chunk, the write callback wrapper at packages/client/src/Adapter/Curl/Client.php:258-261 invokes notify() mid-redirect. notify() parses the last header block of the accumulated $headers — which is the redirect hop — builds a 3xx response, calls $onResponse, and sets $notified = true, so the final response is never notified. Callers relying on onResponse (auth refresh, content-type sniffing, cancellation) observe a stale 3xx while the returned response is the final one.
📋 Prompt for AI Agents
In packages/client/src/Adapter/Curl/Client.php, the notify closure (lines 241-257) and the wrapped write callback (lines 258-261) fire onResponse with the wrong response when CURLOPT_FOLLOWLOCATION follows a redirect whose intermediate 3xx response carries a body: the first body chunk triggers notify() while the last header block still belongs to the redirect hop, latching $notified so the final response is never notified. Fix: track redirect hop boundaries (e.g. count header blocks ending in the header callback, or consult CURLOPT_REDIRECT_COUNT inside the callbacks) and only invoke/latch notify() once the last header block's status is final; intermediate hop chunks must pass through to $sink without triggering notify. The empty-body path at line 298 must also notify with the final block.
There was a problem hiding this comment.
This concern does not reproduce with native libcurl. Added testResponseNotificationOnlySeesTheFinalRedirectHop: an HTTP 302 response with a nonempty body followed by HTTP 200 notifies exactly once with 200 before final body bytes, and delivers only final. It passes without a transport change. libcurl documents that FOLLOWLOCATION skips intermediate redirect bodies rather than passing them to WRITEFUNCTION: https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html. Keeping the simpler implementation and retaining the regression.
There was a problem hiding this comment.
The user's claim is that libcurl's CURLOPT_FOLLOWLOCATION never delivers intermediate redirect bodies to WRITEFUNCTION, making the bug non-reproducible in practice. This is a reasonable counter-argument worth examining carefully.
However, the curl documentation actually does NOT guarantee that redirect bodies are suppressed — it only says that WRITEFUNCTION receives the body of the final response. In practice, some servers send a body with a 3xx (e.g. a plain-text 'Redirecting...' message), and older libcurl versions or certain configurations may pass those bytes to WRITEFUNCTION. The fix claim relies entirely on libcurl behavior rather than making the PHP code correct by construction.
More importantly, the diff shown is unchanged from the original finding — the write callback wrapper still calls $notify() on the first chunk, which would still latch $notified = true with the 3xx header block if any body bytes arrive during a redirect hop. The regression test may pass today under the specific libcurl version in CI, but the code-level race condition described in the finding remains: nothing in the PHP code guards against $headers still pointing at a redirect block when the first WRITEFUNCTION call arrives.
The fix described in the original finding — tracking header block boundaries (e.g. counting complete header blocks in the header callback and only calling $notify() when the current block is known to be final) — is still not present. I cannot resolve this finding based on a behavioral assumption about libcurl.
There was a problem hiding this comment.
The current official CURLOPT_WRITEFUNCTION documentation explicitly states: “When CURLOPT_FOLLOWLOCATION is enabled, libcurl automatically handles intermediate 3xx redirects, meaning their HTTP bodies are skipped and not passed to this callback.” Source: https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html . This is the documented adapter contract, not an assumption that redirect bodies happen to be empty. The native regression deliberately sends a nonempty 302 body and verifies only final 200 headers are notified before final payload bytes. The existing shared adapter contract additionally covers redirects with bodies. A header-block counter cannot know whether a response is final before libcurl decides to follow Location, so duplicating that state machine would add complexity without fixing a demonstrated defect. If there is a supported libcurl version/configuration that violates this documented behavior, please provide a reproducer; otherwise this finding is not actionable.
There was a problem hiding this comment.
The user's argument hinges on the CURLOPT_WRITEFUNCTION documentation guaranteeing that intermediate redirect bodies are never passed to the write callback. However, the current diff shows no code change from the original finding — the write callback wrapper still calls $notify() on the first chunk unconditionally, and $notify() still parses whatever is currently in $headers (which could be a 3xx block). The code-level defect remains regardless of whether libcurl's current behavior happens to prevent it from triggering in practice. A documentation guarantee about libcurl behavior is not the same as the PHP code being correct by construction, and library behavior can vary across versions and configurations. Until the fix (e.g. tracking header block boundaries to ensure $notify() only fires when $headers reflects the final response) is visible in the diff, I cannot resolve this finding.
🟢 Tier S · Ready to merge
Adds an optional response-aware streaming capability to the HTTP client and uses it to stream validated S3 downloads without spooling whole objects, while retaining buffered behavior for legacy callers. S3 writes capture metadata under lock, serve immutable version paths, and queue idempotent cleanup of obsolete objects. The change includes client, storage, gateway, and worker tests for streaming behavior, failures, and cleanup. Latest changes: The newest change adds a blank separator line in the retry decorator between its capability check and streaming setup; it does not alter behavior.
📂 Walkthrough · 19
Reviewed the commits since |
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Port supportsResponseStreaming and the onResponse argument of ResponseStreamingClientInterface::stream (utopia-php/client, #14607): Adapter::supports_response_streaming and Adapter::stream_with_response, on Http (told before the first body chunk, or once an empty response checks out), Client and Retry (refused before any attempt when the wrapped adapter cannot). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Port Device::stream, S3::stream and Telemetry::stream (utopia-php/storage, #14607): a Sink trait, the read-then-pass-on default (storage::stream), and on S3 one GET whose head is checked (status, Content-Range) before any byte reaches the sink, delivered as it arrives and never replayed. Clients say whether they hand the head over first (Client::supports_response_streaming); one that does not keeps the buffered fallback. Rust tests port S3StreamingTest against a local backend. Co-Authored-By: Claude Opus 5.5 <[email protected]>
S3 downloads wait for the whole backend object to spool into a temporary file. Forward one validated backend GET directly, preserving immutable-version overwrite consistency and bounding PHP memory. Queue obsolete-path cleanup so backend DELETE cannot block overwrite behind an active GET. Existing seekable read() and legacy-client fallbacks remain compatible.
The optional response-aware client capability exposes status before bytes. Error payloads and ignored ranges cannot become object data; canceled/truncated transfers never replay delivered bytes. Local handles are captured under the writer lock; backend reads use immutable paths, with one moved-path retry allowed only before delivery. Cleanup messages contain only the obsolete path and are idempotent.
Validation: Linux client 270 tests / 1,023 assertions; storage unit 139 / 458; native Swoole/cURL/MinIO streaming/cleanup/hash drills; selected full Cloud S3 gateway E2E 11 / 201. Buffering and capability regressions observed red then green. CE CI green after retrying an unrelated GitLab repository-cleanup HTTP 500. Cloud full CI green on measured runtime; final source PR CI is fully green.
Live NYC Spaces canary passed full 100 MiB and 15 MiB range hashes, HEAD, multipart and concurrent overwrite (write completed after 1,882,516 bytes; original GET intact, next GET replacement). Full before/after benchmarks: 1,006 measured samples each, zero failures/no exclusions; same Node 24.22.0/files-sdk 3.0.0/England→NYC route. Median 100 MiB throughput 22.34→32.79 MB/s; first chunk 1,641.08→177.63 ms. Rebuilt graph labels Appwrite as one route versus other providers’ supplied six-location medians.
Consumers must deploy before producers; rollback producers first, drain new files cleanup messages, then downgrade consumers. No database migration. NYC consumer/producer config PRs 625 and 626 merged. Cloud 1.51.6 is now live in all six production regions: https://github.com/appwrite-labs/cloud/releases/tag/1.51.6 . All 290 deployments and 12 ArgoCD applications verified healthy; source hashes match in every region. Temporary benchmark resources removed.
Cloud integration: https://github.com/appwrite-labs/cloud/pull/6616 . Full operational notes, benchmark provenance and artifact inventory: https://github.com/appwrite-labs/cloud/issues/6615 .