Skip to content

Commit

Permalink
Do not use sigprocmask on Windows
Browse files Browse the repository at this point in the history
Fixes #85
  • Loading branch information
Jonah Beckford authored and c-cube committed Apr 18, 2024
1 parent bc34363 commit 14a4875
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Pending

- fix: No setting of sigprocmask on Windows

## 0.16

Expand Down
2 changes: 1 addition & 1 deletion src/Tiny_httpd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open struct
slice.len <- 0
end

let create ?(masksigpipe = true) ?max_connections ?(timeout = 0.0) ?buf_size
let create ?(masksigpipe = not (Sys.win32)) ?max_connections ?(timeout = 0.0) ?buf_size
?(get_time_s = Unix.gettimeofday)
?(new_thread = fun f -> ignore (Thread.create f () : Thread.t))
?(addr = "127.0.0.1") ?(port = 8080) ?sock ?middlewares () : t =
Expand Down
3 changes: 2 additions & 1 deletion src/Tiny_httpd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ val create :
{!set_top_handler} to specify how to handle incoming requests.
@param masksigpipe if true, block the signal {!Sys.sigpipe} which otherwise
tends to kill client threads when they try to write on broken sockets. Default: [true].
tends to kill client threads when they try to write on broken sockets.
Default: [true] except when on Windows, which defaults to [false].
@param buf_size size for buffers (since 0.11)
Expand Down
10 changes: 6 additions & 4 deletions src/unix/tiny_httpd_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Unix_tcp_server_ = struct
{
IO.TCP_server.serve =
(fun ~after_init ~handle () : unit ->
if self.masksigpipe then
if self.masksigpipe && not (Sys.win32) then
ignore (Unix.sigprocmask Unix.SIG_BLOCK [ Sys.sigpipe ] : _ list);
let sock, should_bind =
match self.sock with
Expand Down Expand Up @@ -83,7 +83,7 @@ module Unix_tcp_server_ = struct
(Thread.id @@ Thread.self ())
(Util.show_sockaddr client_addr));

if self.masksigpipe then
if self.masksigpipe && not (Sys.win32) then
ignore (Unix.sigprocmask Unix.SIG_BLOCK [ Sys.sigpipe ] : _ list);
Unix.set_nonblock client_sock;
Unix.setsockopt client_sock Unix.TCP_NODELAY true;
Expand Down Expand Up @@ -113,7 +113,8 @@ module Unix_tcp_server_ = struct
Sem.acquire 1 self.sem_max_connections;
(* Block INT/HUP while cloning to avoid children handling them.
When thread gets them, our Unix.accept raises neatly. *)
ignore Unix.(sigprocmask SIG_BLOCK Sys.[ sigint; sighup ]);
if not (Sys.win32) then
ignore Unix.(sigprocmask SIG_BLOCK Sys.[ sigint; sighup ]);
self.new_thread (fun () ->
try
handle_client_unix_ client_sock client_addr;
Expand All @@ -136,7 +137,8 @@ module Unix_tcp_server_ = struct
(Util.show_sockaddr client_addr)
(Printexc.to_string e)
(Printexc.raw_backtrace_to_string bt)));
ignore Unix.(sigprocmask SIG_UNBLOCK Sys.[ sigint; sighup ])
if not (Sys.win32) then
ignore Unix.(sigprocmask SIG_UNBLOCK Sys.[ sigint; sighup ])
| exception Unix.Unix_error ((Unix.EAGAIN | Unix.EWOULDBLOCK), _, _)
->
(* wait for the socket to be ready, and re-enter the loop *)
Expand Down

0 comments on commit 14a4875

Please sign in to comment.