-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Fix Django worker recursion bug + defensive checks for pool_cls.__module__ #10048
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
Fix Django worker recursion bug + defensive checks for pool_cls.__module__ #10048
Conversation
- Remove worker parameter from DjangoWorkerFixup.__init__ - Set worker via on_worker_init callback instead - Add None check for worker.pool_cls to prevent AttributeError - Add regression test to prevent future recursion bugs Fixes recursive instantiation issue where DjangoWorkerFixup(app) would create WorkController(app), which in turn would create another DjangoWorkerFixup, leading to infinite recursion. The worker instance is now properly set via the on_worker_init signal callback, avoiding the circular dependency.
Extends the fix from celery#10045 by adding defensive checks for when pool_cls is a string instead of a class in two additional locations: - celery/contrib/testing/worker.py: TestWorkController.__init__ - celery/worker/components.py: Beat.create This prevents AttributeError: 'str' object has no attribute '__module__' when pool_cls is passed as a string (e.g., 'prefork', 'gevent', 'eventlet'). Also adds unit tests to verify the defensive checks work correctly.
for more information, see https://pre-commit.ci
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.
please check the unit test failures
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10048 +/- ##
==========================================
+ Coverage 88.43% 88.47% +0.04%
==========================================
Files 153 153
Lines 19373 19375 +2
Branches 2228 2228
==========================================
+ Hits 17132 17142 +10
+ Misses 1941 1934 -7
+ Partials 300 299 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
may be we can consider the remaining changes here as minor improvements |
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 extends #10045 to add defensive checks for accessing pool_cls.__module__ in two additional locations throughout the codebase, preventing AttributeError when pool_cls is a string instead of a class object.
Key Changes:
- Added defensive checks in
Beat.createandTestWorkController.__init__to handle stringpool_clsvalues - Added two unit tests for
Beat.createto verify the fix works with string pool types - Applies the same pattern used in the Django fixup to prevent AttributeErrors
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
celery/worker/components.py |
Added defensive check in Beat.create to handle string pool_cls before checking for green pools |
celery/contrib/testing/worker.py |
Added defensive check in TestWorkController.__init__ to handle string pool_cls before checking for prefork pool |
t/unit/worker/test_components.py |
Added two unit tests verifying Beat.create raises ImproperlyConfigured when pool_cls is string 'gevent' or 'eventlet' |
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.
is it possible to improve test coverage?
|
@auvipy Added unit tests for |
|
can you please check why the tests are failing with new changes? |
Add tests to verify defensive handling of string pool_cls in TestWorkController.__init__, addressing reviewer feedback.
a7d89b7 to
34882eb
Compare
|
@auvipy Fixed a test pollution issue discovered during local CI testing. Root cause: The Fix: Added Note: Force pushed to amend the previous commit with this fix. Apologies for not creating a separate commit. |
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
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Description
This PR builds on top of #10045 by @bruunotrindade to add defensive checks for
pool_cls.__module__access in additional locations throughout the codebase.Root Cause (from #10045)
DjangoWorkerFixup.__init__was creating aWorkControllerinstance via theworkerorWorkController(app)pattern. This created a circular dependency where:WorkController.__init__triggers theworker_initsignalDjangoFixup.on_worker_initcreates/accessesDjangoWorkerFixupDjangoWorkerFixup.__init__tries to create anotherWorkControllerAdditionally,
pool_clscan be a string (e.g.,'prefork','solo','gevent') instead of a class object, which causesAttributeError: 'str' object has no attribute '__module__'when trying to accesspool_cls.__module__.Changes in this PR
This PR includes all changes from #10045 plus:
celery/contrib/testing/worker.py: Added defensive check forpool_clsbeing a string inTestWorkController.__init__celery/worker/components.py: Added defensive check forpool_clsbeing a string inBeat.createNew unit tests: Added tests to verify the defensive checks work correctly when
pool_clsis a stringPattern Applied
Testing
Beat.createwith string pool_clsRelated Issues
Acknowledgments
Thanks to @bruunotrindade for identifying and fixing the root cause in #10045. This PR extends that fix to cover additional edge cases.