Skip to content

caddyhttp: end request-body deadline ownership with handler - #8107

Merged
francislavoie merged 2 commits into
masterfrom
idle-request-body-deadlines
Oct 3, 2026
Merged

francislavoie merged 2 commits into
masterfrom
idle-request-body-deadlines

Conversation

@steadytao

@steadytao steadytao commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Fixes #8101 and #8103 -- Closes #8106

Summary

Prevent request-body reads from using the response controller after the handler which installed their timeout has returned. The reader now caches terminal body errors so transport drain reads cannot rearm a deadline after EOF. Handler completion revokes controller access while retaining a final idle deadline for any unread-body drain. The new lifecycle applies to server-level idle timeouts and the route-level timeouts handler.

Assistance Disclosure

None.

@steadytao
steadytao requested review from WeidiDeng, dunglas and francislavoie and removed request for dunglas October 2, 2026 14:55
@steadytao steadytao added the bug 🐞 Something isn't working label Oct 2, 2026
@steadytao

Copy link
Copy Markdown
Member Author

"and removed request for dunglas" -- Did I now? 🧐

@CLAassistant

CLAassistant commented Oct 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@francislavoie
francislavoie force-pushed the idle-request-body-deadlines branch from 0b75547 to b19e7ce Compare October 3, 2026 02:01
@francislavoie

Copy link
Copy Markdown
Member

Pushed b19e7ce on top of this: HandlerDone() now only arms the final read deadline for HTTP/1 requests that have a body.

Before, it armed a deadline for every request whose body was never read, including bodyless GETs. Only HTTP/1 drains an unread body on the connection after the handler returns, so that's the only case the deadline protects. On HTTP/2, SetReadDeadline is a serve-loop message plus a time.AfterFunc per stream. Since the serve loop selects between serveMsgCh and wroteFrameCh, that message can be handled after closeStream, leaving a timer nobody stops for the full timeout. The measured cost is small, but there's no reason to pay it.

  • New DrainDeadline field on IdleTimeoutReader, checked by HandlerDone()
  • server.go and the timeouts handler set it to r.ProtoMajor == 1 && r.ContentLength != 0. I used ContentLength rather than http.NoBody because the route-level reader wraps the server-level one, so the body is never NoBody there.
  • Tests for both the unset case and a bodyless GET through timeouts

Otherwise LGTM. I checked that the new regression tests fail against the pre-fix reader (h2 late-read panic; HTTP/1 418 from the cancelled context) and pass with the fix. go test -race -count=3 passes on caddyhttp, timeouts and reverseproxy.

Unrelated to this PR, but worth a follow-up: when timeouts is nested under the server-level reader, the inner (server) reader's SetReadDeadline runs last on every Read, so the route-level read_timeout is effectively overridden whenever read_body_idle is on (the default).

Assistance Disclosure

Claude Code (Opus 5.5) did the review, wrote the patch and drafted this comment; I reviewed them.

steadytao and others added 2 commits October 2, 2026 22:03
HandlerDone armed a final read deadline for every request whose body
was never read, including bodyless GETs. Only HTTP/1 drains an unread
body on the connection after the handler returns; on HTTP/2 the
deadline is a serve-loop message plus a timer per stream which can
outlive the stream if processed after it closes. Gate it behind a
DrainDeadline field, set for HTTP/1 requests with a non-zero
ContentLength.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@francislavoie
francislavoie force-pushed the idle-request-body-deadlines branch from b19e7ce to 0360a87 Compare October 3, 2026 02:03
@francislavoie
francislavoie enabled auto-merge (squash) October 3, 2026 02:04
@francislavoie
francislavoie merged commit ab7e6b6 into master Oct 3, 2026
33 checks passed
@francislavoie
francislavoie deleted the idle-request-body-deadlines branch October 3, 2026 02:11
koniecdev added a commit to koniecdev/LotroKoniecDev that referenced this pull request Oct 6, 2026
2.11.4 was pinned because 2.11.6 crashed the proxy (#988). Caddy 2.11.7
fixes that crash (caddyserver/caddy#8107), and its official Docker image
is now published. Both compose files move to the same 2.11.7 image by
digest, and the anchor comment says why.

Closes #989

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CwrjDhHx6t7YacvC5CJ21W
EgorKhaklin pushed a commit to EgorKhaklin/polaris-id that referenced this pull request Oct 23, 2026
…pair

python:3.12-slim (polaris_web/Dockerfile) and python:3.12-slim-bookworm
(both Dockerfile.prod stages) were re-resolved on 2026-10-23 (anonymous
read of Docker Hub's index). Both tags had moved past the grouped
update's digests, so they take today's, a0f57064 and 6a0e06f5, each an
index that lists linux/amd64 and linux/arm64. The update's digests are
in no tracked file now.

Dockerfile.caddy now fails the build unless the copied binary's
`caddy version` names the release its runtime base's CADDY_VERSION
names; an empty CADDY_VERSION fails too. The grouped update first moved
the runtime to 2.11.7 while the compile stage still built 2.11.6, and
nothing in the build compared the two.

The Dependabot ignore's comment now says why each release is skipped:
2.11.7 for caddyserver/caddy#8165 (the edge-limits drill fails its case
2 there; 2.11.4 and 2.11.6 end the body at read_body), and 2.11.6 for
caddyserver/caddy#8101 (a crash reading an HTTP/2 request body after
its handler returns, fixed in 2.11.7 by caddyserver/caddy#8107). That
`versions` matches the suffixed tags (2.11.7-alpine,
2.11.7-builder-alpine) rests on dependabot-core's source, not its
documentation, which says only "Bundler version syntax": at d261cc79,
docker/lib/dependabot/docker/tag.rb `numeric_version` drops the
`-[a-z]+` suffixes, and update_checker.rb `filter_ignored` rejects a
tag whose numeric version satisfies an ignore requirement. The pair
check above is the guard either way.

EVIDENCE:
- The RUN line on the 2.11.4 pair, measured on 2026-10-21 on an export
  whose Dockerfile.caddy differs from this one only in the comment's
  wording (sealed DOCKER_CONFIG, no push, a throwaway tag): the build
  exits 0, and the image's CADDY_VERSION and the first field of its
  `caddy version` are both v2.11.4. With the compile stage on the
  official 2.11.3 compile image, pinned by its digest (3eae6b35), and
  the runtime kept, the build fails at the new RUN line (exit 1). The
  edge-limits drill on the built image exits 0: the slow body is ended
  after 31.1 s while a GET is answered in 0.1 s, a 2 MiB body gets 413,
  and trickled headers are ended after 10.0 s. CI's caddy-edge job
  builds this tree and runs the drill again.

Qualifying reason: THREAT-MODEL (docs/RED-TEAM-SCOPE.md l.287, "to
substitute a base image": an update that moved the runtime base to
another Caddy release than the one compiled passed the build; with the
compile stage on 2.11.3 and the runtime on 2.11.4 the build now fails
at the new RUN line). The digests and the comment are housekeeping.

Signed-off-by: Khaklin Technologies <PolarisID@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Idle request-body deadlines outlive their handler panic: runtime error: invalid memory address or nil pointer dereference

4 participants