Skip to content

Commit b2b6370

Browse files
committed
fix: do not block in accept
1 parent 38680e0 commit b2b6370

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/Tiny_httpd_server.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,14 @@ module Unix_tcp_server_ = struct
937937
()
938938
in
939939
940+
Unix.set_nonblock sock;
940941
while self.running do
941-
(* limit concurrency *)
942-
Sem_.acquire 1 self.sem_max_connections;
943-
try
944-
let client_sock, client_addr = Unix.accept sock in
942+
ignore (Unix.select [ sock ] [] [ sock ] 1.0 : _ * _ * _);
943+
match Unix.accept sock with
944+
| client_sock, client_addr ->
945+
(* limit concurrency *)
946+
Sem_.acquire 1 self.sem_max_connections;
947+
945948
Unix.setsockopt client_sock Unix.TCP_NODELAY true;
946949
(* Block INT/HUP while cloning to avoid children handling them.
947950
When thread gets them, our Unix.accept raises neatly. *)
@@ -955,8 +958,10 @@ module Unix_tcp_server_ = struct
955958
Sem_.release 1 self.sem_max_connections;
956959
raise e);
957960
ignore Unix.(sigprocmask SIG_UNBLOCK Sys.[ sigint; sighup ])
958-
with e ->
959-
Sem_.release 1 self.sem_max_connections;
961+
| exception Unix.Unix_error ((Unix.EAGAIN | Unix.EWOULDBLOCK), _, _)
962+
->
963+
()
964+
| exception e ->
960965
_debug (fun k ->
961966
k "Unix.accept or Thread.create raised an exception: %s"
962967
(Printexc.to_string e))

0 commit comments

Comments
 (0)