mirror of
https://github.com/zebrajr/ansible.git
synced 2025-12-06 00:19:48 +01:00
Fix jsonfile cache on WSL (#85816)
On WSL, `os.rename` can't correctly move a file while a handle to that file is still open. It remains half-moved where neither the source or destination seem to exist (according to `os.path.exists`). However the move seems to complete correctly when the open handle is closed. In `BaseFileCacheModule`, when writing a cache file, a temporary file is created with `mkstemp` that returns an open file descriptor and a filename. Once the cache is written to that file, it is renamed to the correct file name with `os.rename` and then its permissions set with `os.chmod`. On WSL the `os.chmod` fails because it doesn't think the file exists yet because the file descriptor returned by `mkstemp` is still open. This PR fixes this by closing that file descriptor before renaming. Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
967d654ef6
commit
e30da51731
2
changelogs/fragments/85816-wsl-cache-files.yml
Normal file
2
changelogs/fragments/85816-wsl-cache-files.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- cache plugins - close temp cache file before moving it to fix error on WSL. (https://github.com/ansible/ansible/pull/85816)
|
||||
1
lib/ansible/plugins/cache/__init__.py
vendored
1
lib/ansible/plugins/cache/__init__.py
vendored
|
|
@ -162,6 +162,7 @@ class BaseFileCacheModule(BaseCacheModule):
|
|||
except OSError as ex:
|
||||
display.error_as_warning(f"Error in {self.plugin_name!r} cache plugin while trying to write to {tmpfile_path!r}.", exception=ex)
|
||||
try:
|
||||
os.close(tmpfile_handle) # os.rename fails if handle is still open in WSL
|
||||
os.rename(tmpfile_path, cachefile)
|
||||
os.chmod(cachefile, mode=S_IRWU_RG_RO)
|
||||
except OSError as ex:
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user