Skip to content

Commit 8698753

Browse files
committed
fix(Lwt_utils.mkdir_p): Surround the Lwt_unix.mkdir call with a Lwt.catch guard
Aim: mitigate a TOC/TOU race condition which occurs when Learnocaml_process_exercise_repository.n_processes > 1, triggered by a command such as "learn-ocaml build -j 2". This isn't the only possible fix/workaround, just a concise one relying on error handling.
1 parent 31b5750 commit 8698753

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/utils/lwt_utils.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ let rec mkdir_p ?(perm=0o755) dir =
1818
(Printf.sprintf "Can't create dir: file %s is in the way" dir)
1919
| false ->
2020
mkdir_p (Filename.dirname dir) >>= fun () ->
21-
Lwt_unix.mkdir dir perm
21+
(* This exn handler mitigates a TOC/TOU race condition which
22+
occurs when [mkdir_p] is called from parallel build jobs
23+
(namely, for some "learn-ocaml build -j 2" command) *)
24+
Lwt.catch (fun () -> Lwt_unix.mkdir dir perm)
25+
(function
26+
| Unix.Unix_error(Unix.EEXIST, "mkdir", _path)
27+
when Sys.is_directory dir -> Lwt.return ()
28+
| e -> Lwt.fail e)
2229

2330
let copy_file src dst =
2431
Lwt.catch (fun () ->

0 commit comments

Comments
 (0)