Skip to content

v2.11.7: HTTP/1.1 client gets no response, and its connection stays open, after read_body / read_body_idle expires in reverse_proxy (regression from 2.11.6) #8165

Closed

Description

@RublerubitZ

Summary

On v2.11.7, an HTTP/1.1 request body proxied by reverse_proxy can hit read_body (or read_body_idle with a minimum rate) while less than 256 KiB of it remains unread. When that happens, Caddy correctly aborts the upstream request at the deadline. However, it does not answer the client:

  • If the client keeps trickling the body, the error response is only written after the client has sent the entire remainder.
  • If the client stops sending, the connection stays open with no response. We waited 60 s.

On v2.11.4 and v2.11.6, and over HTTP/2 on v2.11.7, the client gets 502/504 at the deadline.

Environment

  • Official Docker images caddy:2.11.4-alpine, caddy:2.11.6-alpine, caddy:2.11.7-alpine (caddy version → v2.11.4 / v2.11.6 / v2.11.7)
  • Docker Desktop on macOS (arm64); client curl and Python over https://localhost with tls internal

Reproduction

Caddyfile:

{
	servers {
		timeouts {
			read_body 5s
		}
	}
}

https://localhost:8443 {
	tls internal
	reverse_proxy upstream:8080
}

The read_body_idle rows below use read_body_idle 3s 4096 instead.

upstream.py reads the whole body before replying and logs how much it got:

import http.server
import time


