Skip to content

Commit 3cbe319

Browse files
authored
[hooks_runner] Fail eagerly when file locking is not supported on file system (dart-lang#3679)
Instead of treating all `FileSystemException`s during `RandomAccessFile.lock` as lock contention and waiting until timeout, check the OS error code against the host OS contention error codes (`EAGAIN`/`EACCES` on Linux and macOS, `ERROR_SHARING_VIOLATION`/`ERROR_LOCK_VIOLATION` on Windows) and fail eagerly with a descriptive message when process locks are not supported. Closes: dart-lang#3651 Also, actually close the file. (Sorry diff is unreadable in github UI, see in vscode, everything is nested 1 level deeper)
1 parent cc7e4b0 commit 3cbe319

3 files changed

Lines changed: 455 additions & 50 deletions

File tree

‎pkgs/hooks_runner/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
classes) and `Target`.
55
- Drop regular dependency on `package:code_assets`.
66
- Track header dependencies in C build test projects.
7+
- Fail eagerly instead of waiting for timeout when running on a file system that
8+
does not support process locks.
79

810
## 1.6.4
911

‎pkgs/hooks_runner/lib/src/locking/locking.dart‎

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ File _fileInDir(Directory path, String filename) {
6868
return path.fileSystem.file('$dirPath$separator$filename');
6969
}
7070

71+
const int _linuxEagain = 11;
72+
const int _posixEacces = 13;
73+
const int _macOSEagain = 35;
74+
const int _windowsSharingViolation = 32;
75+
const int _windowsLockViolation = 33;
76+
77+
const _contentionErrorCodes = <String, Set<int>>{
78+
'linux': {_linuxEagain, _posixEacces},
79+
'macos': {_macOSEagain, _posixEacces},
80+
'windows': {_windowsSharingViolation, _windowsLockViolation},
81+
};
82+
7183
/// Run [callback] with this Dart process having exclusive access to [file].
7284
///
7385
/// Note multiple isolates and isolate groups in the same Dart process share
@@ -86,43 +98,54 @@ Future<T> _runUnderFileLock<T>(
8698
}) async {
8799
if (!await file.exists()) await file.create(recursive: true);
88100
final randomAccessFile = await file.open(mode: .write);
89-
var printed = false;
90-
var errorFromCallback = false;
91-
final stopwatch = Stopwatch()..start();
92-
while (timeout == null || stopwatch.elapsed < timeout) {
93-
try {
94-
await randomAccessFile.lock(FileLock.exclusive);
101+
try {
102+
var printed = false;
103+
final stopwatch = Stopwatch()..start();
104+
while (timeout == null || stopwatch.elapsed < timeout) {
105+
try {
106+
await randomAccessFile.lock(FileLock.exclusive);
107+
} on FileSystemException catch (e) {
108+
final errorCode = e.osError?.errorCode;
109+
final contentionErrorCodes =
110+
_contentionErrorCodes[Platform.operatingSystem];
111+
if (errorCode != null &&
112+
contentionErrorCodes != null &&
113+
!contentionErrorCodes.contains(errorCode)) {
114+
final message =
115+
'Could not acquire the lock to ${file.path}: ${e.osError}. '
116+
'The file system containing this project does not appear to '
117+
'support file locking (e.g. network shares like SMB or NFS). '
118+
'Build hooks require file locking. '
119+
'Please move the project to a local file system.';
120+
logger?.severe(message);
121+
throw FileSystemException(message, file.path, e.osError);
122+
}
123+
if (!printed) {
124+
logger?.finer(
125+
'Waiting to be able to obtain lock of directory: ${file.path}.',
126+
);
127+
printed = true;
128+
}
129+
// Don't busy wait, give the CPU some rest.
130+
// Magic constant taken from flutter_tools for startup lock.
131+
await Future<void>.delayed(const Duration(milliseconds: 50));
132+
continue;
133+
}
95134
try {
96135
await randomAccessFile.writeString(
97136
'Last acquired by ${Platform.resolvedExecutable} '
98137
'(pid $pid) running ${Platform.script} on ${DateTime.now()}.',
99138
);
100-
try {
101-
return await callback();
102-
} on FileSystemException {
103-
errorFromCallback = true;
104-
rethrow;
105-
}
139+
return await callback();
106140
} finally {
107141
await randomAccessFile.unlock();
108142
}
109-
} on FileSystemException {
110-
if (errorFromCallback) {
111-
rethrow;
112-
}
113-
if (!printed) {
114-
logger?.finer(
115-
'Waiting to be able to obtain lock of directory: ${file.path}.',
116-
);
117-
printed = true;
118-
}
119-
// Don't busy wait, give the CPU some rest.
120-
// Magic constant taken from flutter_tools for startup lock.
121-
await Future<void>.delayed(const Duration(milliseconds: 50));
122143
}
123-
}
124144

125-
final message = 'Could not acquire the lock to ${file.path}.';
126-
logger?.severe(message);
127-
throw TimeoutException(message, timeout);
145+
final message = 'Could not acquire the lock to ${file.path}.';
146+
logger?.severe(message);
147+
throw TimeoutException(message, timeout);
148+
} finally {
149+
await randomAccessFile.close();
150+
}
128151
}

0 commit comments

Comments
 (0)