Skip to content
Myr

Merge pull request #1655 from dvitek/dvitek/issue1654

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix Race conditions in FileSystemBytecodeCache

authored by David Lord · committed by GitHub · parents 466a200, 746bb95 · Browse files · 2 files+49 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+49 added−6 removed

  • other:+49 added−6 removed

2 files+49 added−6 removed

modified:src/jinja2/bccache.py+48 added-6 removed
77 unmodified lines
78 except (EOFError, ValueError, TypeError):79 self.reset()80 return8182 def write_bytecode(self, f: t.BinaryIO) -> None:82 def write_bytecode(self, f: t.IO[bytes]) -> None:83 """Dump the bytecode into the file or file like object passed."""84 if self.code is None:85 raise TypeError("can't write empty bucket")86 f.write(bc_magic)
174 unmodified lines
261262 def load_bytecode(self, bucket: Bucket) -> None:263 filename = self._get_cache_filename(bucket)264265 if os.path.exists(filename):266 with open(filename, "rb") as f:267 bucket.load_bytecode(f)265 # Don't test for existence before opening the file, since the266 # file could disappear after the test before the open.267 try:268 f = open(filename, "rb")269 except (FileNotFoundError, IsADirectoryError, PermissionError):270 # PermissionError can occur on Windows when an operation is271 # in progress, such as calling clear().272 return273274 with f:275 bucket.load_bytecode(f)276277 def dump_bytecode(self, bucket: Bucket) -> None:270 with open(self._get_cache_filename(bucket), "wb") as f:271 bucket.write_bytecode(f)278 # Write to a temporary file, then rename to the real name after279 # writing. This avoids another process reading the file before280 # it is fully written.281 name = self._get_cache_filename(bucket)282 f = tempfile.NamedTemporaryFile(283 mode="wb",284 dir=os.path.dirname(name),285 prefix=os.path.basename(name),286 suffix=".tmp",287 delete=False,288 )289290 def remove_silent() -> None:291 try:292 os.remove(f.name)293 except OSError:294 # Another process may have called clear(). On Windows,295 # another program may be holding the file open.296 pass297298 try:299 with f:300 bucket.write_bytecode(f)301 except BaseException:302 remove_silent()303 raise304305 try:306 os.replace(f.name, name)307 except OSError:308 # Another process may have called clear(). On Windows,309 # another program may be holding the file open.310 remove_silent()311 except BaseException:312 remove_silent()313 raise314315 def clear(self) -> None:316 # imported lazily here because google app-engine doesn't support317 # write access on the file system and the function does not exist318 # normally.
88 unmodified lines
modified:CHANGES.rst+1 added
5 unmodified lines
6Unreleased78- Add parameters to ``Environment.overlay`` to match ``__init__``.9 :issue:`1645`10- Handle race condition in ``FileSystemBytecodeCache``. :issue:`1654`111213Version 3.1.114-------------
961 unmodified lines