The Fix
pip install celery==5.3.0b2
Based on closed celery/celery issue #7726 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -72,6 +72,7 @@ def start_worker(
test_worker_starting.send(sender=app)
+ worker = None
try:
with _start_worker_thread(app,
app = <Celery celery.tests at 0x7f57bdd6ee50>, concurrency = 1, pool = 'prefork'
loglevel = 'debug', logfile = None, perform_ping_check = True
ping_task_timeout = 10.0, shutdown_timeout = 10.0, kwargs = {}
@contextmanager
def start_worker(
app, # type: Celery
concurrency=1, # type: int
pool='solo', # type: str
loglevel=WORKER_LOGLEVEL, # type: Union[str, int]
logfile=None, # type: str
perform_ping_check=True, # type: bool
ping_task_timeout=10.0, # type: float
shutdown_timeout=10.0, # type: float
**kwargs # type: Any
):
# type: (...) -> Iterable
"""Start embedded worker.
Yields:
celery.app.worker.Worker: worker instance.
"""
test_worker_starting.send(sender=app)
try:
with _start_worker_thread(app,
concurrency=concurrency,
pool=pool,
loglevel=loglevel,
logfile=logfile,
perform_ping_check=perform_ping_check,
shutdown_timeout=shutdown_timeout,
**kwargs) as worker:
if perform_ping_check:
from .tasks import ping
with allow_join_result():
assert ping.delay().get(timeout=ping_task_timeout) == 'pong'
yield worker
finally:
test_worker_stopped.send(sender=app, worker=worker)
E UnboundLocalError: local variable 'worker' referenced before assignment
/usr/local/lib/python3.7/site-packages/celery/contrib/testing/worker.py:91: UnboundLocalError
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\npip install celery==5.3.0b2\nWhen NOT to use: Do not use this fix if the worker's initialization logic changes significantly.\n\n
Why This Fix Works in Production
- Trigger: When `_start_worker_thread` fails, the code the `finally` fails as well because `worker` is not defined.
- Mechanism: The variable 'worker' was not defined before being referenced in the finally block
- Why the fix works: Fixes an issue where a variable used in a finally block may not be instantiated, leading to an UnboundLocalError. (first fixed release: 5.3.0b2).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Shows up under Python 3.7 in real deployments (not just unit tests).
- The variable 'worker' was not defined before being referenced in the finally block
- Production symptom (often without a traceback): When `_start_worker_thread` fails, the code the `finally` fails as well because `worker` is not defined.
Proof / Evidence
- GitHub issue: #7726
- Fix PR: https://github.com/celery/celery/pull/7727
- First fixed release: 5.3.0b2
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.40
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Hey @woutdenolf :wave:, Thank you for opening an issue”
Failure Signature (Search String)
- When `_start_worker_thread` fails, the code the `finally` fails as well because `worker` is not defined.
- ping_task_timeout = 10.0, shutdown_timeout = 10.0, kwargs = {}
Copy-friendly signature
Failure Signature
-----------------
When `_start_worker_thread` fails, the code the `finally` fails as well because `worker` is not defined.
ping_task_timeout = 10.0, shutdown_timeout = 10.0, kwargs = {}
Error Message
Signature-only (no traceback captured)
Error Message
-------------
When `_start_worker_thread` fails, the code the `finally` fails as well because `worker` is not defined.
ping_task_timeout = 10.0, shutdown_timeout = 10.0, kwargs = {}
Minimal Reproduction
app = <Celery celery.tests at 0x7f57bdd6ee50>, concurrency = 1, pool = 'prefork'
loglevel = 'debug', logfile = None, perform_ping_check = True
ping_task_timeout = 10.0, shutdown_timeout = 10.0, kwargs = {}
@contextmanager
def start_worker(
app, # type: Celery
concurrency=1, # type: int
pool='solo', # type: str
loglevel=WORKER_LOGLEVEL, # type: Union[str, int]
logfile=None, # type: str
perform_ping_check=True, # type: bool
ping_task_timeout=10.0, # type: float
shutdown_timeout=10.0, # type: float
**kwargs # type: Any
):
# type: (...) -> Iterable
"""Start embedded worker.
Yields:
celery.app.worker.Worker: worker instance.
"""
test_worker_starting.send(sender=app)
try:
with _start_worker_thread(app,
concurrency=concurrency,
pool=pool,
loglevel=loglevel,
logfile=logfile,
perform_ping_check=perform_ping_check,
shutdown_timeout=shutdown_timeout,
**kwargs) as worker:
if perform_ping_check:
from .tasks import ping
with allow_join_result():
assert ping.delay().get(timeout=ping_task_timeout) == 'pong'
yield worker
finally:
test_worker_stopped.send(sender=app, worker=worker)
E UnboundLocalError: local variable 'worker' referenced before assignment
/usr/local/lib/python3.7/site-packages/celery/contrib/testing/worker.py:91: UnboundLocalError
Environment
- Python: 3.7
What Broke
UnboundLocalError occurs when the worker fails to start, causing confusion during debugging.
Why It Broke
The variable 'worker' was not defined before being referenced in the finally block
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install celery==5.3.0b2
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/celery/celery/pull/7727
First fixed release: 5.3.0b2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if the worker's initialization logic changes significantly.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Make timeouts explicit and test them (unit + integration) to avoid silent behavior changes.
- Instrument retries (attempt count + reason) and alert on spikes to catch dependency slowdowns.
Version Compatibility Table
| Version | Status |
|---|---|
| 5.3.0b2 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.