diff --git a/cachecontrol/caches/file_cache.py b/cachecontrol/caches/file_cache.py index 5fc59524..c311c36d 100644 --- a/cachecontrol/caches/file_cache.py +++ b/cachecontrol/caches/file_cache.py @@ -88,8 +88,10 @@ def _write(self, path: str, data: bytes) -> None: with self.lock_class(path + ".lock"): # Write our actual file (fd, name) = tempfile.mkstemp(dir=dirname) - os.write(fd, data) - os.close(fd) + try: + os.write(fd, data) + finally: + os.close(fd) os.chmod(name, self.filemode) os.replace(name, path)