ocsigen / lwt

OCaml promises and concurrent I/O
https://ocsigen.org/lwt
MIT License
706 stars 172 forks source link

Pooling processes on Windows raised Bad file descriptor (waitpid) error. #85

Open braibant opened 9 years ago

braibant commented 9 years ago

Hi,

I have an example of code that runs perfectly fine on Linux and is broken on Windows. A simplified version of the code is the following one.

let worker invocation =
  let open Lwt in
  let worker = Lwt_process.open_process_none invocation in
  Lwt_unix.waitpid [(* Unix.WUNTRACED *)] worker#pid >>=
  begin
    fun (pid,status) ->
      return ()
  end

let main cores jobs =
  let pool = Lwt_pool.create cores Lwt.return in
  let f job () =  worker job in
  let e = Lwt_list.iter_p (fun job -> Lwt_pool.use pool (f job)) jobs in
  Lwt_main.run e

On Linux, jobs are properly created and there are never more than cores jobs running at once. On Windows, all the jobs are launched at once and then, I get a Bad file descriptor (waitpid) error. What could be going wrong?

braibant commented 9 years ago

I can solve this issue using Lwt_unix.system, or the like, but I would really like to know what is going wrong here.

ghost commented 9 years ago

IIRC Lwt_process already does a waitpid. Multiple waitpid might just not be possible on windows.

Instead of calling manually waitpid worker#pid you should use worker#status (might be a different method name) which does the same thing.