Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not block in accept #73

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Tiny_httpd_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,14 @@ module Unix_tcp_server_ = struct
()
in

Unix.set_nonblock sock;
while self.running do
(* limit concurrency *)
Sem_.acquire 1 self.sem_max_connections;
try
let client_sock, client_addr = Unix.accept sock in
ignore (Unix.select [ sock ] [] [ sock ] 1.0 : _ * _ * _);
match Unix.accept sock with
| client_sock, client_addr ->
(* limit concurrency *)
Sem_.acquire 1 self.sem_max_connections;

Unix.setsockopt client_sock Unix.TCP_NODELAY true;
(* Block INT/HUP while cloning to avoid children handling them.
When thread gets them, our Unix.accept raises neatly. *)
Expand All @@ -955,8 +958,10 @@ module Unix_tcp_server_ = struct
Sem_.release 1 self.sem_max_connections;
raise e);
ignore Unix.(sigprocmask SIG_UNBLOCK Sys.[ sigint; sighup ])
with e ->
Sem_.release 1 self.sem_max_connections;
| exception Unix.Unix_error ((Unix.EAGAIN | Unix.EWOULDBLOCK), _, _)
->
()
| exception e ->
_debug (fun k ->
k "Unix.accept or Thread.create raised an exception: %s"
(Printexc.to_string e))
Expand Down