# Buggy code
def _setdefaultopt(self, d, alt, value):
    for opt in alt[1:]:
        try:
            return d[opt]
        except KeyError:
            pass
    value = os.path.normpath(value)
    dir_path = os.path.dirname(value)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    return d.setdefault(alt[0], value)


# Traceback (PermissionError)
celery multi v4.4.3 (cliffs)
_annotate_with_default_opts: print options
OrderedDict([('--app', 'service.celery:app'),
('--pidfile', '/var/run/demo/celeryd-%n.pid'),
('--logfile', '/var/log/demo/celeryd-%n%I.log'),
('--loglevel', 'INFO'),
('--workdir', '/var/lib/demo-celery'),
('--events', None),
('--heartbeat-interval', '5'),
('--without-gossip', None),
('--queues', 'high'),
('--concurrency', '1'),
('-n', 'high@celeryd.worker')])
Traceback (most recent call last):
File "/var/www/misc/ve-2006011156/bin/celery", line 8, in <module>
sys.exit(main())
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/__main__.py", line 16, in main
_main()
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/celery.py", line 322, in main
cmd.execute_from_commandline(argv)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/celery.py", line 495, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/base.py", line 305, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/celery.py", line 487, in handle_argv
return self.execute(command, argv)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/celery.py", line 419, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/celery.py", line 335, in run_from_argv
return cmd.execute_from_commandline([command] + argv)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/multi.py", line 266, in execute_from_commandline
return self.call_command(argv[0], argv[1:])
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/multi.py", line 273, in call_command
return self.commands[command](*argv) or EX_OK
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/multi.py", line 143, in _inner
return fun(self, *args, **kwargs)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/multi.py", line 151, in _inner
return fun(self, self.cluster_from_argv(argv), **kwargs)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/multi.py", line 361, in cluster_from_argv
_, cluster = self._cluster_from_argv(argv, cmd=cmd)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/bin/multi.py", line 366, in _cluster_from_argv
return p, self.Cluster(list(nodes), cmd=cmd)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/apps/multi.py", line 306, in <genexpr>
for name in names
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/apps/multi.py", line 314, in _node_from_options
p.optmerge(namespace, options), p.passthrough)
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/apps/multi.py", line 136, in __init__
options or OrderedDict())
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/apps/multi.py", line 145, in _annotate_with_default_opts
self._setdefaultopt(options, ['--pidfile', '-p'], '/var/run/celery/%n.pid')
File "/var/www/misc/ve-2006011156/lib/python3.7/site-packages/celery/apps/multi.py", line 159, in _setdefaultopt
os.makedirs(dir_path)
File "/var/www/misc/ve-2006011156/lib/python3.7/os.py", line 223, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/var/run/celery'

systemd[1]: demo@celeryd.service: Control process exited, code=exited, status=1/FAILURE

# Fixed code
def _setdefaultopt(self, d, alt, value):
    for opt in alt[1:]:
        try:
            return d[opt]
        except KeyError:
            pass
    value = d.setdefault(alt[0], os.path.normpath(value))
    dir_path = os.path.dirname(value)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    return value