Skip to content

Don't override explicitly set configuration with env variables - #9089

Open
soceanainn wants to merge 5 commits into
celery:mainfrom
soceanainn:soceanainn/dont-override-explicit-config-with-env-vars
Open

soceanainn wants to merge 5 commits into
celery:mainfrom
soceanainn:soceanainn/dont-override-explicit-config-with-env-vars

Conversation

@soceanainn

Copy link
Copy Markdown

Note: Before submitting this pull request, please review our contributing
guidelines
.

Description

Fixes #4284.

To give a concrete example where this is causing issues:

  1. My organisation runs a large Python monolithic backend with many Celery apps/workers (which currently all sharing a single RabbitMQ broker).
  2. We recently started work to split our most critical use cases into separate isolated Rabbit MQ brokers for better isolation.
  3. We came across this issue because setting specific broker URLs for specific Celery apps in our code base isn't working. Our backend is sending everything to the value configured in CELERY_BROKER_URL env 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).

Comment thread celery/app/utils.py
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@soceanainn

Copy link
Copy Markdown
Author

@auvipy tagging you here as you had volunteered to review any contributions to the original issue (although I understand that was 18 months ago)

@auvipy

auvipy commented Jun 22, 2024

Copy link
Copy Markdown
Member

All the tests are failing. Can you check?

@SebastianDix

Copy link
Copy Markdown

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

@soceanainn

Copy link
Copy Markdown
Author

Okay I see the issue in tests now:

  1. Celery CLI is actually configured by explicitly setting these env variables:

    celery/celery/bin/celery.py

    Lines 152 to 162 in 1aff856

    if loader:
    # Default app takes loader from this env (Issue #1066).
    os.environ['CELERY_LOADER'] = loader
    if broker:
    os.environ['CELERY_BROKER_URL'] = broker
    if result_backend:
    os.environ['CELERY_RESULT_BACKEND'] = result_backend
    if config:
    os.environ['CELERY_CONFIG_MODULE'] = config
    if skip_checks:
    os.environ['CELERY_SKIP_CHECKS'] = 'true'
  2. This is causing issues in tests due to my change, since we pass broker CLI option in the test, but it's using an explicitly configured AMQP broker_url from t.integration.test_worker_config when defining the test app:

@soceanainn
soceanainn force-pushed the soceanainn/dont-override-explicit-config-with-env-vars branch from 3eb616c to ef5eef5 Compare July 1, 2024 14:40
@codecov

codecov Bot commented Jul 1, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.

Project coverage is 78.02%. Comparing base (e6dd621) to head (7523d45).
Report is 41 commits behind head on main.

Files with missing lines Patch % Lines
celery/app/base.py 80.00% 1 Missing ⚠️
celery/bin/celery.py 50.00% 1 Missing ⚠️
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           
Flag Coverage Δ
unittests 77.99% <71.42%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind adding new tests for the proposed changes? My understanding so far is that you are trying to change the precedence mostly?

@Nusnus
Nusnus force-pushed the soceanainn/dont-override-explicit-config-with-env-vars branch from ef5eef5 to 816fa88 Compare July 18, 2024 11:38
@Nusnus
Nusnus self-requested a review July 18, 2024 11:41
@Nusnus
Nusnus force-pushed the soceanainn/dont-override-explicit-config-with-env-vars branch from 816fa88 to 151c5fc Compare July 18, 2024 22:00
_INSPECT_OPTIONS = ['--timeout', '0'] # Avoid waiting for the zero workers to reply


@pytest.fixture(autouse=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removing this test? and why no new one?

@Nusnus
Nusnus force-pushed the soceanainn/dont-override-explicit-config-with-env-vars branch from 151c5fc to 7523d45 Compare September 25, 2024 11:24
@celery celery deleted a comment from Nusnus Feb 24, 2025
@auvipy auvipy added this to the 5.6.0 milestone Feb 24, 2025

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't add / consider this without additional tests

@auvipy auvipy removed this from the 5.6.0 milestone Feb 24, 2025
@auvipy auvipy added this to the 5.7.0 milestone May 14, 2025
@auvipy
auvipy requested a lite review from Copilot August 29, 2026 13:38
@auvipy auvipy modified the milestones: 5.7.0, 5.8.0 Aug 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 over CELERY_* env vars.
  • Stop the CLI from exporting CELERY_BROKER_URL / CELERY_RESULT_BACKEND into os.environ, and instead apply CLI overrides via app pre-configuration.
  • Remove a test fixture that was clearing os.environ in test_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.

Comment thread celery/app/base.py
Comment on lines 394 to +401
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)
Comment thread celery/app/utils.py
Comment on lines 100 to 105
@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')
)

This branch has not been deployed

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

Environment variable for Broker URL is prioritized over app settings

4 participants