@@ -53,7 +53,7 @@ absl::Status FileLock::Delete() && {
5353 assert (fd_ != FileDescriptorTraits::Invalid ());
5454
5555 auto fd = std::exchange (fd_, FileDescriptorTraits::Invalid ());
56- auto status = internal_os:: DeleteOpenFile (fd, lock_path_);
56+ auto status = DeleteOpenFile (fd, lock_path_);
5757 Unlock (fd);
5858 FileDescriptorTraits::Close (fd);
5959 return MaybeAnnotateStatus (std::move (status), " Failed to clean lock file" );
@@ -70,13 +70,13 @@ Result<FileLock> AcquireFileLock(std::string lock_path) {
7070 using private_t = FileLock::private_t ;
7171 TENSORSTORE_ASSIGN_OR_RETURN (
7272 UniqueFileDescriptor fd,
73- internal_os:: OpenFileWrapper (lock_path, OpenFlags::DefaultWrite));
73+ OpenFileWrapper (lock_path, OpenFlags::DefaultWrite));
7474 FileInfo a, b;
7575 FileInfo* info = &a;
7676
7777 // Is this a network filesystem?
78- TENSORSTORE_RETURN_IF_ERROR (internal_os:: GetFileInfo (fd.get (), info));
79- if (!internal_os:: IsRegularFile (*info)) {
78+ TENSORSTORE_RETURN_IF_ERROR (GetFileInfo (fd.get (), info));
79+ if (!IsRegularFile (*info)) {
8080 return absl::FailedPreconditionError (
8181 absl::StrCat (" Not a regular file: " , lock_path));
8282 }
@@ -85,20 +85,19 @@ Result<FileLock> AcquireFileLock(std::string lock_path) {
8585 while (true ) {
8686 // Acquire lock.
8787 TENSORSTORE_ASSIGN_OR_RETURN (
88- auto unlock_fn, internal_os:: AcquireFdLock (fd.get ()),
88+ auto unlock_fn, AcquireFdLock (fd.get ()),
8989 MaybeAnnotateStatus (_, absl::StrCat (" Failed to acquire lock on file: " ,
9090 QuoteString (lock_path))));
9191
9292 // Reopening the file should give the same value since the lock is held.
9393 TENSORSTORE_ASSIGN_OR_RETURN (
9494 UniqueFileDescriptor other_fd,
95- internal_os::OpenFileWrapper (lock_path, OpenFlags::DefaultWrite));
95+ OpenFileWrapper (lock_path,
96+ OpenFlags::DefaultWrite | OpenFlags::CloseOnExec));
9697
9798 FileInfo* other_info = info == &a ? &b : &a;
98- TENSORSTORE_RETURN_IF_ERROR (
99- internal_os::GetFileInfo (other_fd.get (), other_info));
100- if (internal_os::GetDeviceId (a) == internal_os::GetDeviceId (b) &&
101- internal_os::GetFileId (a) == internal_os::GetFileId (b)) {
99+ TENSORSTORE_RETURN_IF_ERROR (GetFileInfo (other_fd.get (), other_info));
100+ if (GetDeviceId (a) == GetDeviceId (b) && GetFileId (a) == GetFileId (b)) {
102101 // Lock was acquired successfully.
103102 return FileLock (private_t (), std::move (lock_path), fd.release (),
104103 std::move (unlock_fn));
@@ -122,10 +121,11 @@ Result<FileLock> AcquireExclusiveFile(std::string lock_path,
122121
123122 // Determine whether the lock file is stale.
124123 auto detect_stale_lock = [&]() mutable {
125- auto read_fd = OpenFileWrapper (lock_path, OpenFlags::OpenReadOnly);
124+ auto read_fd = OpenFileWrapper (
125+ lock_path, OpenFlags::OpenReadOnly | OpenFlags::CloseOnExec);
126126 if (read_fd.ok ()) {
127127 TENSORSTORE_RETURN_IF_ERROR (GetFileInfo (read_fd->get (), &info));
128- if (!internal_os:: IsRegularFile (info)) {
128+ if (!IsRegularFile (info)) {
129129 // A lock file must be a regular file, not a symlink or directory.
130130 return absl::FailedPreconditionError (
131131 absl::StrCat (" Not a regular file: " , lock_path));
@@ -148,11 +148,11 @@ Result<FileLock> AcquireExclusiveFile(std::string lock_path,
148148 n++;
149149 if (m > 1000 ) m = 1000 ;
150150
151- auto fd = internal_os:: OpenFileWrapper (
152- lock_path,
153- OpenFlags::Create | OpenFlags::Exclusive | OpenFlags::OpenReadWrite );
151+ auto fd = OpenFileWrapper (
152+ lock_path, OpenFlags::Create | OpenFlags::Exclusive |
153+ OpenFlags::OpenReadWrite | OpenFlags::CloseOnExec );
154154 if (fd.ok ()) {
155- TENSORSTORE_RETURN_IF_ERROR (internal_os:: GetFileInfo (fd->get (), &info));
155+ TENSORSTORE_RETURN_IF_ERROR (GetFileInfo (fd->get (), &info));
156156 return FileLock (private_t {}, std::move (lock_path), fd->release (),
157157 std::nullopt );
158158 }
0 commit comments