Skip to content

Commit

Permalink
fix: truncate lock file
Browse files Browse the repository at this point in the history
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>

<!-- ps-id: 21d117de-b760-42a5-82b9-8b60517dce90 -->
  • Loading branch information
rgrinberg committed May 24, 2024
1 parent 426571f commit 7bf4a99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc/changes/10575.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Make sure to truncate dune's lock file after locking and unlocking so that
users cannot observe incorrect pid's (#10575, @rgrinberg)
20 changes: 12 additions & 8 deletions src/dune_util/global_lock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ module Lock = struct
| `Failure -> ()
| `Success ->
let fd = Flock.fd t in
Unix.ftruncate fd 0;
write_pid fd);
res
;;

let unlock () = Lazy.force t |> Flock.unlock |> or_raise_unix ~name:"unlock"
let unlock () =
let lock = Lazy.force t in
Unix.ftruncate (Flock.fd lock) 0;
Flock.unlock lock |> or_raise_unix ~name:"unlock"
;;
end

let locked = ref false
Expand Down Expand Up @@ -95,11 +100,10 @@ let lock_exn ~timeout =
;;

let unlock () =
match Config.(get global_lock) with
| `Disabled -> ()
| `Enabled ->
if !locked
then (
Lock.unlock ();
locked := false)
if !locked
then (
Lock.unlock ();
locked := false)
;;

let () = at_exit unlock

0 comments on commit 7bf4a99

Please sign in to comment.