caddy: lb_retries apparently not working

I’d like to be able to recreate/restart Docker services that are hooked into Caddy via reverse_proxy without downtime. Usually a (POST) request takes about 10 seconds to complete. My config is super simple:

example.com {
        log {
                format console
                output stdout
        }

        root * /srv

        file_server {
                precompressed br gzip
        }

        reverse_proxy /-/proxy1 http://docker_container1:9292 {
                lb_retries 3
                lb_try_interval 1s
        }
        reverse_proxy /-/proxy2 http://docker_container2:9292 {
                lb_retries 3
                lb_try_interval 1s
        }
}

When running a bunch of requests and restarting the container before the responses were returned, I always get 502 errors, despite the lb_retries directive.

The error messages look like this:

2024/04/21 12:35:27.404 ERROR   http.log.access.log0    handled request {"request": {"remote_ip": "62.153.218.40", "remote_port": "44362", "client_ip": "62.153.218.40", "proto": "HTTP/1.1", "method": "POST", "host": "example.com", "uri": "/-/proxy1", "headers": {"Accept-Encoding": ["gzip, compress, deflate, br"], "Connection": ["keep-alive"], "Accept": ["application/json, text/plain, */*"], "Content-Type": ["application/json"], "User-Agent": ["axios/1.6.7"], "Content-Length": ["67993"]}, "tls": {"resumed": true, "version": 772, "cipher_suite": 4865, "proto": "", "server_name": "example.com"}}, "bytes_read": 0, "user_id": "", "duration": 1.014601761, "size": 0, "status": 502, "resp_headers": {"Server": ["Caddy"], "Alt-Svc": ["h3=\":443\"; ma=2592000"]}}

{"level":"error","ts":1713702927.4045582,"logger":"http.log.error.log0","msg":"readfrom tcp 172.18.0.2:34872->172.18.0.3:9292: http: invalid Read on closed Body","request":{"remote_ip":"62.153.218.40","remote_port":"44362","client_ip":"62.153.218.40","proto":"HTTP/1.1","method":"POST","host":"example.com","uri":"/-/proxy1","headers":{"Accept-Encoding":["gzip, compress, deflate, br"],"Connection":["keep-alive"],"Accept":["application/json, text/plain, */*"],"Content-Type":["application/json"],"User-Agent":["axios/1.6.7"],"Content-Length":["67993"]},"tls":{"resumed":true,"version":772,"cipher_suite":4865,"proto":"","server_name":"example.com"}},"duration":1.014601761,"status":502,"err_id":"cr390dupf","err_trace":"reverseproxy.statusError (reverseproxy.go:1267)"}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 32 (16 by maintainers)

Most upvoted comments

@gottlike No need to wait, please feel free to download our pre-compiled binaries from CI artifacts: https://github.com/caddyserver/caddy/actions/runs/8808836194#artifacts 😁

Edit: Lightning Mohammed beat me to it again!

Thanks guys! With master it works 🥳

@gottlike Oh, I am using master, good point.

We’re hoping to tag 2.8 (beta) this week, can you try master? That should work!

Damn, and I omitted the warning so nobody would notice 😋

How can we debug this further? I edited my post and added the Caddy logs, maybe that helps to shed a light?

Yeah, connection failures (before the HTTP request is written upstream) is always allowed to retry, because we know it’s always safe. The lb_retry_match is for enabling retries other than just GET if it fails after connecting.

So you want to not retry even connection problems? But why? I can’t think of any reason why that would be helpful.

Okay, so the “always retry, no matter what lb_retry_match says”-behaviour only happens if Caddy was never able to initially connect to the upstream? That would be safe then, I guess. I figured it would also do that if it did connect initially and the upstream connection broke while processing the request. If that’s not the case and the lb_retry_match is taken into account in those cases, that’s fine.

And 1 second afterwards caddy respond --listen 127.0.0.1:1235 'Hi there!' in another tab.

Aaand you are right about the dual “Server: Caddy” response. That’s because I use Caddy as a reverse proxy, obviously - sorry.

Yeah, exactly, you’re running Caddy behind Caddy, so of course it’ll have two copies of the Server: Caddy header. That’s working as intended. Each server adds their own Server header.

I would expect to receive a 502 error with curl, even though the proxy went up after a second. That’s because the request didn’t have “X-Idempotent-Retry: true” as a header.

So you want to not retry even connection problems? But why? I can’t think of any reason why that would be helpful.

The logic for retries is here:

https://github.com/caddyserver/caddy/blob/6a0299905479083f250c39d5780c305f82863273/modules/caddyhttp/reverseproxy/reverseproxy.go#L1013-L1071

Actually, just following up with d93e027e01905406671ed8df295d5e48d16645c2 which also preserves the body in case one of the backends partially (or totally) depletes it before having the connection severed.

I got the email. Thank you for reaching out!

While investigating this, I realized this should already work:

:1234 {
	reverse_proxy 127.0.0.1:1235 {
		# same as the OP's original config
		lb_retries 3
		lb_try_interval 1s

		# only retry request if the app will treat it as idempotent
		# (GETs are considered idempotent by default, POSTs aren't)
		lb_retry_match header Idempotency-Key *

		# enable buffering sufficient to fit the request body
		request_buffers 128K

		# optional, just to see what's going on
		verbose_logs
	}
}

I start the proxy, and then I fire off a request:

$ curl -v "http://localhost:1234" -H 'Idempotency-Key: asdf' -d 'foobar'

and then quickly, before the load balancer gives up, I bring up the simulated backend:

$ caddy respond --listen 127.0.0.1:1235 'Request body: {http.request.body}'

But I was getting readFrom errors, because the request body was being closed… but it was definitely being fully read into the buffer! So closing the request body shouldn’t matter.

Anyway, all I had to do was patch half a line of code: 613d544a4775b831595f223028fee76b980aa003

Seems to work now.

See https://caddyserver.com/sponsor, you can reach out that way to get a quote.