Menu

Your SMTP connection pool is probably leaking
📰

Your SMTP connection pool is probably leaking

DEV Community·Volanea·13 days ago
#nYgYpuuZ
#dev#connection#connections#send#close
Reading 0:00
15s threshold

A worker opens a connection, sends, and forgets to close it. Nothing breaks for a week, then the provider starts refusing new connections and every send fails at once.

Connections are the scarce resource

Providers cap concurrent connections per account and per IP, often at a low number. A pool that grows because closed connections are not returned will hit that cap, and the failure mode is total rather than gradual: once the cap is reached, every new send blocks.

Where the leak comes from

An exception between MAIL FROM and QUIT skips the close. A timeout on DATA leaves the socket open until the OS reaps it. A retry path that creates a fresh connection without releasing the failed one doubles the count. None of these show up in a happy-path test.

The fix

Wrap the whole conversation in a context manager that guarantees QUIT, and on any exception close the socket rather than returning it to the pool — a connection in an unknown protocol state is worse than no connection.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.