Skip to content

Commit

Permalink
Fix FFI cache race condition
Browse files Browse the repository at this point in the history
The race condition exists in the following circumstance:

1. PyVEX process 1 parses ffi and opens a new cache file but has not
   written to it
2. PyVEX process 2 goes to parse ffi, sees the existing (empty) cache
   file, opens it, then fails when it attempts to read an empty file

This commit makes cache writing atomic, preventing the race condition.
  • Loading branch information
jakelamberson committed Jan 30, 2025
1 parent e1eaa97 commit a695b9e
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyvex/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ def _parse_ffi_str():
"_declarations": ffi._parser._declarations,
"_int_constants": ffi._parser._int_constants,
}
with open(cache_location, "wb") as f:
# atomically write cache
temp_file = tempfile.NamedTemporaryFile(delete=False)
with open(temp_file.name, "wb") as f:
f.write(pickle.dumps(cache))
os.replace(temp_file.name, cache_location)


def _find_c_lib():
Expand Down

0 comments on commit a695b9e

Please sign in to comment.