Fix race condition when creating async dir (#72069)
* Fix race condition when creating async dir * Simplify exception wrapper * Remove var used for testing
This commit is contained in:
parent
618d1a3871
commit
c9fa1d0e7e
2 changed files with 22 additions and 8 deletions
2
changelogs/fragments/async-race-condition.yml
Normal file
2
changelogs/fragments/async-race-condition.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- async_wrapper - Fix race condition when ``~/.ansible_async`` folder tries to be created by multiple async tasks at the same time - https://github.com/ansible/ansible/issues/59306
|
|
@ -8,6 +8,7 @@ from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
|
import errno
|
||||||
import json
|
import json
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
|
@ -123,6 +124,15 @@ def _get_interpreter(module_path):
|
||||||
module_fd.close()
|
module_fd.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _make_temp_dir(path):
|
||||||
|
# TODO: Add checks for permissions on path.
|
||||||
|
try:
|
||||||
|
os.makedirs(path)
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno != errno.EEXIST:
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def _run_module(wrapped_cmd, jid, job_path):
|
def _run_module(wrapped_cmd, jid, job_path):
|
||||||
|
|
||||||
tmp_job_path = job_path + ".tmp"
|
tmp_job_path = job_path + ".tmp"
|
||||||
|
@ -231,14 +241,16 @@ def main():
|
||||||
jobdir = os.path.expanduser(async_dir)
|
jobdir = os.path.expanduser(async_dir)
|
||||||
job_path = os.path.join(jobdir, jid)
|
job_path = os.path.join(jobdir, jid)
|
||||||
|
|
||||||
if not os.path.exists(jobdir):
|
|
||||||
try:
|
try:
|
||||||
os.makedirs(jobdir)
|
_make_temp_dir(jobdir)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
print(json.dumps({
|
print(json.dumps({
|
||||||
"failed": 1,
|
"failed": 1,
|
||||||
"msg": "could not create: %s" % jobdir
|
"msg": "could not create: %s - %s" % (jobdir, to_text(e)),
|
||||||
|
"exception": to_text(traceback.format_exc()),
|
||||||
}))
|
}))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# immediately exit this process, leaving an orphaned process
|
# immediately exit this process, leaving an orphaned process
|
||||||
# running which immediately forks a supervisory timing process
|
# running which immediately forks a supervisory timing process
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue