Skip to content

Fix: Close SMTP connections opened in worker threads in _send_bulk()#526

Open
simonphilips wants to merge 1 commit into
ui:masterfrom
simonphilips:fix/send-bulk-connection-leak
Open

Fix: Close SMTP connections opened in worker threads in _send_bulk()#526
simonphilips wants to merge 1 commit into
ui:masterfrom
simonphilips:fix/send-bulk-connection-leak

Conversation

@simonphilips

Copy link
Copy Markdown

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 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.

@simonphilips

simonphilips commented Jun 30, 2026

Copy link
Copy Markdown
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.
@simonphilips simonphilips force-pushed the fix/send-bulk-connection-leak branch from 25cdfad to 42b32c1 Compare June 30, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant