Skip to content

Commit

Permalink
core: fix race condition for file backend
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Sep 19, 2023
1 parent 251efb5 commit 1bcc807
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/cachew/backend/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def __enter__(self) -> 'FileBackend':

def __exit__(self, *args) -> None:
if self.jsonl_tmp_fw is not None:
self.jsonl_tmp_fw.close()

# might still exist in case of early exit
self.jsonl_tmp.unlink(missing_ok=True)

# NOTE: need to unlink first
# otherwise possible that someone else might open the file before we unlink it
self.jsonl_tmp_fw.close()

if self.jsonl_fr is not None:
self.jsonl_fr.close()

Expand Down
2 changes: 1 addition & 1 deletion src/cachew/tests/test_cachew.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ def fun():
print("FINISHED")
'''
r = run(['python3', '-c', prog], cwd=tmp_path, stderr=PIPE, stdout=PIPE, check=True)
r = run([sys.executable, '-c', prog], cwd=tmp_path, stderr=PIPE, stdout=PIPE, check=True)
assert r.stdout.strip() == b'FINISHED'
assert b'Traceback' not in r.stderr

Expand Down

0 comments on commit 1bcc807

Please sign in to comment.