Skip to content

Commit d756f54

Browse files
rgrinbergMA0010
authored andcommitted
fix: truncate lock file (ocaml#10575)
Truncate it after successful locks and before an unlock. This makes sure that we aren't left with garbage from previous runs. Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 8fc2085 commit d756f54

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

doc/changes/10575.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Make sure to truncate dune's lock file after locking and unlocking so that
2+
users cannot observe incorrect pid's (#10575, @rgrinberg)

src/dune_util/global_lock.ml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,16 @@ module Lock = struct
5252
| `Failure -> ()
5353
| `Success ->
5454
let fd = Flock.fd t in
55+
Unix.ftruncate fd 0;
5556
write_pid fd);
5657
res
5758
;;
5859

59-
let unlock () = Lazy.force t |> Flock.unlock |> or_raise_unix ~name:"unlock"
60+
let unlock () =
61+
let lock = Lazy.force t in
62+
Unix.ftruncate (Flock.fd lock) 0;
63+
Flock.unlock lock |> or_raise_unix ~name:"unlock"
64+
;;
6065
end
6166

6267
let locked = ref false
@@ -95,11 +100,10 @@ let lock_exn ~timeout =
95100
;;
96101

97102
let unlock () =
98-
match Config.(get global_lock) with
99-
| `Disabled -> ()
100-
| `Enabled ->
101-
if !locked
102-
then (
103-
Lock.unlock ();
104-
locked := false)
103+
if !locked
104+
then (
105+
Lock.unlock ();
106+
locked := false)
105107
;;
108+
109+
let () = at_exit unlock

0 commit comments

Comments
 (0)