Repository navigation
Conversation
Add RequestContext::after_response(budget, task) and the adapter-facing AfterResponseScheduler/AfterResponseHandle in edgezero-core, plus the after-response-tasks manifest capability (Axum Native, Fastly BoundedCooperative, Cloudflare and Spin Unsupported). Axum spawns each task on the server's LocalSet, releases it when Hyper drops the response body, bounds it with a Tokio timeout inside its own outbound callback scope, and drains registered tasks on SIGTERM within the existing shutdown grace after connections settle. Fastly queues tasks per request lifecycle and drives them after the response is sent, inside the same callback scope, each raced against its budget. Adapters without a scheduler return 501 at registration. Closes #476.
Address self-review findings on the after-response task work: - Cover the Fastly registry lifecycle used by run_app and serve_app through send_request_with_registries_and_hooks_for_test, and assert that a prepare hook cannot replace the adapter scheduler. - Stamp each Fastly lifecycle with a generation so a retained handle is rejected instead of queuing into a later request. - Let an idle tracker win over an elapsed deadline in the Axum task wait, and log abandoned tasks on every shutdown exit path. - Classify Axum as BoundedCooperative, matching its await-point cancellation, and document AxumDevServer::run draining. - Add tests for the shared grace, a second signal during the task wait, Fastly outbound requests from a task, and native replacement. - Keep the handler example from failing pages on unsupported adapters and sync the design and plan documents with the code.
Move the abandoned-task warning from drain_tasks into run_owned through AfterResponseTracker::warn_abandoned, so the prebound serve() path and the unusable-deadline return also report cancelled tasks, exactly once. Exercise the prepare-hook override through the registry funnel test too.
Installing a rejecting Spin scheduler changes a zero budget from 501 to 500: core now finds a handle and rejects the budget before the scheduler, as it already does on Axum and Fastly. The guides, changelog and spec claimed every Spin registration returns 501 and that the zero budget case was unchanged. Correct them and pin the 500 with a Spin unit test. Also note that core pre-dispatch hooks can replace or remove the handle (removal falls back to core's 501), that PR #512 overlaps on the CHANGELOG line as well as the capabilities bullet, and point the #509 design at the follow-up.
#509 (2620377) adds bounded work after the response on Fastly and Axum, drained on Axum SIGTERM (EZI-1, #476). #512 (cdc232c) runs it on Cloudflare through waitUntil (#478), and #515 (ec347a2) reports Spin's capability error (#483). The line now stacks on #517 and each PR merged the one below it, so this merge is clean.
|
For Trusted Server's identity integration chain, this PR now stacks on #517, the top of the line
One behavior change, from the port: the base records an Checked locally at |
Exercise the consumer path Trusted Server relies on for follow-up writes: - Axum: a conditional Add record write inside a task is acknowledged and readable by a later request, without delaying the response. - Axum: SIGTERM arrives while a request is in flight, the handler then registers a KV write; the request and the write both complete within the grace, the process exits 0, and the value survives a restart. - Fastly: a guarded record write inside a task lands after delivery. Ignored in CI because the bare Viceroy runner has no KV stores; run it from the adapter crate, whose runner loads the demo fastly.toml.
|
For the identity integration branch (#495), I pushed |
|
For the identity integration branch (#495), I pushed |
|
For the identity integration branch (#495), I pushed |
dhruv8sh
left a comment
There was a problem hiding this comment.
This PR adds RequestContext::after_response and a core scheduler trait, with bounded implementations for Axum (tasks start when Hyper drops the response body, and shutdown waits for them within the grace period) and Fastly (tasks are queued per request and run after delivery inside the callback scope). Cloudflare and Spin report the capability as Unsupported. The Axum design is solid and well tested: tasks are tracked with a watch channel, the grace period is shared with connection draining, and registration closes once the response body is released.
Two problems need fixing before merge. First, nothing limits how many tasks a request can register or how long a budget can be. Second, on Fastly the tasks run before a NotCommitted delivery's fallback response is sent, which delays the client. The second one is a Fastly-only ordering issue.
I could not run the CI gates (fmt, clippy, tests) because I only had read access.
Comments not anchored to the diff:
- crates/edgezero-adapter-fastly/src/after_response.rs:1262 — 🔧 No limit on queued tasks or budget:
lifecycle.tasks.pushaccepts any number of tasks, anddrainonly caps each budget atDEADLINE_FAR_FUTURE. A handler in a loop, or one that sets a budget from request input, can grow memory without limit and keep the Fastly invocation busy long after the response. The Axum tracker has the same problem in production between shutdowns. The project guide asks for no unbounded growth.
Fix: add a per-request task limit and a maximum budget, either as core constants or as adapter config. Reject anything over the limit in AfterResponseHandle::schedule with a semantic error, and add tests for both rejection paths.
- crates/edgezero-adapter-fastly/src/request.rs:1472 — 🔧 Tasks run before a NotCommitted fallback is sent:
with_after_responsedrains the tasks before it returns theFastlyRequestDelivery. When delivery did not commit, the caller still has to send a native replacement response, so the client waits for every task to finish or hit its budget. The testafter_response_task_runs_when_delivery_did_not_commitconfirms this ordering. It also breaks the documented promise that tasks start after the response attempt ends.
Fix: return the delivery to the caller first and drain afterwards. Alternatively, when may_send_native_response() is true, send the native fallback before draining.
dhruv8sh
left a comment
There was a problem hiding this comment.
PR Review
Summary
This PR adds RequestContext::after_response(budget, task) with a portable AfterResponseHandle and AfterResponseScheduler in core. Axum gets a release-on-body-drop scheduler with a tracker that SIGTERM drains within the shutdown grace. Fastly gets a lifecycle-scoped, generation-stamped queue that is drained inside the callback scope after delivery. Cloudflare and Spin return a 501 capability error. Overall the design is careful and well tested. I found no blocking issues.
😃 Praise
- Axum ties the release to the drop of
AxumEgressBody. This covers a normal finish, a client disconnect and a cancelled service future without extra code. The tests show all three over a real Hyper connection, including the keep-alive case. - Connections and tasks share one grace in
drain_tasks, using abiasedselect where an idle tracker wins over a deadline that has already passed. The subprocess tests cover the shared grace, grace exhaustion and a second signal. - Fastly stamps each lifecycle with a generation and inserts the handle after the
preparescratch extensions. This means a handle kept from an earlier request on a reusedserve_appinstance is rejected, and a hook cannot replace the adapter's handle. Both cases have tests. - Core tests use
block_onand a drop probe to show that a task which fails to register is never polled and is dropped.
Findings
Blocking
None.
Non-blocking
- 🤔 No per-request cap on registered tasks: each
schedulecall queues a boxed future on Fastly or spawns a task on Axum, with no limit on the count (crates/edgezero-adapter-fastly/src/after_response.rs:80,crates/edgezero-adapter-axum/src/after_response.rs:94). See the inline comment.
📌 Out of Scope
ChristianPavilonis
left a comment
There was a problem hiding this comment.
I'd hold this until both adapters enforce the budget before polling a task, and Axum measures it from egress release. Neither fix requires preempting work already running in a poll.
With a 50 ms budget, controlled-clock checks reproduced task work at 100 ms in the Axum scheduler and in the Fastly task drain with an injected progress driver. The existing Axum listener and SIGTERM tests passed. The hosted Fastly contract suite passed 42 tests and skipped the guarded-write test, which needs a KV-configured runner.
- Cap each request at 64 after-response tasks and each budget at 30 s (MAX_AFTER_RESPONSE_TASKS, MAX_AFTER_RESPONSE_BUDGET), enforced in AfterResponseHandle::schedule; a refused task does not use a slot. - Add core run_within_budget, which checks the absolute deadline before every poll of the task and keeps a timer to wake pending work. Axum and Fastly both use it instead of timeout/select, which polled the task first. - Axum: the release publishes its time as it drops, and each task's deadline derives from it, so a busy executor no longer extends a budget. - Docs and rustdoc describe the budget as starting when the response attempt ends and cancelling before the next poll, plus the limits.
|
@dhruv8sh, on the two findings in your first review body:
|
Takes the base's three new commits (949c0b6). CHANGELOG.md conflicted in Unreleased; both the after-response and the portable client info / JA4 sections are kept.
Summary
ctx.after_response(budget, task): the task starts once the response attempt ends at the adapter's terminal egress boundary, and is cancelled when its budget elapses. Axum drains registered tasks on SIGTERM within the existing shutdown grace. Adapters without support (Cloudflare, Spin for now) return501 Not Implementedat registration instead of dropping work silently.after-response-tasksmanifest capability so apps can declare itrequiredand havebuild/serve/deployrefuse adapters that cannot run it.Stacked on #517 (
feat/portable-client-infoat10ed3803), which includes #406 (e37e0df2), #275, #499 and #460; this work needs #406's SIGTERM drain and #275's Fastly callback scope. Design:docs/superpowers/specs/2026-11-17-after-response-tasks-design.md; plan:docs/superpowers/plans/2026-11-17-after-response-tasks.md.Integration note: a current
git merge-treecomparison with #496 reports conflicts in Axum response conversion, Cloudflare/Spin contracts, and core error/extractor code. These siblings need reconciliation when integrated.Changes
edgezero-core/src/after_response.rs(new)AfterResponseScheduleradapter trait,AfterResponseHandleextension handle, 501 when absent, zero budget rejectededgezero-core/src/context.rsRequestContext::after_response(budget, task)edgezero-core/src/manifest.rsCapability::AfterResponseTasks(after-response-tasks)edgezero-adapter-axum/src/after_response.rs(new)spawn_localon the serverLocalSet, gated on a release signal,timeout(budget)inside a fresh outbound callback scope;watch-based task tracker; registration closes once releasededgezero-adapter-axum/src/{service,response}.rsAxumEgressBodyand fires when Hyper drops the body (final frame handed over, or early disconnect/cancellation)edgezero-adapter-axum/src/dev_server.rsdrain_taskswaits for the tracker after connections settle, under the same grace deadline (an idle tracker wins over an elapsed deadline); pending tasks at the grace are cancelled → existinggrace exhaustedfailure; abandoned tasks are logged on every exit path. Applies to production entrypoints,run_appandAxumDevServer::runedgezero-adapter-fastly/src/after_response.rs(new),request.rsfinish_request, tasks are drained inside the samewith_callbackscope (outbound and timers work), each raced against its budget; handle inserted only while armed, afterprepareextensions; each lifecycle has a generation so a retained handle is rejected;send_request_with_registries_and_hooks_for_testexposes therun_app/serve_appfunnel to contract testsedgezero-adapter-*/src/cli.rsBoundedCooperative(budget cancels at await/yield points), Cloudflare/SpinUnsupportedtests/after_response.rs), subprocess SIGTERM tests; Fastly host tests and Viceroy contract testscapabilities.md(new checked matrix),handlers.md,adapters/axum.md,adapters/fastly.md,CHANGELOG.md;scripts/check_outbound_docs_contract.mjschecks the new matrixCloses
Closes #476
This PR targets
feat/portable-client-info, so merging it into that branch does not automatically close the linked issue(s); close them manually or upon default-branch integration.Test plan
Historical author-reported local verification below (original head unrecorded unless an explicit SHA is given). These results, counts, timings and negative controls were not rerun in this audit, and their underlying local logs were unavailable; checked boxes retain the original report. At the current head
a5c176e1, all 29 reported hosted checks are passing.Local, Rust 1.98.1 / Node 24.20.0 (pinned versions):
cargo test --workspace --all-targetscargo clippy --workspace --all-targets --all-features -- -D warnings, plus the per-adapter wasm/native clippy matrix fromformat.ymland Fastly--features cli/--no-default-featurescargo fmt --all -- --checkcargo check --workspace --all-targets --features "fastly cloudflare spin"wasm32-wasip1(Fastly) /wasm32-wasip2(Spin) /wasm32-unknown-unknown(Cloudflare, core)examples/app-demoworkspace:cargo test --workspace --all-targets, fmt, clippynpm run lint && npm run format && npm run build;node scripts/check_outbound_docs_contract.mjs--test contract42/42,--lib357/357run_appregistry funnel fails its contract testprepareextensions fails the hook-override testsexamples/app-demowith a/after-taskroute (registers a task with a 1.5 s timer):run_app: the response arrives in ~1 ms and the task completes ~1.5 s later. SIGTERM right after the response waits ~1.5 s and exits 0. With a 1 s grace the task is cancelled, the process exits 1 withgrace exhausted, and the abandoned-task warning is logged.viceroy serve(0.21.0): the response arrives in ~1–2 ms and the task completes ~1.5 s later on two consecutive requests.spin upand Cloudflarewrangler dev --local:/after-taskreturns501withafter-response tasks are not supported by this adapter, while existing routes are unaffected.Not verified: deployed Fastly Compute and a production Axum container/ECS stop; Trusted Server consumer integration. Current reported hosted checks are passing.
Notes and limitations
ResponseEgressOutcome::HostHandoff), not proof of client receipt. Tasks run whatever the outcome (completed, error response, disconnect).BoundedCooperativefor both. On Fastly a blocked hostcall cannot be preempted.serve_appinstance, and — when delivery did not commit — a caller-sent native replacement response, by at most the longest registered budget (documented trade-off; skipping would drop work silently).waitUntil) #478 (CloudflarewaitUntil) and After-response capability error on Spin #483 (Spin capability error naming the adapter).Checklist
{id}syntax (not:id)edgezero_core(nothttpcrate)KvRegistry/ConfigRegistry/SecretRegistry(not the legacy single-handle setters) — not touched