TimeoutError
PythonERRORI/O ErrorHIGH confidence

Operation timed out

What this means

A subclass of `OSError`, raised when a system function that has a time limit (like a network request or IPC call) expires without completing.

Why it happens
  1. 1A remote server failed to respond to a network request within the specified timeout.
  2. 2A subprocess or parallel task did not complete its work in the allotted time.
  3. 3A resource (like a lock) could not be acquired within the timeout period.
How to reproduce

This error is triggered when a network connection attempt does not receive a response within the timeout duration.

trigger — this will error
trigger — this will error
import socket

# 192.0.2.1 is a non-routable IP address, so this will time out.
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2) # Set a 2-second timeout
    s.connect(('192.0.2.1', 80)) 
except TimeoutError as e:
    print(f"Caught TimeoutError: {e}")

expected output

Caught TimeoutError: [WinError 10060] A connection attempt failed...

Fix 1

Increase the timeout duration

WHEN The operation is valid but simply needs more time to complete.

Increase the timeout duration
import requests
try:
    # Increase timeout from default to 30 seconds
    response = requests.get("https://slow.example.com", timeout=30)
except requests.exceptions.Timeout:
    print("Request timed out after 30 seconds.")

Why this works

By providing a longer timeout, you give the slow operation more time to finish before the client gives up.

Fix 2

Implement a retry mechanism

WHEN Timeouts are intermittent and likely to succeed on a second try (e.g., due to transient network issues).

Implement a retry mechanism
import requests
import time
for i in range(3): # Try up to 3 times
    try:
        response = requests.get("https://example.com", timeout=5)
        break # Success, exit loop
    except requests.exceptions.Timeout:
        print(f"Attempt {i+1} timed out, retrying...")
        time.sleep(2) # Wait before retrying

Why this works

Wrapping the operation in a loop with a `try...except` block allows you to catch the timeout and attempt the same operation again, which can overcome temporary failures.

Code examples
Triggerpython
import socket
s = socket.socket()
s.settimeout(0.001)
s.connect(("1.2.3.4", 80))  # TimeoutError
Handle with try/exceptpython
try:
    response = requests.get(url, timeout=5)
except TimeoutError:
    print("Request timed out")
Avoid with retry and backoffpython
import time, requests
for i in range(3):
    try:
        return requests.get(url, timeout=10)
    except TimeoutError:
        time.sleep(2 ** i)
What not to do

Setting an infinitely long or extremely large timeout

This can cause your application to hang indefinitely, waiting for a response that will never come. It's better to have a reasonable timeout and handle the error.

Version notes

Sources
Official documentation ↗

cpython/Objects/exceptions.c

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors