Skip to content

Fix escaped percent signs in node formatting - #10663

Merged
auvipy merged 13 commits into
celery:mainfrom
jakezwang:fix/simple-format-percent-escape
Sep 27, 2026
Merged

auvipy merged 13 commits into
celery:mainfrom
jakezwang:fix/simple-format-percent-escape

Conversation

@jakezwang

@jakezwang jakezwang commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

Recognize %% as an escaped percent sign in simple_format(). The formatter already registers % as a literal value, but its pattern only matches word characters, so %%n expands the node name and %%x raises an unknown-format error.

host_format("logs/%%n-%n.log", host="worker.example.com") now returns logs/%n-worker.log. Log and pid file options passed through celery multi are 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.pidfile now applies the worker's second formatting pass too. For example, --pidfile=%%%%n-%n.pid is 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 --pidfile and -p, and actual logfile creation through Celery's logging subsystem in isolated subprocesses.

Validation

  • 67 focused unit tests passed, including ten pidfile cases. The six standalone subprocess logfile checks also pass (pytest --noconftest t/integration/test_logfile_format.py).
  • Before the pidfile fix, eight escaped-path cases failed to locate the pidfile; they pass with the fix.
  • All configured pre-commit hooks passed for changed files, including YAML validation and mypy.
  • Docker and smoke-workflow changes are now provided by upstream Fix cache directory ownership in Docker builds #10707 and Modify apt-get install to add libmemcached-dev and libev-dev #10709; this PR contains only the percent-formatting fix, documentation, and tests.

@auvipy auvipy added this to the 5.7.0 milestone Sep 18, 2026
@codecov

codecov Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.52%. Comparing base (42851ea) to head (07a82a9).
⚠️ Report is 1 commits behind head on main.

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     
Flag Coverage Δ
unittests 89.50% <100.00%> (+0.07%) ⬆️

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

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

@auvipy
auvipy self-requested a review September 19, 2026 17:21
@auvipy

auvipy commented Sep 19, 2026

Copy link
Copy Markdown
Member

@MehrazRumman can you check this?

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

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 %% for celery worker, and %%%% when going through celery 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 that node_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.py asserting 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 / %d section) 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_escape is a module-level function in test_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 (see prefork.process_initializer, which re-runs logging setup when %i is present in CELERY_LOG_FILE).
  • test_simple_format_unknown_key passes on main too, 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.

@auvipy auvipy modified the milestones: 5.7.0, 5.7.x Sep 21, 2026
@auvipy

auvipy commented Sep 21, 2026

Copy link
Copy Markdown
Member

@jakezwang please cross check the remaining review suggestions. if possible add some smoke or integrations tests which are relevant in this case

@auvipy

auvipy commented Sep 24, 2026

Copy link
Copy Markdown
Member

@GangEunzzang you review will help to proceed with this

@auvipy auvipy modified the milestones: 5.7.x, 5.7.0 Sep 24, 2026

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

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.

MehrazRumman

This comment was marked as duplicate.

Comment thread .github/workflows/smoke-tests.yml Resolved
Comment thread docker/Dockerfile Resolved
@auvipy
auvipy merged commit 31b6167 into celery:main Sep 27, 2026
411 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants