A path that forgets an idle connection fails the next request. With urllib3 2.8.0 a retried GET recovered, a POST did not; a pool idle limit below the timeout fixed both.
The first request after a quiet spell
Jenya's stock-sync job at Belgavi's Book Store posts updates to a supplier API every few minutes through a pooled HTTP client. During busy hours it works. After a quiet spell, the first request regularly fails with a connection reset, and the next one succeeds. The job's logs blame the supplier. The supplier sees no failed request.
The request never reached them. Somewhere on the path, a NAT gateway, a load balancer or the supplier's own server had closed the idle keep-alive connection. The client pool still held it and handed it to the next request. What the client saw then depended on how the connection had been closed, which HTTP method it was sending and how its retries were configured.
This article reproduces the cases on one machine with urllib3 2.8.0, the current release on 18 November 2026, on CPython 3.13, and gives the settings that prevent them.
A relay that forgets idle flows
The companion probe runs everything on 127.0.0.1: an HTTP/1.1 origin that keeps connections alive and counts the requests it receives, and a relay in front of it that applies a 0.6 second idle timeout per flow in one of two ways.
fin: the relay closes both sides cleanly, the way a server closes a keep-alive connection when its own idle timeout expires.rst: the relay silently forgets the flow and answers the next packet from the client with a reset, the way a NAT or load balancer that dropped its state behaves. The client sees nothing until it writes.
A PoolManager sends one GET, sleeps past the idle timeout, then sends a GET or a POST to the same host. Three runs gave identical results.
| Path behaviour | Second request | Client retries | Caller saw | Requests at the origin |
|---|---|---|---|---|
| Clean close | GET | none | 200 | 2 |
| Clean close | POST | none | 200 | 2 |
| Silent drop | GET | none | MaxRetryError caused by ProtocolError | 1 |
| Silent drop | POST | none | ProtocolError | 1 |
| Silent drop | GET | Retry(total=3) | 200 | 2 |
| Silent drop | POST | Retry(total=3) | ProtocolError | 1 |
| Silent drop | POST | none, but pool idle limit below the path timeout | 200 | 2 |
Reading the results
When the far end closes cleanly, the FIN arrives while the connection sits idle. Before reusing a pooled connection, urllib3 checks whether it has been dropped, and its utility function is_connection_dropped returns true when the connection is no longer connected. The pool discards it and opens a new one. Both the GET and the POST succeeded, each on a fresh connection.
A silent drop gives the pool nothing to detect. The connection looks healthy until the request is written and the reset comes back. Without retries, both methods fail.
With retries, the method decides. urllib3's Retry treats a failure while reading the response as a read error, and retries a read error only when the method is in DEFAULT_ALLOWED_METHODS: HEAD, GET, PUT, DELETE, OPTIONS and TRACE. The GET was retried on a new connection and succeeded, so the caller never knew. The POST was not retried and the error reached the caller, even though the origin never received it. urllib3's Retry.DEFAULT is Retry(3), so a pool created without a retries argument behaves like the retried rows.
That asymmetry follows the HTTP specification. RFC 9110 section 9.2.2 says a client should not automatically retry a request with a non-idempotent method unless it knows the request is safe to repeat or that the original was never applied. A reset after sending the request does not tell the client which. In this probe the request died at the relay; with a server that closes its idle connection at the same moment the client sends, the request may have been processed and the response lost.
Keep the pool idle limit below every timeout on the path
The last row is the structural fix. The probe closes pooled connections that have been idle longer than half the path's timeout, so the client never reuses a connection the path may have forgotten. The POST then goes out on a fresh connection and succeeds without any retry.
Most clients have this setting under some name: a maximum idle time, an idle eviction interval or a connection time-to-live. In urllib3 itself the pool has no idle timer, so long-running services either clear the pool after quiet periods, as the probe does, or use a higher-level client that evicts idle connections. Set the limit below the smallest idle timeout anywhere on the path, which means finding out what that is:
- the server's keep-alive timeout, which web servers commonly set between a few seconds and a couple of minutes;
- any load balancer or reverse proxy between you and the server;
- any NAT gateway or firewall that tracks connection state, including the one in front of your own cluster.
TCP keepalive is a second line of defence, not a substitute. On Linux the default keepalive time is two hours according to the tcp(7) manual page, far longer than typical middlebox idle timeouts. Setting a shorter per-socket keepalive interval keeps a NAT entry alive for long-lived connections, but it sends traffic on every idle connection and does nothing about a server that closes connections deliberately.
Make POST safe to retry, or detect that it was not applied
Even with good idle limits, a connection can die between the check and the write. For non-idempotent requests, give the server a way to deduplicate. An Idempotency-Key header, which the IETF HTTPAPI working group is standardising as an Internet-Draft, lets the client retry a POST with the same key and lets the server return the original result if the first attempt succeeded. With that in place you can add POST to the client's allowed retry methods for that endpoint only.
Without it, surface the error and let the caller decide, with an explicit reconciliation step: query whether the supplier has the update before resending it.
Choosing the numbers
Use the shortest idle timeout on the path as the ceiling and leave a margin for clock and timer granularity; half of it is a reasonable start. If one path has a NAT timeout of a few minutes and the server's keep-alive is longer, the NAT decides. Where you cannot learn the timeouts, measure them: hold an idle connection open for increasing periods and record when the first request fails.
Verification
- Reproduce the path in a test, as the probe does, rather than waiting for production idle periods. A relay with a configurable idle timeout and
finorrstbehaviour is a few dozen lines. - Log connection reuse and connection age with each request failure. A reset on a connection idle for longer than some round number of seconds points straight at a timeout on the path.
- Alert on the first-request-after-idle failure pattern, not just on error rate, which hides it in busy periods.
- After changing idle limits, rerun the test with the real timeout values.
Failure modes
- Retrying all methods globally to make the errors go away, which turns a lost POST into a duplicate one.
- An idle limit set below the server timeout but above the NAT timeout in another environment.
- Health checks that keep one pooled connection busy, so the pool reuses the healthy one while other connections age silently.
- Connection pools shared across threads where one thread's long idle period is invisible to the others.
- Proxies that keep the client side open while dropping the upstream connection, so the error appears as a 502 from the proxy instead of a reset.