Skip to content

Commit

Permalink
Need to close file descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
JDBetteridge committed Oct 8, 2024
1 parent 81d0f0a commit df14e55
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pyop2/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,14 @@ def __setitem__(self, key, value):
# the filesystem may be network based. `mkstemp` does so securely without
# race conditions:
# https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp
_, tempfile = mkstemp(suffix=".tmp", prefix=k2, dir=basedir, text=False)
# The file descriptor must also be closed after use with `os.close()`.
fd, tempfile = mkstemp(suffix=".tmp", prefix=k2, dir=basedir, text=False)
tempfile = Path(tempfile)
# Open using `tempfile` (the filename) rather than the file descriptor
# to allow redefining `self.open`
with self.open(tempfile, mode="wb") as fh:
self.write(fh, value)
os.close(fd)

# Renaming (moving) the file is guaranteed by any POSIX compliant
# filesystem to be atomic. This may fail if somehow the destination is
Expand Down
3 changes: 2 additions & 1 deletion pyop2/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def make_so(compiler, jitmodule, extension, comm, filename=None):
tempdir = MEM_TMP_DIR.joinpath(f"{randint(0, 255):02x}")
tempdir.mkdir(parents=True, exist_ok=True)
# This path + filename should be unique
_, filename = mkstemp(suffix=f".{extension}", dir=tempdir, text=True)
descriptor, filename = mkstemp(suffix=f".{extension}", dir=tempdir, text=True)
filename = Path(filename)
else:
filename.parent.mkdir(exist_ok=True)
Expand All @@ -609,6 +609,7 @@ def make_so(compiler, jitmodule, extension, comm, filename=None):
# Write source code to disk
with open(cname, "w") as fh:
fh.write(jitmodule.code_to_compile)
os.close(descriptor)

if not compiler.ld:
# Compile and link
Expand Down

0 comments on commit df14e55

Please sign in to comment.