fix: streaming callback returns non-opt StreamingCallbackHttpResponse#96
Merged
Merged
Conversation
The HTTP gateway decodes the streaming-callback reply via ic-agent into a non-optional StreamingCallbackHttpResponse. We returned `opt ...` — which the written HTTP Gateway Protocol spec specifies, but which the gateway does not accept: candid cannot coerce wire-type `opt T` into the expected `T`, so the reply failed to decode and every multi-chunk asset served HTTP 503. Single-chunk assets skip the callback, so no existing test caught it. Drop the Option/opt wrapper from the callback return type in the method, the candid interface, and the StreamingStrategy callback func type. Add an e2e test that round-trips a ~5 MB (3-chunk) asset through the local gateway, plus canister-core unit tests covering the multi-chunk streaming paths (3-chunk continuation token, non-identity encoding, single-chunk no-strategy, and the callback's unknown-key / unknown-encoding errors). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lwshang
added a commit
to dfinity/developer-docs
that referenced
this pull request
Jun 17, 2026
## Summary The streaming callback in the HTTP gateway protocol spec must return a non-opt `StreamingCallbackHttpResponse`. The spec incorrectly wrapped the return type in `opt`, which no real gateway accepts — candid cannot coerce wire-type `opt T` into the expected `T`, so serving any multi-chunk (streamed) asset fails to decode. Fixed all three occurrences: - `public/references/http-gateway.did` (downloadable interface) - `docs/references/http-gateway-protocol-spec.md` — full Canister HTTP Interface block - `docs/references/http-gateway-protocol-spec.md` — Response Body Streaming Interface block ```diff - callback: func (StreamingToken) -> (opt StreamingCallbackHttpResponse) query; + callback: func (StreamingToken) -> (StreamingCallbackHttpResponse) query; ``` Note: only the callback's **return type** changes. The `token : opt Token` field inside `StreamingCallbackHttpResponse` stays optional — a null *token* (not a null response) terminates the stream. ## Verification The real gateway decodes the callback reply into a non-opt `StreamingCallbackHttpResponse`: `ic-gateway` → `ic-http-gateway-protocol` (tag `v0.5.1`) → `ic-agent` / `ic-utils` - [`ic-http-gateway-protocol` @ v0.5.1 — `response_handler.rs#L116-L120`](https://github.com/dfinity/ic-http-gateway-protocol/blob/v0.5.1/packages/ic-http-gateway-protocol/src/response/response_handler.rs#L116-L120): calls `http_request_stream_callback` and matches `Ok((StreamingCallbackHttpResponse { body, token },))` — the bare struct, no `Option`. - [`ic-agent` — `ic-utils/src/interfaces/http_request.rs`](https://github.com/dfinity/agent-rs/blob/main/ic-utils/src/interfaces/http_request.rs): `http_request_stream_callback -> SyncCall<Value = (StreamingCallbackHttpResponse,)>`, and the callback CandidType `func((ArgToken) -> (StreamingCallbackHttpResponse) query)`. Detected while adding e2e streaming tests for certified-assets (dfinity/certified-assets#96). `npm run build` passes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Serving any multi-chunk asset (>
MAX_CHUNK_SIZE, 1.9 MB) through the HTTP gateway returned HTTP 503. The fix:http_request_streaming_callbacknow returns a bareStreamingCallbackHttpResponseinstead ofopt StreamingCallbackHttpResponse.Root cause
The HTTP gateway follows the streaming callback via
ic-agent, which decodes the reply into a non-optional(StreamingCallbackHttpResponse,). We returnedopt ..., and candid cannot coerce wire-typeopt Tinto the expectedT(subtype-incomparable), so the gateway failed withAgentError::CandidError("Candid returned an error") and served 503. The firsthttp_requestresponse decodes fine, so single-chunk assets worked and no existing test exercised the callback decode path.This means
optwas a real bug even though it matches the written HTTP Gateway Protocol spec. The spec text is stale: ic-agent and the SDK reference asset canister's actual wasm both use non-opt (the SDK's hand-written.didsaysoptbut its Rust returns non-opt). A separate docs PR will correct the spec.Changes
Option/optfrom the callback return type in three places: the#[query]method,certified-assets.did, and theStreamingStrategycallback func type (define_function!).crates/e2e/tests/streaming.rs): generates a ~5 MB (3-chunk) incompressible asset on the fly, deploys it, and asserts it round-trips byte-for-byte through the local gateway. A >2 MB body can only return intact via streaming callbacks, and the gateway validates the certificate before responding — so this also proves certified reassembly.Testing
cargo test -p canister-core— 104 passcargo test -p canister candid_interface_compatibility— passcargo test -p e2e— full suite pass (streaming round-trips through the gateway; previously 503)cargo fmt --check,cargo clippy --workspace --all-targets— clean🤖 Generated with Claude Code