Fix: Close SMTP connections opened in worker threads in _send_bulk()#526
Open
simonphilips wants to merge 1 commit into
Open
Fix: Close SMTP connections opened in worker threads in _send_bulk()#526simonphilips wants to merge 1 commit into
simonphilips wants to merge 1 commit into
Conversation
Author
|
I just missed that there's already another PR for it but it's a bit cryptic and does some things that aren't needed (like the locking, dicts are thread-safe even without the GIL). |
When sending queued mail, _send_bulk runs each send in a ThreadPool worker that opens its own connection in thread-local storage. The main thread's connections.close() can't reach those, so the sockets were leaked and only cleaned up at garbage collection. To solve this, we collect the worker connections and close them once the pool has finished. This keeps re-using one connection per worker thread for the whole batch.
25cdfad to
42b32c1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When sending queued mail,
_send_bulkruns each send in aThreadPoolworker that opens its own connection in thread-local storage. The main thread'sconnections.close()can't access those, so the sockets were leaked and only cleaned up at garbage collection (which triggered a lot of warnings for us).To solve this, collect the worker connections and close them once the pool has finished. This keeps re-using one connection per worker thread for the whole batch.