Trace Unclosed Socket / File Descriptor Leaks
Abandoning open sockets or file descriptors without calling .close() causes resource leaks that trigger OSError: [Errno 24] Too many open files. While Python may close them during garbage collection, relying on delayed cleanup exhausts OS descriptors under heavy load.
import socket
from contextlib import contextmanager
@contextmanager
def managed_socket():
s = socket.socket()
try:
yield s
finally:
s.close()
# Trace Unclosed Socket / File Descriptor Leaks When your web crawler or microservice opens 1,000 HTTP sockets every minute, forgetting to call `.close()` or omit `with` blocks creates a ticking time bomb. The OS assigns each open socket an integer file descriptor (FD). The default Linux limit is often 1024. 🛑💣 Once your process hits that limit, every subsequent `socket()`, `open()`, and `accept()` call explodes with `OSError: [Errno 24] Too many open files`. Even worse, relying on garbage collection to close them is fatal because GC runs non-deterministically. ```python import socket from contextlib import contextmanager @contextmanager def managed_socket(*args, **kwargs): sock = socket.socket(*args, **kwargs) try: yield sock finally: sock.close() ``` ## Pop Quiz Why does relying on Python's garbage collector to close abandoned sockets cause production outages? - [ ] GC runs non-deterministically; under heavy traffic, the OS descriptor table fills up long before GC triggers. - [ ] The CPython interpreter crashes with SIGABRT if any socket is reclaimed by reference counting. - [ ] Closing sockets via garbage collection leaves TCP SYN packets in a permanent kernel loop. - [ ] Garbage-collected sockets leak their IP addresses to other processes on the same host. ## Real-World Challenge Implement `SafeSocketPair()` as a context manager that generates a connected socket pair (`socket.socketpair()`). Ensure that both sockets are closed deterministically in the `finally` block even if an exception is raised inside the `with` block. <details> <summary>Solution</summary> ```python import socket from contextlib import contextmanager @contextmanager def SafeSocketPair(): sock_a, sock_b = socket.socketpair() try: yield sock_a, sock_b finally: sock_a.close() sock_b.close() # Verification a_ref = None b_ref = None try: with SafeSocketPair() as (sock1, sock2): a_ref = sock1 b_ref = sock2 sock1.sendall(b"PING") msg = sock2.recv(4) assert msg == b"PING" raise ValueError("Simulated unexpected failure inside transaction") except ValueError: pass # Verify both sockets were cleanly closed assert a_ref.fileno() == -1 assert b_ref.fileno() == -1 ``` </details>