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>
pull/85670/merge
Ben Buchwald 3 months ago committed by GitHub
parent 967d654ef6
commit e30da51731
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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)

@ -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…
Cancel
Save