class Handler(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        started = time.time()
        expected = int(self.headers.get("Content-Length", 0))
        received = 0
        outcome = "complete"
        try:
            while received < expected:
                chunk = self.rfile.read1(min(65536, expected - received))
                if not chunk:
                    outcome = "connection closed"
                    break
                received += len(chunk)
        except OSError as error:
            outcome = type(error).__name__
        print(f"upstream: {received}/{expected} bytes in {time.time() - started:.1f}s ({outcome})", flush=True)
        if outcome != "complete":
            return
        self.send_response(200)
        self.send_header("Content-Length", "0")
        self.end_headers()

    def log_message(self, *args):
        pass


http.server.ThreadingHTTPServer(("0.0.0.0", 8080), Handler).serve_forever()
docker network create caddyrepro
docker run -d --name upstream --network caddyrepro -v "$PWD/upstream.py:/upstream.py:ro" python:3.12-alpine python -u /upstream.py
docker run -d --name caddy --network caddyrepro -p 127.0.0.1:18444:8443 \
  -v "$PWD/Caddyfile:/etc/caddy/Caddyfile:ro" caddy:2.11.7-alpine

# 1) slow client: 80 KiB at 2 KiB/s (~40 s)
head -c 81920 /dev/zero > body.bin
curl -sk --http1.1 --resolve localhost:18444:127.0.0.1 --limit-rate 2k --max-time 60 \
  --data-binary @body.bin -o /dev/null \
  -w '%{http_version} status=%{http_code} uploaded=%{size_upload} time=%{time_total}s\n' \
  https://localhost:18444/upload
docker logs upstream
# 2) stalled client: announces 100 KiB, sends 10 KiB, then stops sending and waits for a response
import socket, ssl, time
ctx = ssl.create_default_context(); ctx.check_hostname = False; ctx.verify_mode = ssl.CERT_NONE
ctx.set_alpn_protocols(["http/1.1"])
s = ctx.wrap_socket(socket.create_connection(("127.0.0.1", 18444)), server_hostname="localhost")
s.sendall(b"POST /upload HTTP/1.1\r\nHost: localhost\r\nContent-Length: 102400\r\n\r\n" + b"\0" * 10240)
started = time.time(); s.settimeout(60)
try:
    print(f"after {time.time() - started:.1f}s:", s.recv(256).split(b"\r\n")[0])
except socket.timeout:
    print(f"no response after {time.time() - started:.1f}s")

Results

All runs use the Caddyfile above (read_body 5s) unless noted. "Upstream" is what upstream.py logged.

Caddy client protocol client result upstream
2.11.4 80 KiB @ 2 KiB/s HTTP/1.1 504 after 6.0 s 12 KiB, closed at 5.0 s
2.11.6 80 KiB @ 2 KiB/s HTTP/1.1 504 after 5.0–6.0 s (two runs) 10 KiB, closed at 5.0 s
2.11.7 80 KiB @ 2 KiB/s HTTP/1.1 499 after 40.1 s, after the whole 80 KiB was uploaded 10 KiB, closed at 5.0 s
2.11.7 80 KiB @ 2 KiB/s HTTP/2 502 after 6.0 s 10 KiB, closed at 5.0 s
2.11.6 stalled after 10 of 100 KiB HTTP/1.1 504 after 5.0 s 10 KiB, closed at 5.0 s
2.11.7 stalled after 10 of 100 KiB HTTP/1.1 no response after 60 s, connection open 10 KiB, closed at 5.0 s
2.11.6 1 MiB @ 20 KiB/s HTTP/1.1 504 after 5.2 s 104 KiB, closed at 5.0 s
2.11.7 1 MiB @ 20 KiB/s HTTP/1.1 499 after 5.2 s 104 KiB, closed at 5.0 s
2.11.6 80 KiB @ 2 KiB/s, read_body_idle 3s 4096 HTTP/1.1 499 after 7.0 s 14 KiB, closed at 6.5 s
2.11.7 80 KiB @ 2 KiB/s, read_body_idle 3s 4096 HTTP/1.1 no response within 25 s (curl --max-time 25), 50 KiB uploaded 12 KiB, closed at 6.0 s
2.11.7 80 KiB @ 2 KiB/s, read_body_idle 3s 4096 HTTP/2 502 after 7.0 s 12 KiB, closed at 6.0 s

The 1 MiB row is answered at the deadline on 2.11.7 too. The difference is how much of the body is still unread when the deadline hits: about 920 KiB there, versus less than 256 KiB in the other rows.

What seems to happen

This is our reading of the code. We have not tested a patch.

  1. When the proxy's read of the request body hits the deadline, IdleTimeoutReader.Read (modules/caddyhttp/idletimeout.go, changed in caddyhttp: end request-body deadline ownership with handler #8107) stores the error as terminalErr and calls clearDeadlineLocked(). That clears the connection's read deadline, even though the error is the deadline itself. HandlerDone() clears it again because terminalErr != nil.
  2. After the handler returns its error, net/http writes the response header. For HTTP/1 with an unread body, chunkWriter.writeHeader first discards the rest of the body, up to maxPostHandlerReadBytes (256 KiB): io.CopyN(io.Discard, w.reqBody, maxPostHandlerReadBytes+1) (go1.26.8 net/http/server.go). It does this so the connection can be reused.
  3. With the read deadline cleared, that discard has no deadline. It waits for the client to send the remainder before the 5xx header goes out. If the client never sends it, the connection stays open.
  4. On 2.11.6 the expired deadline stayed on the connection. The discard failed immediately, net/http set closeAfterReply, and the 504 went out at the deadline.
  5. HTTP/2 does not do this discard, and a remainder of 256 KiB or more takes the tooBig path, which skips it. That explains why those cases are unaffected.

A possible fix: do not clear the read deadline when the terminal error is a timeout (errors.Is(err, os.ErrDeadlineExceeded)), or set a deadline in the past in that case. The post-handler discard would then fail fast. The clear-on-EOF behavior that #8103 needed would stay as it is.

Expected behavior

The docs describe read_body as "a hard limit on the whole upload". They say read_body_idle with <min_rate> "stops clients that send just enough data to never stall". We expect the client to get an error response and the connection to be released at the deadline, as on 2.11.6 and over HTTP/2.

Impact

read_body / read_body_idle are the documented mitigations for slow-body clients. On 2.11.7 the upstream is protected, but over HTTP/1.1 any client can hold a Caddy connection open indefinitely without getting a response. It only needs to announce a body, send part of it, and stop.

Activity

  1. steadytao commented on Oct 8, 2026

    @steadytao
    Member

    Thank you. Preserve an expired terminal read deadline through handler return so HTTP/1's post-handler drain fails immediately; keep the existing clear-on-EOF and non-timeout behaviour. Add wire tests for stalled and trickled bodies below 256 KiB plus an HTTP/2 control.

  2. mholt commented on Oct 8, 2026

    @mholt
    Member

    Hi there, did you use AI to write this issue? We require disclosure of all AI use.

  3. added
    bug 🐞
    on Oct 8, 2026
  4. SiyXo511 commented on Oct 9, 2026

    @SiyXo511
    Contributor

    I'd like to contribute a fix for this issue. When an HTTP/1 upload times out, clearing the expired read deadline can leave net/http blocked while draining the unread request body before sending the error response.
    I've prepared a patch that preserves the expired deadline and prevents route timeout restoration from extending it. It includes regression tests for stalled and slow uploads, with HTTP/2 controls. I'll open a draft PR for review.
    OpenAI Codex assisted with the investigation, implementation, and tests. I've reviewed the changes and understand the fix.

  5. steadytao commented on Oct 9, 2026

    @steadytao
    Member

    Hi there, did you use AI to write this issue? We require disclosure of all AI use.

    They did but it is what it is for an issue 🤷‍♂️ I normally just skim the text if I see a block that requires me to scroll.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions