Fix connection leaks in broker and transport access#1490
Open
ShubhAtWork wants to merge 1 commit intomher:masterfrom
Open
Fix connection leaks in broker and transport access#1490ShubhAtWork wants to merge 1 commit intomher:masterfrom
ShubhAtWork wants to merge 1 commit intomher:masterfrom
Conversation
Several places call `capp.connection()` without using a context manager, creating AMQP/Redis connections that are never closed. Each leaked connection holds open a socket and file descriptor. Under sustained use (e.g. the Broker view refreshing, or the `/api/queues/length` endpoint being polled by monitoring), this causes: - Gradual file descriptor exhaustion (hitting OS `ulimit`) - Socket leak warnings from the broker client library - Eventual `Too many open files` errors that crash the process Specific leaks fixed: 1. **`Flower.transport` property** — called on every request that checks the broker type. Opens a connection, reads the transport driver type, then discards the connection object without closing. Fixed by caching the result (transport type never changes at runtime) and using a context manager for the initial lookup. 2. **`print_banner()`** — `app.connection().as_uri()` opens a connection to format the broker URL for the startup log. Never closed. Fixed with a context manager. 3. **`BrokerView.get()`** — two separate `app.capp.connection()` calls: one to create the Broker object, another to get the display URL. Neither closed. Fixed by using a single context-managed connection for both. 4. **`GetQueueLengths.get()`** — `app.capp.connection().as_uri()` opens a connection to get the broker URI. Never closed. Fixed with a context manager. 5. **`WorkersView.get()`** — `self.application.capp.connection().as_uri()` opens a connection on every non-JSON page render. Never closed. Fixed with a context manager. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
5 tasks
Owner
|
I suggest doing something like this instead |
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.
Summary
Several places call
capp.connection()without using a context manager, creating AMQP/Redis connections that are never closed. Each leaked connection holds open a socket and file descriptor. Under sustained use (e.g. the Broker view refreshing, or/api/queues/lengthbeing polled by monitoring), this causes gradual file descriptor exhaustion, eventually leading toToo many open fileserrors that crash the process.Leaks fixed
Flower.transportpropertyprint_banner()app.connection().as_uri()opens a connection to format the broker URL for the startup log. Never closed.BrokerView.get()capp.connection()calls — one for Broker creation, one for display URL. Neither closed.GetQueueLengths.get()capp.connection().as_uri()opens a connection to get broker URI. Never closed.WorkersView.get()capp.connection().as_uri()opens a connection on every non-JSON page render. Never closed.Test plan
lsof -p <pid> | wc -l)🤖 Generated with Claude Code