Repository navigation
Don't override explicitly set configuration with env variables - #9089
soceanainn wants to merge 5 commits into
Conversation
| os.environ.get('CELERY_BROKER_WRITE_URL') or | ||
| self.get('broker_write_url') or | ||
| os.environ.get('CELERY_BROKER_WRITE_URL') or | ||
| self.broker_url |
There was a problem hiding this comment.
This behaviour for broker_read_url / broker_write_url may be surprising, as I think it's less clear cut whether an explicitly defined broker_url should be used ahead of specific env vars for read/write URLs.
In this case, I think that env vars for read/write URLs are probably more specific than explicit setting for broker_url, so I set the priority accordingly.
Happy to adjust if necessary
There was a problem hiding this comment.
@soceanainn Thank you for your contribution.
I think this might however break a lot of Django projects with stray environment variables, so this should be announced in some change log I think, not sure if it needs to be part of this PR.
And also we should make this super explicit in the docs.
|
@auvipy tagging you here as you had volunteered to review any contributions to the original issue (although I understand that was 18 months ago) |
|
All the tests are failing. Can you check? |
|
So it seems that test_custom_remote_command[control-custom_cmd0] FAILED and then it continued running test_custom_remote_command[control-custom_cmd1] until the 30 minute timeout |
|
Okay I see the issue in tests now:
|
3eb616c to
ef5eef5
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #9089 +/- ##
=======================================
Coverage 78.02% 78.02%
=======================================
Files 150 150
Lines 18840 18840
Branches 3225 3225
=======================================
Hits 14699 14699
Misses 3835 3835
Partials 306 306
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
auvipy
left a comment
There was a problem hiding this comment.
would you mind adding new tests for the proposed changes? My understanding so far is that you are trying to change the precedence mostly?
ef5eef5 to
816fa88
Compare
816fa88 to
151c5fc
Compare
| _INSPECT_OPTIONS = ['--timeout', '0'] # Avoid waiting for the zero workers to reply | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) |
There was a problem hiding this comment.
why removing this test? and why no new one?
151c5fc to
7523d45
Compare
auvipy
left a comment
There was a problem hiding this comment.
can't add / consider this without additional tests
There was a problem hiding this comment.
Pull request overview
This PR aims to ensure explicitly configured Celery app settings (e.g., broker_url, result_backend) are not overridden by legacy CELERY_* environment variables, addressing the surprising precedence described in #4284.
Changes:
- Change
Settings.broker_url/Settings.result_backend(and read/write variants) to prefer app config overCELERY_*env vars. - Stop the CLI from exporting
CELERY_BROKER_URL/CELERY_RESULT_BACKENDintoos.environ, and instead apply CLI overrides via app pre-configuration. - Remove a test fixture that was clearing
os.environintest_control.py.
Reviewed changes
Copilot reviewed 2 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| t/unit/bin/test_control.py | Removes env-clearing fixture (but currently also drops a needed patch import). |
| celery/bin/celery.py | Switches CLI broker/backend overrides from env vars to app preconf via _autoset. |
| celery/app/utils.py | Flips precedence to prefer explicit app config over CELERY_* env vars. |
| celery/app/base.py | Exposes _autoset (renaming from __autoset) to support CLI-driven preconf updates. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| self._preconf = changes or {} | ||
| self._preconf_set_by_auto = set() | ||
| self.__autoset('broker_url', broker) | ||
| self.__autoset('result_backend', backend) | ||
| self.__autoset('include', include) | ||
| self._autoset('broker_url', broker) | ||
| self._autoset('result_backend', backend) | ||
| self._autoset('include', include) | ||
|
|
||
| for key, value in kwargs.items(): | ||
| self.__autoset(key, value) | ||
| self._autoset(key, value) |
| @property | ||
| def broker_url(self): | ||
| return ( | ||
| os.environ.get('CELERY_BROKER_URL') or | ||
| self.first('broker_url', 'broker_host') | ||
| self.first('broker_url', 'broker_host') or | ||
| os.environ.get('CELERY_BROKER_URL') | ||
| ) |
Note: Before submitting this pull request, please review our contributing
guidelines.
Description
Fixes #4284.
To give a concrete example where this is causing issues:
CELERY_BROKER_URLenv variable, instead of the specific value configured for the Celery app.I believe that this behaviour is extremely surprising for developers (that explicitly defined settings are being overwritten by env variables).