Skip to content

Commit e8fa0a3

Browse files
meshcollidersipa
andcommitted
Fix WSL file locking by using flock instead of fcntl
Co-authored-by: sipa <[email protected]>
1 parent 6ae99aa commit e8fa0a3

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/fs.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#ifndef WIN32
88
#include <fcntl.h>
9+
#include <string>
10+
#include <sys/file.h>
11+
#include <sys/utsname.h>
912
#else
1013
#ifndef NOMINMAX
1114
#define NOMINMAX
@@ -47,20 +50,38 @@ FileLock::~FileLock()
4750
}
4851
}
4952

53+
static bool IsWSL()
54+
{
55+
struct utsname uname_data;
56+
return uname(&uname_data) == 0 && std::string(uname_data.version).find("Microsoft") != std::string::npos;
57+
}
58+
5059
bool FileLock::TryLock()
5160
{
5261
if (fd == -1) {
5362
return false;
5463
}
55-
struct flock lock;
56-
lock.l_type = F_WRLCK;
57-
lock.l_whence = SEEK_SET;
58-
lock.l_start = 0;
59-
lock.l_len = 0;
60-
if (fcntl(fd, F_SETLK, &lock) == -1) {
61-
reason = GetErrorReason();
62-
return false;
64+
65+
// Exclusive file locking is broken on WSL using fcntl (issue #18622)
66+
// This workaround can be removed once the bug on WSL is fixed
67+
static const bool is_wsl = IsWSL();
68+
if (is_wsl) {
69+
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
70+
reason = GetErrorReason();
71+
return false;
72+
}
73+
} else {
74+
struct flock lock;
75+
lock.l_type = F_WRLCK;
76+
lock.l_whence = SEEK_SET;
77+
lock.l_start = 0;
78+
lock.l_len = 0;
79+
if (fcntl(fd, F_SETLK, &lock) == -1) {
80+
reason = GetErrorReason();
81+
return false;
82+
}
6383
}
84+
6485
return true;
6586
}
6687
#else

0 commit comments

Comments
 (0)