Repository navigation
Fix escaped percent signs in node formatting - #10663
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10663 +/- ##
==========================================
+ Coverage 89.44% 89.52% +0.07%
==========================================
Files 153 153
Lines 20414 20414
Branches 2434 2434
==========================================
+ Hits 18260 18275 +15
+ Misses 1851 1838 -13
+ Partials 303 301 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
@MehrazRumman can you check this? |
MehrazRumman
left a comment
There was a problem hiding this comment.
Review: Fix escaped percent signs in node formatting
Thanks, this is a clean, minimal fix. The intent was already in the code: simple_format() does keys.setdefault('%', '%'). But RE_FORMAT = r'%(\w)' can never match a second %, so that line was dead code. Widening the pattern to %([%\w]) makes the existing mapping take effect. Because re.sub scans left to right and doesn't overlap matches, %%%n → % + expanded n and %%%%n → %%n behave as you'd expect. The parametrized cases cover this well.
I traced all callers (host_format, node_format in bin/worker.py, app/log.py, apps/multi.py). A few things to consider before merging.
🟡 1. Escapes don't survive celery multi (double expansion)
celery multi expands option values through build_expander() → node_format(). The worker then formats the same value again (setup_logging_subsystem() → node_format(logfile, hostname)). With this change, an escape is consumed by the first pass and the second pass expands it:
| input | after multi pass |
after worker pass |
|---|---|---|
logs/%%n-%n.log |
logs/%n-w1.log |
logs/w1-w1.log ❌ |
logs/50%%d.log |
logs/50%d.log |
logs/50example.com.log ❌ |
logs/%%x.log |
logs/%x.log |
ValueError: Unknown format %x ❌ |
This isn't a regression, since %%n was already broken before. But the PR description presents %% as the way to get a literal %, and that only holds for single-pass paths (celery worker directly). Under multi you'd need %%%%. The existing UNKNOWN_SIMPLE_FORMAT_KEY message already hints at this ("did you escape and the value was expanded twice? (%%N -> %N -> %hostname)"). build_expander() also deliberately re-emits i='%i' / I='%I' so those survive to the second pass.
Suggestions, any of these would do:
- Document it: literal
%is%%forcelery worker, and%%%%when going throughcelery multi. - Or, preferably, have
build_expander()re-emit the escape as well (**{'%': '%%'}), the same way it already preserves%i/%I. Then%%means the same thing everywhere. Note thatnode_format(..., **extra)can't take%as a keyword, so this needs a small change to how the extra keys are passed. - Add a test in
t/unit/apps/test_multi.pyasserting the final worker argv for an escaped value, whichever behavior you pick.
🟡 2. Small behavior change worth a changelog line
Anyone who currently has %%h (or %%n / %%d) reaching Celery unprocessed gets %<hostname> today and will get a literal %h after this change. This is the correct behavior and unlikely to be relied on, but it's user-visible for log and pid file paths. Note that docs/userguide/workers.rst tells supervisor users to write %%h: supervisor unescapes that to %h before Celery sees it, so those users are unaffected. I'd still mention it in the 5.7 changelog, e.g. "%% in node/host format strings now produces a literal %".
🟢 Nits
- Docs: the variable table in
docs/userguide/workers.rst(the%h/%n/%dsection) would benefit from a%%row ("literal%"). Currently the only mention of%%is the supervisor note, which is about supervisor's escaping, not Celery's, and reads confusingly next to this change. - Test placement:
test_host_format_percent_escapeis a module-level function intest_nodenames.py, while the existing tests there are grouped in a class. Minor, just for consistency. - Extra cases: consider covering an escape adjacent to the process-index keys (
%%i→%i, and%%I). Those are the ones most likely to be passed through multiple layers (seeprefork.process_initializer, which re-runs logging setup when%iis present inCELERY_LOG_FILE). test_simple_format_unknown_keypasses onmaintoo, so it's a guard rather than a regression case. That's fine to keep, just noting it for accuracy against the "seven regression cases" count.
Summary
LGTM on the core change: it's correct, minimal, and restores clearly intended behavior. I'd like to see (1) either handled in build_expander() or documented, plus a changelog entry for (2). The nits are optional.
|
@jakezwang please cross check the remaining review suggestions. if possible add some smoke or integrations tests which are relevant in this case |
Signed-off-by: Jake Wang <[email protected]>
Signed-off-by: Jake Wang <[email protected]>
|
@GangEunzzang you review will help to proceed with this |
GangEunzzang
left a comment
There was a problem hiding this comment.
Nice fix. One thing I ran into: with celery multi, a pidfile like --pidfile=%%%%n-%n.pid gets written as %n-w1.pid, but multi looks for %%n-w1.pid, so stopwait can't find the worker and leaves it running. It's not a regression since main can't even start with that, so a follow-up is fine too. Wrapping Node.pidfile in node_format() fixed it for me.
Signed-off-by: Jake Wang <[email protected]>
Description
Recognize
%%as an escaped percent sign insimple_format(). The formatter already registers%as a literal value, but its pattern only matches word characters, so%%nexpands the node name and%%xraises an unknown-format error.host_format("logs/%%n-%n.log", host="worker.example.com")now returnslogs/%n-worker.log. Log and pid file options passed throughcelery multiare expanded twice, so they require%%%%to preserve a literal percent sign in the final worker path. The worker guide and changelog document this behavior.Node.pidfilenow applies the worker's second formatting pass too. For example,--pidfile=%%%%n-%n.pidis both written and located as%n-worker.pid, allowing multi's stop/stopwait commands to find the worker.Tests cover the final multi argv, reading real pidfiles for both
--pidfileand-p, and actual logfile creation through Celery's logging subsystem in isolated subprocesses.Validation
pytest --noconftest t/integration/test_logfile_format.py).