Repository: celery/celery

Issue:
`celery multi`creates xxx_%I.log files
<!--
Please fill this template entirely and do not erase parts of it.
We reserve the right to close without a response
bug reports which are incomplete.
-->
# Checklist
<!--
To check an item on the list replace [ ] with [x].
-->
- [x] I have verified that the issue exists against the `master` branch of Celery.
- [x] I have read the relevant section in the
  [contribution guide](http://docs.celeryproject.org/en/latest/contributing.html#other-bugs)
  on reporting bugs.
- [x] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical bug reports.
- [x] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed fixes.
- [x] I have checked the [commit log](https://github.com/celery/celery/commits/master)
  to find out if the bug was already fixed in the master branch.
- [x] I have included all related issues and possible duplicate issues
  in this issue (If there are none, check this box anyway).

## Mandatory Debugging Information

- [x] I have included the output of ``celery -A proj report`` in the issue.
    (if you are not able to do this, then at least specify the Celery
     version affected).
- [x] I have verified that the issue exists against the `master` branch of Celery.
- [ ] I have included the contents of ``pip freeze`` in the issue.
- [x] I have included all the versions of all the external dependencies required
  to reproduce this bug.

## Optional Debugging Information
<!--
Try some of the below if you think they are relevant.
It will help us figure out the scope of the bug and how many users it affects.
-->
- [ ] I have tried reproducing the issue on more than one Python version
  and/or implementation.
- [ ] I have tried reproducing the issue on more than one message broker and/or
  result backend.
- [ ] I have tried reproducing the issue on more than one version of the message
  broker and/or result backend.
- [ ] I have tried reproducing the issue on more than one operating system.
- [ ] I have tried reproducing the issue on more than one workers pool.
- [ ] I have tried reproducing the issue with autoscaling, retries,
  ETA/Countdown & rate limits disabled.
- [ ] I have tried reproducing the issue after downgrading
  and/or upgrading Celery and its dependencies.

## Related Issues and Possible Duplicates
<!--
Please make sure to search and mention any related issues
or possible duplicates to this issue as requested by the checklist above.

This may or may not include issues in other repositories that the Celery project
maintains or other repositories that are dependencies of Celery.

If you don't know how to mention issues, please refer to Github's documentation
on the subject: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
-->

#### Related Issues

- None

#### Possible Duplicates

- None

## Environment & Settings
<!-- Include the contents of celery --version below -->
**Celery version**:
<!-- Include the output of celery -A proj report below -->
<details>
<summary><b><code>celery report</code> Output:</b></summary>
<p>

```
software -> celery:5.0.1 (singularity) kombu:5.0.2 py:3.8.3
            billiard:3.6.3.0 redis:3.5.3
platform -> system:Linux arch:64bit, ELF
            kernel version:5.4.0-48-generic imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:disabled
```

</p>
</details>

# Steps to Reproduce

## Required Dependencies
<!-- Please fill the required dependencies to reproduce this issue -->
* **Minimal Python Version**: N/A or Unknown
* **Minimal Celery Version**: 5.0.1
* **Minimal Kombu Version**: N/A or Unknown
* **Minimal Broker Version**: N/A or Unknown
* **Minimal Result Backend Version**: N/A or Unknown
* **Minimal OS and/or Kernel Version**: N/A or Unknown
* **Minimal Broker Client Version**: N/A or Unknown
* **Minimal Result Backend Client Version**: N/A or Unknown

### Python Packages
<!-- Please fill the contents of pip freeze below -->
<details>
<summary><b><code>pip freeze</code> Output:</b></summary>
<p>

```
```

</p>
</details>

### Other Dependencies
<!--
Please provide system dependencies, configuration files
and other dependency information if applicable
-->
<details>
<p>
N/A
</p>
</details>

## Minimally Reproducible Test Case
<!--
Please provide a reproducible test case.
Refer to the Reporting Bugs section in our contribution guide.

We prefer submitting test cases in the form of a PR to our integration test suite.
If you can provide one, please mention the PR number below.
If not, please attach the most minimal code example required to reproduce the issue below.
If the test case is too large, please include a link to a gist or a repository below.
-->

<details>
<p>

```shell
$ celery multi start normal low --app=service.celery:app --logfile=/tmp/celery/celery-%n%I.log --queues:normal=normal --queues:low=low --concurrency:normal=2 --concurrency:low=1 --pidfile=/tmp/celery/celeryd-%n.pid
celery multi v5.0.1 (singularity)
> Starting nodes...
	> normal@dell: OK
	> low@dell: OK
```

</p>
</details>

# Expected Behavior
<!-- Describe in detail what you expect to happen -->

Celery 4.4.7 behavior
```shell
$ ls -l /tmp/celery
-rw-r--r-- 1 www-data www-data   6 Oct 19 14:39 celeryd-low.pid
-rw-r--r-- 1 www-data www-data   6 Oct 19 14:39 celeryd-normal.pid
-rw-rw-r-- 1 www-data www-data  80 Oct 19 14:39 celery-low-1.log
-rw-rw-r-- 1 www-data www-data 316 Oct 19 14:39 celery-low.log
-rw-rw-r-- 1 www-data www-data  80 Oct 19 14:39 celery-normal-1.log
-rw-rw-r-- 1 www-data www-data  80 Oct 19 14:39 celery-normal-2.log
-rw-rw-r-- 1 www-data www-data 316 Oct 19 14:39 celery-normal.log
```

# Actual Behavior
<!--
Describe in detail what actually happened.
Please include a backtrace and surround it with triple backticks (```).
In addition, include the Celery daemon logs, the broker logs,
the result backend logs and system logs below if they will help us debug
the issue.
-->
```shell
$ ls -l /tmp/celery
-rw-r--r-- 1 www-data www-data   6 Oct 19 14:39 celeryd-low.pid
-rw-r--r-- 1 www-data www-data   6 Oct 19 14:39 celeryd-normal.pid
-rw-rw-r-- 1 www-data www-data  80 Oct 19 14:39 celery-low-1.log
-rw-rw-r-- 1 www-data www-data   0 Oct 19 14:39 celery-low%I.log
-rw-rw-r-- 1 www-data www-data 316 Oct 19 14:39 celery-low.log
-rw-rw-r-- 1 www-data www-data  80 Oct 19 14:39 celery-normal-1.log
-rw-rw-r-- 1 www-data www-data  80 Oct 19 14:39 celery-normal-2.log
-rw-rw-r-- 1 www-data www-data   0 Oct 19 14:39 celery-normal%I.log
-rw-rw-r-- 1 www-data www-data 316 Oct 19 14:39 celery-normal.log
```


Discussion:
I can confirm this issue too on 5.0.1
I am aware of that problem.
I saw it myself but figured that other bugs need to be prioritized.
I'm adding this to the next bugfix release milestone.

If any of you have a solution, I'd gladly accept a PR.
I haven't checked the code for it yet (and not so known with the celery code-base) but gave it some thought:
Could it be that the main command (`celery multi`) also tries to process the `--logfile` parameter and because it doesn't do any further parsing on `%I` it creates that logfile? (While it also passes the parameter successfully on to its worker children commands)
It's the direction I had in mind as well.
@kwist-sgr  - can you confirm for me that `/tmp/celery` was clean prior to running both commands? I assume that's the case and the extra empty log files are created by some confusion along the lines of what @mfonville hypothesised above, but it'd be nice to get a sanity check before I check this out myself. Just hard to tell since the timestamps are the same in both `ls` outputs!

*Edit:* nevermind, confirm the above myself since I had some free time. Looks like the culprit is:
https://github.com/celery/celery/blob/2a6c7cfe3b1283961887bf1cb3f5aa6c8aa70820/celery/platforms.py#L411-L414
which gets passed an unexpanded logfile value from `celery.bin.worker.detach()`

Resolving patch:
diff --git a/celery/bin/worker.py b/celery/bin/worker.py
--- a/celery/bin/worker.py
+++ b/celery/bin/worker.py
@@ -94,6 +94,11 @@ def detach(path, argv, logfile=None, pidfile=None, uid=None,
            executable=None, hostname=None):
     """Detach program by argv."""
     fake = 1 if C_FAKEFORK else fake
+    # `detached()` will attempt to touch the logfile to confirm that error
+    # messages won't be lost after detaching stdout/err, but this means we need
+    # to pre-format it rather than relying on `setup_logging_subsystem()` like
+    # we can elsewhere.
+    logfile = node_format(logfile, hostname)
     with detached(logfile, pidfile, uid, gid, umask, workdir, fake,
                   after_forkers=False):
         try:


Test patch: