A backend habit I think is worth checking in any codebase: does every outbound call have a timeout? As far as I know from the docs, the Python requests library has no default timeout, and urllib blocks indefinitely unless you pass one, so a server that accepts the connection and then goes silent can hold your thread (or worker, or connection pool slot) for as long as it likes. Other languages and clients have their own defaults, so it's worth checking yours.
A small experiment to see what a timeout buys you. A local server that accepts connections and never replies:
import socket, threading, time, urllib.request
srv = socket.socket()
srv.bind(("127.0.0.1", 0))
srv.listen(5)
port = srv.getsockname()[1]
def accept_and_stay_silent():
conns = []
while True:
c, _ = srv.accept()
conns.append(c) # accept, never reply
threading.Thread(target=accept_and_stay_silent, daemon=True).start()
t = time.time()
try:
urllib.request.urlopen(f"http://127.0.0.1:{port}/", timeout=1.5)
except Exception as e:
print(type(e).__name__, e, f"after {time.time() - t:.1f}s")
Output on Python 3.13.18:
TimeoutError timed out after 1.6s
With requests 2.34.2 and a (connect, read) timeout tuple:
import requests
requests.get(f"http://127.0.0.1:{port}/", timeout=(1, 1.5))
# ReadTimeout after 1.5s
I did not run the no-timeout version, since it would simply wait. That's the point.
A "timeout budget" rule I'd suggest (opinion):
- Every outbound call gets an explicit timeout. Make it a lint rule or a thin wrapper, so no one has to remember.
- Separate connect and read timeouts where your client supports it. A connection that can't be established should fail fast, while a slow but alive response may deserve more time.
- The sum of downstream timeouts should fit inside your own deadline. If your endpoint promises an answer in 2 seconds, three sequential calls with 5-second timeouts can't keep that promise.
- Retries multiply it. Three attempts at 5 seconds is up to 15 seconds, plus backoff, so budget total time, not just per attempt.
Related things to decide:
- What the caller sees on timeout: an error, a cached value or a degraded response.
- Whether a timed-out request may have succeeded on the other side. For non-idempotent operations, a retry after a timeout can duplicate the effect.
- Whether you propagate a deadline to downstream calls instead of giving each its own independent timeout.
Limits: the experiment uses one local server and one failure mode (accept and stay silent). Real networks fail in other ways, such as DNS delays, slow TLS handshakes and slow streaming bodies, and some timeouts apply per operation rather than to the whole call. Check what your HTTP client's timeout actually covers.
Do you enforce timeouts with a wrapper, a lint rule, or convention? And how do you pick the numbers?