OSPython Error

TimeoutError

TimeoutError: [Errno 110] Connection timed out

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    s.connect(("192.0.2.1", 80))  # may hang forever
TimeoutError: [Errno 110] Connection timed out

What causes this error

A network operation exceeded its time limit without receiving a response. The remote host may be unreachable, overloaded, or a firewall may be silently dropping packets.

How to fix it

Set explicit timeouts on all network operations. Implement retry logic with exponential backoff. Consider using async I/O for better timeout management. Check network connectivity and DNS resolution.

Code that causes this error

Broken
import socket
s = socket.socket()
s.connect(("192.0.2.1", 80))  # may hang forever

Fixed code

Fixed
import socket

s = socket.socket()
s.settimeout(5)  # 5-second timeout
try:
    s.connect(("192.0.2.1", 80))
except TimeoutError:
    print("Connection timed out")
finally:
    s.close()

About TimeoutError

TimeoutError is raised when a system function times out at the operating system level. It is a subclass of OSError and corresponds to the POSIX errno ETIMEDOUT. This error appears in socket operations when a connection or data transfer takes too long, and in asyncio when an `asyncio.wait_for()` call exceeds its timeout.

It is important to distinguish from socket.timeout (which is an alias for TimeoutError since Python 3.3) and various library-specific timeouts (like requests.Timeout). TimeoutError indicates that the remote host did not respond within the allotted time, which could be due to network congestion, routing issues, firewall rules silently dropping packets, or the remote service being overloaded. Setting appropriate timeout values and implementing retry logic are essential practices for robust network code.

Common scenarios

1

Running scripts without sufficient file system permissions

2

Connecting to servers that are not running or are unreachable

3

Installing Python packages without proper environment setup

4

Running out of disk space or file descriptors during I/O operations

Related errors