diff options
author | David Lord <davidism@gmail.com> | 2022-04-25 14:14:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-25 14:14:10 -0700 |
commit | b802b5a6ad9deea082c16d9adb6417eda1a184d8 (patch) | |
tree | 044f7d4e6a5af7e1eda83b2eddc45c4b534d137c | |
parent | 466a200ea40642b674db77588d13889abbad55f5 (diff) | |
parent | 746bb95780c17687b27b6d1bf4df1216f0da972c (diff) | |
download | jinja2-b802b5a6ad9deea082c16d9adb6417eda1a184d8.tar.gz |
Merge pull request #1655 from dvitek/dvitek/issue1654
Fix Race conditions in FileSystemBytecodeCache
-rw-r--r-- | CHANGES.rst | 1 | ||||
-rw-r--r-- | src/jinja2/bccache.py | 54 |
2 files changed, 49 insertions, 6 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index da7d0a6..96a9567 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,7 @@ Unreleased - Add parameters to ``Environment.overlay`` to match ``__init__``. :issue:`1645` +- Handle race condition in ``FileSystemBytecodeCache``. :issue:`1654` Version 3.1.1 diff --git a/src/jinja2/bccache.py b/src/jinja2/bccache.py index 3bb61b7..d0ddf56 100644 --- a/src/jinja2/bccache.py +++ b/src/jinja2/bccache.py @@ -79,7 +79,7 @@ class Bucket: self.reset() return - def write_bytecode(self, f: t.BinaryIO) -> None: + def write_bytecode(self, f: t.IO[bytes]) -> None: """Dump the bytecode into the file or file like object passed.""" if self.code is None: raise TypeError("can't write empty bucket") @@ -262,13 +262,55 @@ class FileSystemBytecodeCache(BytecodeCache): def load_bytecode(self, bucket: Bucket) -> None: filename = self._get_cache_filename(bucket) - if os.path.exists(filename): - with open(filename, "rb") as f: - bucket.load_bytecode(f) + # Don't test for existence before opening the file, since the + # file could disappear after the test before the open. + try: + f = open(filename, "rb") + except (FileNotFoundError, IsADirectoryError, PermissionError): + # PermissionError can occur on Windows when an operation is + # in progress, such as calling clear(). + return + + with f: + bucket.load_bytecode(f) def dump_bytecode(self, bucket: Bucket) -> None: - with open(self._get_cache_filename(bucket), "wb") as f: - bucket.write_bytecode(f) + # Write to a temporary file, then rename to the real name after + # writing. This avoids another process reading the file before + # it is fully written. + name = self._get_cache_filename(bucket) + f = tempfile.NamedTemporaryFile( + mode="wb", + dir=os.path.dirname(name), + prefix=os.path.basename(name), + suffix=".tmp", + delete=False, + ) + + def remove_silent() -> None: + try: + os.remove(f.name) + except OSError: + # Another process may have called clear(). On Windows, + # another program may be holding the file open. + pass + + try: + with f: + bucket.write_bytecode(f) + except BaseException: + remove_silent() + raise + + try: + os.replace(f.name, name) + except OSError: + # Another process may have called clear(). On Windows, + # another program may be holding the file open. + remove_silent() + except BaseException: + remove_silent() + raise def clear(self) -> None: # imported lazily here because google app-engine doesn't support |