Skip to content

Commit b341726

Browse files
authored
Fix infinite waiting bug in (:std/os/flock flock/block) (#1391)
1 parent 0cee64d commit b341726

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/std/os/flock-test.ss

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(import (only-in :std/test
2+
test-suite
3+
test-case
4+
check
5+
check-exception)
6+
(only-in :std/error
7+
timeout-error?)
8+
(only-in :std/misc/string
9+
random-string)
10+
(only-in :std/os/fdio
11+
S_IRUSR
12+
S_IWUSR
13+
_open)
14+
(only-in :std/os/fcntl
15+
O_RDONLY
16+
O_CREAT)
17+
(only-in :std/os/temporaries
18+
call-with-temporary-file-name)
19+
(only-in ./flock
20+
flock/block
21+
LOCK_EX
22+
LOCK_SH))
23+
(export flock-test)
24+
25+
(def tmp-prefix "gerbil-flock-test")
26+
(def flags (fxior O_RDONLY O_CREAT))
27+
(def mode (fxior S_IRUSR S_IWUSR))
28+
29+
(def flock-test
30+
(test-suite "test :std/os/flock"
31+
(test-case "cooperative flock/block"
32+
(call-with-temporary-file-name
33+
tmp-prefix
34+
(lambda (path)
35+
(flock/block (_open path flags mode) LOCK_SH)
36+
(check (flock/block (_open path flags mode) LOCK_SH) ? void?))))
37+
38+
(test-case "uncooperative flock/block"
39+
(call-with-temporary-file-name
40+
tmp-prefix
41+
(lambda (path)
42+
(flock/block (_open path flags mode) LOCK_EX)
43+
(let ((fuse #f))
44+
(spawn (lambda ()
45+
(thread-sleep! 1)
46+
(set! fuse #t)))
47+
(check-exception (flock/block (_open path flags mode) LOCK_EX 3) timeout-error?)
48+
(check fuse => #t)))))))

src/std/os/flock.ss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
(let lp ()
4545
(unless (flock raw-or-fd op)
4646
(when deadline
47-
(unless (< deadline (##current-time-point))
47+
(when (< deadline (##current-time-point))
4848
(raise-timeout flock "Deadline for flock operation exceeded" raw-or-fd op)))
4949
(thread-sleep! (random-real))
5050
(lp))))

0 commit comments

Comments
 (0)