Timeouts
coreintermediateA timeout is the maximum time you let a call to another service run before giving up and raising an error yourself. Without one, a hung downstream service hangs your service too — forever, not just slowly.
Think of it as
A timeout is a deadline you set, not one the other side agrees to. You are not asking the downstream service to hurry — you are deciding how long you personally are willing to wait before treating silence as a failure and moving on.
What we're doing: Set a short socket timeout against an address that will not respond, and confirm the call fails fast with socket.timeout instead of hanging.
- 4
- settimeout(0.001) caps EVERY blocking socket call — connect, recv — at 1 millisecond.
- 6
- connect() to a non-routable address never gets a response — without a timeout this line blocks indefinitely.
- 8
- The timeout fires as socket.timeout, a normal Python exception the caller can catch and handle.
socket.timeout: timed outWhy this works: The connect() call has no way to know the destination will never answer — 10.255.255.1 is a non-routable address chosen specifically so nothing responds. settimeout(0.001) is what turns that unknown, unbounded wait into a concrete, catchable exception after 1 millisecond, instead of the caller hanging with no way to know whether the call is still in progress or already dead.
Using a client with no timeout at all
Wrong
Better
What you see: A request thread stays blocked for minutes or hours against a hung downstream, tying up a worker/connection that could be serving other requests — often the actual cause of a cascading outage, not the original slow service alone.
Why: httpx.Client() with no timeout argument waits forever by design — there is no built-in ceiling. One slow or hung downstream then holds your resources hostage indefinitely, which is how a single struggling service takes an entire caller down with it.
- Call starts — deadline set, e.g. 5s
- No response by deadline — downstream may be alive or dead — unknown
- Timeout fires — raises an exception NOW, not eventually
Timeout knobs on Python's common HTTP clients
Together
Remember: Every network call needs an explicit timeout — no timeout means "wait forever," and a hung downstream then hangs you too.
See also: retries backoff and jitter · circuit breakers · connection pooling for resilience

