shaare.it

Connection pool leaked

7 Dec 2025

1 min read

Connection pool leaked

$ python -c "import requests; [requests.Session().get('https://example.com') for _ in range(100)]"
# Resource warnings or exhaustion

Why this happens

Creating many sessions without closing keeps sockets open and exhausts resources.

Fix

  • Reuse a single Session and close it when done.

Wrong code

import requests
for _ in range(100):
    requests.Session().get("https://example.com")

Fixed code

import requests
with requests.Session() as s:
    for _ in range(100):
        s.get("https://example.com")