-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Replace DelayedDelivery connection creation to use context manger #9793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace DelayedDelivery connection creation to use context manger #9793
Conversation
auvipy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code changes are making the CI fail. can you please add/update tests to verify this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR updates the delayed delivery setup to open the broker connection using a context manager, ensuring it’s closed automatically when the step completes.
- Replaced manual
connection_for_writeassignment with awithblock for automatic closing. - Scoped all delayed-delivery setup logic within the context manager.
- Resolves issue #9792 by preventing orphaned connections.
Comments suppressed due to low confidence (1)
celery/worker/consumer/delayed_delivery.py:117
- Add a unit test that mocks
connection_for_writeand asserts thatconnection.close()is called when_setup_delayed_deliverycompletes to verify the new context-manager behavior.
with c.app.connection_for_write(url=broker_url) as connection:
…text manager, replaced those places with MagicMock instead
Verified locally by running - |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9793 +/- ##
=======================================
Coverage 78.62% 78.62%
=======================================
Files 153 153
Lines 19178 19178
Branches 2540 2540
=======================================
Hits 15078 15078
Misses 3809 3809
Partials 291 291
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
…k connection context was used
Nusnus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My last change here was mainly adding better errors, but this is a much welcome improvement.
Let's see how the CI is doing and move forward, looking good in general
auvipy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thats good now

Description
DelayedDeliverystep connection creation to use context manager, so the the connection will be closed when the step is completed.Fixing an issue I found - #9792.
Verifications
Running Celery with my fix, created the exchange and queues needed with binding, and closed the connection.
Logs -
Rabbit connections - only 1 left as expected, for consuming tasks -
Rabbit channels - only 1 channel -
Exchanges were created successfully -
Queues were created successfully -