Skip to content

Commit 26199cd

Browse files
committed
[hooks_runner] Fail eagerly when file locking is not supported on file system
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: #3651
1 parent 27f7678 commit 26199cd

3 files changed

Lines changed: 379 additions & 32 deletions

File tree

‎pkgs/hooks_runner/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
- **Breaking change**: Remove `KernelAssets` (and related `KernelAsset*`
44
classes) and `Target`.
55
- Drop regular dependency on `package:code_assets`.
6+
- Fail eagerly instead of waiting for timeout when running on a file system that
7+
does not support process locks.
68

79
## 1.6.4
810

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

Lines changed: 60 additions & 32 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,59 @@ 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+
var errorFromCallback = false;
104+
final stopwatch = Stopwatch()..start();
105+
while (timeout == null || stopwatch.elapsed < timeout) {
95106
try {
96-
await randomAccessFile.writeString(
97-
'Last acquired by ${Platform.resolvedExecutable} '
98-
'(pid $pid) running ${Platform.script} on ${DateTime.now()}.',
99-
);
107+
await randomAccessFile.lock(FileLock.exclusive);
100108
try {
101-
return await callback();
102-
} on FileSystemException {
103-
errorFromCallback = true;
109+
await randomAccessFile.writeString(
110+
'Last acquired by ${Platform.resolvedExecutable} '
111+
'(pid $pid) running ${Platform.script} on ${DateTime.now()}.',
112+
);
113+
try {
114+
return await callback();
115+
} on FileSystemException {
116+
errorFromCallback = true;
117+
rethrow;
118+
}
119+
} finally {
120+
await randomAccessFile.unlock();
121+
}
122+
} on FileSystemException catch (e) {
123+
if (errorFromCallback) {
104124
rethrow;
105125
}
106-
} finally {
107-
await randomAccessFile.unlock();
108-
}
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;
126+
final errorCode = e.osError?.errorCode;
127+
final contentionErrorCodes =
128+
_contentionErrorCodes[Platform.operatingSystem]!;
129+
if (errorCode != null && !contentionErrorCodes.contains(errorCode)) {
130+
final message =
131+
'Could not acquire the lock to ${file.path}. '
132+
'Running hooks_runner on a project on a file system where '
133+
'process locks are not supported is not supported. '
134+
'Please move your project to a different location.';
135+
logger?.severe(message);
136+
throw FileSystemException(message, file.path, e.osError);
137+
}
138+
if (!printed) {
139+
logger?.finer(
140+
'Waiting to be able to obtain lock of directory: ${file.path}.',
141+
);
142+
printed = true;
143+
}
144+
// Don't busy wait, give the CPU some rest.
145+
// Magic constant taken from flutter_tools for startup lock.
146+
await Future<void>.delayed(const Duration(milliseconds: 50));
118147
}
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));
122148
}
123-
}
124149

125-
final message = 'Could not acquire the lock to ${file.path}.';
126-
logger?.severe(message);
127-
throw TimeoutException(message, timeout);
150+
final message = 'Could not acquire the lock to ${file.path}.';
151+
logger?.severe(message);
152+
throw TimeoutException(message, timeout);
153+
} finally {
154+
await randomAccessFile.close();
155+
}
128156
}

0 commit comments

Comments
 (0)