Skip to content

Commit

Permalink
Use accept_n in Lwt_io.establish_server
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Jun 17, 2018
1 parent e658860 commit d5b429d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/unix/lwt_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1592,20 +1592,30 @@ let establish_server_generic

let rec accept_loop () =
let try_to_accept =
Lwt_unix.accept listening_socket >|= fun x ->
Lwt_unix.accept_n listening_socket 10_000 >|= fun x ->
`Accepted x
in

Lwt.pick [try_to_accept; should_stop] >>= function
| `Accepted (client_socket, client_address) ->
begin
try Lwt_unix.set_close_on_exec client_socket
with Invalid_argument _ -> ()
end;
| `Accepted (connections, maybe_error) ->
connections
|> List.iter begin fun (client_socket, client_address) ->
begin
try Lwt_unix.set_close_on_exec client_socket
with Invalid_argument _ -> ()
end;

connection_handler_callback client_address client_socket;
connection_handler_callback client_address client_socket
end;

accept_loop ()
(* Note: this means an exception in accept_n will get passed to
Lwt.async_exception_hook and terminate the process. This was an
oversight in the previous implementation of this function, however, and
will be fixed separately. *)
begin match maybe_error with
| Some exn -> Lwt.fail exn
| None -> accept_loop ()
end

| `Should_stop ->
Lwt_unix.close listening_socket >>= fun () ->
Expand Down

0 comments on commit d5b429d

Please sign in to comment.