A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/ocsigen/lwt/issues/789 below:

Lwt_unix.fork not canceling pending promises · Issue #789 · ocsigen/lwt · GitHub

The documentation Lwt_unix.fork claims that

in the child process all pending jobs are canceled

However it seems it is not entirely the case, as demonstrated by the example bellow.
In this example I fork processes in a parallel iteration over a list. I expect that only the main process forks new jobs, however some forked processes do fork again while they obviously shouldn't.

With sequential iteration on the list, the problematic behavior does not happen.

Warning: Don't increase the size of the list, it rapidly leads to a fork bomb that exhausts available pids which crashes the system.

Tested with Lwt 4.2.1(ocaml 4.07.1) and 5.3.0 (ocaml 4.10.0).

open Lwt

let ( -- ) i j = List.init (j - i + 1) (fun x -> x + i)

let main_pid = Unix.getpid ()

let fork_one i =
  Lwt_io.flush_all () >>= fun () ->
  if Unix.getpid () <> main_pid then
    Format.printf
      "Error: Forking from non-main process %d (%d in the list) child of %s@."
      (Unix.getpid ())
      i
      (let ppid = Unix.getppid () in
       if ppid = main_pid then "main" else string_of_int ppid) ;
  match Lwt_unix.fork () with
  | 0 -> Lwt_unix.sleep 0.1 >>= fun _ -> exit 0
  | pid ->
      Format.printf "%d forked from %d@." pid (Unix.getpid ()) ;
      Lwt_unix.waitpid [] pid >>= fun _ -> Lwt.return_unit

let fork_many i = Lwt_list.map_p fork_one (0 -- i)

let _ =
  Format.printf "Main process pid is %d@." (Unix.getpid ()) ;
  Lwt_main.run (fork_many 3)

adrianbrink, mbouaziz, germanD and aantron


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4