pymongo.errors.ServerSelectionTimeoutError: connection closed, Timeout: 30s
ID: database/mongodb-connection-pool-timeout
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| MongoDB 5.0 | active | — | — | — |
| MongoDB 6.0 | active | — | — | — |
| MongoDB 7.0 | active | — | — | — |
Root Cause
MongoDB driver's server selection timeout occurs when all connections in the pool are closed or exhausted, often due to network interruptions, server overload, or connection pool misconfiguration in the application.
generic中文
当连接池中的所有连接都已关闭或耗尽时,MongoDB 驱动程序的服务器选择超时发生,通常由网络中断、服务器过载或应用程序中的连接池配置错误引起。
Official Documentation
https://www.mongodb.com/docs/drivers/pymongo/Workarounds
-
85% success Set MongoClient options: client = MongoClient('mongodb://localhost:27017', maxPoolSize=50, minPoolSize=10, retryWrites=True, retryReads=True). This ensures connections are maintained and operations retry on failure.
Set MongoClient options: client = MongoClient('mongodb://localhost:27017', maxPoolSize=50, minPoolSize=10, retryWrites=True, retryReads=True). This ensures connections are maintained and operations retry on failure. -
80% success Use MongoDB drivers' built-in heartbeat monitoring: MongoClient('mongodb://localhost:27017', heartbeatFrequencyMS=10000). This detects closed connections and reconnects automatically.
Use MongoDB drivers' built-in heartbeat monitoring: MongoClient('mongodb://localhost:27017', heartbeatFrequencyMS=10000). This detects closed connections and reconnects automatically. -
90% success Wrap database calls in a retry decorator: import time; from pymongo.errors import ServerSelectionTimeoutError; def retry_on_timeout(func): for attempt in range(3): try: return func() except ServerSelectionTimeoutError: time.sleep(2**attempt); raise.
Wrap database calls in a retry decorator: import time; from pymongo.errors import ServerSelectionTimeoutError; def retry_on_timeout(func): for attempt in range(3): try: return func() except ServerSelectionTimeoutError: time.sleep(2**attempt); raise.
中文步骤
Set MongoClient options: client = MongoClient('mongodb://localhost:27017', maxPoolSize=50, minPoolSize=10, retryWrites=True, retryReads=True). This ensures connections are maintained and operations retry on failure.Use MongoDB drivers' built-in heartbeat monitoring: MongoClient('mongodb://localhost:27017', heartbeatFrequencyMS=10000). This detects closed connections and reconnects automatically.Wrap database calls in a retry decorator: import time; from pymongo.errors import ServerSelectionTimeoutError; def retry_on_timeout(func): for attempt in range(3): try: return func() except ServerSelectionTimeoutError: time.sleep(2**attempt); raise.
Dead Ends
Common approaches that don't work:
-
90% fail
This only delays failure; if connections are genuinely closed, the timeout will eventually trigger anyway, and the application remains unresponsive longer.
-
95% fail
The same configuration or network problem persists; the error will reappear shortly after restart.
-
70% fail
This prevents connection reuse, causing new connections for every operation, which increases latency and may exhaust server resources.