@@ -53,7 +53,7 @@ absl::Status FileLock::Delete() && {
53
53
assert (fd_ != FileDescriptorTraits::Invalid ());
54
54
55
55
auto fd = std::exchange (fd_, FileDescriptorTraits::Invalid ());
56
- auto status = internal_os:: DeleteOpenFile (fd, lock_path_);
56
+ auto status = DeleteOpenFile (fd, lock_path_);
57
57
Unlock (fd);
58
58
FileDescriptorTraits::Close (fd);
59
59
return MaybeAnnotateStatus (std::move (status), " Failed to clean lock file" );
@@ -70,13 +70,13 @@ Result<FileLock> AcquireFileLock(std::string lock_path) {
70
70
using private_t = FileLock::private_t ;
71
71
TENSORSTORE_ASSIGN_OR_RETURN (
72
72
UniqueFileDescriptor fd,
73
- internal_os:: OpenFileWrapper (lock_path, OpenFlags::DefaultWrite));
73
+ OpenFileWrapper (lock_path, OpenFlags::DefaultWrite));
74
74
FileInfo a, b;
75
75
FileInfo* info = &a;
76
76
77
77
// 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)) {
80
80
return absl::FailedPreconditionError (
81
81
absl::StrCat (" Not a regular file: " , lock_path));
82
82
}
@@ -85,20 +85,19 @@ Result<FileLock> AcquireFileLock(std::string lock_path) {
85
85
while (true ) {
86
86
// Acquire lock.
87
87
TENSORSTORE_ASSIGN_OR_RETURN (
88
- auto unlock_fn, internal_os:: AcquireFdLock (fd.get ()),
88
+ auto unlock_fn, AcquireFdLock (fd.get ()),
89
89
MaybeAnnotateStatus (_, absl::StrCat (" Failed to acquire lock on file: " ,
90
90
QuoteString (lock_path))));
91
91
92
92
// Reopening the file should give the same value since the lock is held.
93
93
TENSORSTORE_ASSIGN_OR_RETURN (
94
94
UniqueFileDescriptor other_fd,
95
- internal_os::OpenFileWrapper (lock_path, OpenFlags::DefaultWrite));
95
+ OpenFileWrapper (lock_path,
96
+ OpenFlags::DefaultWrite | OpenFlags::CloseOnExec));
96
97
97
98
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)) {
102
101
// Lock was acquired successfully.
103
102
return FileLock (private_t (), std::move (lock_path), fd.release (),
104
103
std::move (unlock_fn));
@@ -122,10 +121,11 @@ Result<FileLock> AcquireExclusiveFile(std::string lock_path,
122
121
123
122
// Determine whether the lock file is stale.
124
123
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);
126
126
if (read_fd.ok ()) {
127
127
TENSORSTORE_RETURN_IF_ERROR (GetFileInfo (read_fd->get (), &info));
128
- if (!internal_os:: IsRegularFile (info)) {
128
+ if (!IsRegularFile (info)) {
129
129
// A lock file must be a regular file, not a symlink or directory.
130
130
return absl::FailedPreconditionError (
131
131
absl::StrCat (" Not a regular file: " , lock_path));
@@ -148,11 +148,11 @@ Result<FileLock> AcquireExclusiveFile(std::string lock_path,
148
148
n++;
149
149
if (m > 1000 ) m = 1000 ;
150
150
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 );
154
154
if (fd.ok ()) {
155
- TENSORSTORE_RETURN_IF_ERROR (internal_os:: GetFileInfo (fd->get (), &info));
155
+ TENSORSTORE_RETURN_IF_ERROR (GetFileInfo (fd->get (), &info));
156
156
return FileLock (private_t {}, std::move (lock_path), fd->release (),
157
157
std::nullopt);
158
158
}
0 commit comments