Worker/huey ignores REDIS_HOST and connects to localhost when OTEL_ENABLED=true (import-order race in settings) #1124
Unanswered
mgradalska
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What's happening
With
OTEL_ENABLED=trueand an OTLP endpoint configured, the huey worker (and the task-enqueue path in the API server) connect tolocalhost:6379instead of the configuredREDIS_HOST. The worker then fails in a tight retry loop:The worker pegs CPU at 100% retrying.
karrio-serverlogs the same error (it enqueues via the same broken huey instance). Background tasks never run.This does not happen when OTel is disabled.
Root cause
In
huey.contrib.djhuey, the module readssettings.HUEYonce at import time. If that attribute isn't set yet, it falls back to constructing a defaultRedisHuey()- which connects tolocalhost:6379- and that fallback instance is cached at module level for the lifetime of the process.Karrio's settings package builds
settings.HUEYinapps/api/karrio/server/settings/workers.py, and the settings__init__.pyimports modules in this order:When
OTEL_ENABLED=trueand an OTLP endpoint is set,apm.pyeagerly imports the OpenTelemetry instrumentation graph and callsRedisInstrumentor().instrument()at settings-load time. That import graph pulls inhuey.contrib.djhuey- which runsgetattr(settings, 'HUEY', None)beforeworkers.pyhas executed. So djhuey seesNone, builds its own defaultRedisHuey()pointed atlocalhost, and caches it forever.workers.pythen setssettings.HUEYcorrectly, but it's too late - djhuey already has its broken singleton.When OTel is disabled,
apm.pydoesn't import the instrumentation graph, djhuey is imported later (afterworkers.pyhas run), and it correctly captures the configured instance.Reproduction
Using
karrio/server:2026.1.31with Redis/Valkey reachable at hostvalkey(adjust to your setup):Broken - OTel enabled:
Output:
Working - OTel disabled (same command without the two
OTEL_*vars):Impact
OTEL_ENABLED=true+ OTLP endpoint) has a non-functional worker. Background jobs (tracking pulses, webhooks, data archiving) silently never run, and the worker burns a CPU core in a retry loop.localhost:6379) sends operators chasing Redis/networking config whenREDIS_HOSTis in fact set correctly.settings.HUEYlooks right under inspection; only the djhuey singleton is wrong.Notes on fixing
The fix needs to guarantee
settings.HUEYis populated beforehuey.contrib.djhueyis first imported. A few directions (not validated here - leaving the choice to maintainers):workers.pyruns beforeapm.py.RedisInstrumentor().instrument()(and the OTel instrumentation imports) out of settings-load time.Hueyinstance tosettings.HUEYin a way that's sensitive to import order.Workaround for affected operators
Disable OTel on the server and worker (
OTEL_ENABLED=false). djhuey then imports afterworkers.pyruns and captures the correct broker connection. Costs you Karrio's OpenTelemetry tracing until a fix lands.Beta Was this translation helpful? Give feedback.
All reactions