caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Thread.delay afraid of SIGTERM
@ 2010-08-16 11:56 Paolo Donadeo
  2010-08-16 13:43 ` [Caml-list] " Daniel Bünzli
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Donadeo @ 2010-08-16 11:56 UTC (permalink / raw)
  To: OCaml mailing list

[-- Attachment #1: Type: text/plain, Size: 2314 bytes --]

I'm writing an application in which some threads do their job, communicating
with the Concurrent Cell library <http://ccell.forge.ocamlcore.org/>. In
this context I decided to avoid 1) the multiprocess model, for the sake of
simplicity and 2) libraries like Lwt, because the threads use many external
and non lwt-aware libraries.

When the process receives a SIGTERM, I want to smoothly kill the process. To
do this I send (in the signal handler) a message to all the threads, and the
main thread waits (Thread.join) for the other worker to finish. Then the
main thread exits. To do this I install a signal handler in the main thread
(Sys.set_signal)

I found a strange behavior: when the process receives the SIGTERM, the
programs terminates with the exception Unix.Unix_error(11, "select", "").

I was able to remove all the noise and obtain a minimal program exposing
the behavior. Here it is:

$ cat thread_test.ml

open Printf;;

Random.self_init ();;

let do_something id () =
  let _ = Thread.sigmask Unix.SIG_SETMASK [15] in
  while true
  do
    Thread.delay (1.0 +. (Random.float 1.0));
    printf "Thread %d\n%!" id
  done
;;

(* MAIN THREAD FROM HERE *)
let kill_handler _ = printf "Signal caught\n%!";;

Sys.set_signal 15 (Sys.Signal_handle kill_handler);;

ignore (Thread.create (do_something 1) ());;
ignore (Thread.create (do_something 2) ());;
ignore (Thread.create (do_something 3) ());;

while true
do
  Thread.delay 5.0; (* Unix.sleep 5 WORKS! *)
  printf "Main Thread\n%!";
done;;


The process should (at least I think) catch the SIGTERM signal and continue
without problems. Instead the result is this:

$ ocamlfind ocamlopt -linkpkg -thread -package threads thread_test.ml -o
thread_test
$ ./thread_test
Thread 1
Thread 2
Thread 3
Thread 1
Thread 2
Thread 3
Thread 1
Thread 2
Main Thread
Thread 1
Thread 3
Thread 2
Thread 3
Thread 1
Thread 2
Thread 1
Thread 3
Signal caught  <--- *in* *another shell*: $ kill -15 PID
Fatal error: exception Unix.Unix_error(11, "select", "")

The exception is raised by the Thread.delay function present in the loop of
the main thread. Note that if I use Unix.sleep 5 instead of Thread.delay 5.0
the program runs as expected.

Any ideas?

Best regards,


-- 
Paolo ⠠⠵

[-- Attachment #2: Type: text/html, Size: 7368 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Thread.delay afraid of SIGTERM
  2010-08-16 11:56 Thread.delay afraid of SIGTERM Paolo Donadeo
@ 2010-08-16 13:43 ` Daniel Bünzli
  2010-08-16 14:58   ` Paolo Donadeo
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Bünzli @ 2010-08-16 13:43 UTC (permalink / raw)
  To: Paolo Donadeo; +Cc: OCaml mailing list

Paolo,

>From http://ocamlunix.forge.ocamlcore.org/threads.html#htoc63 :

val delay : float -> unit
This primitive is provided for portability with vm-level threads, but
delay s is simply an abbreviation for ignore (Unix.select [] [] [] s).
This call, unlike join, is not restarted when it is interrupted by a
signal.

So the error you see here is Unix.EINTR: the call to select in delay
is interrupted by your signal. For more information about Unix.EINTR :

http://ocamlunix.forge.ocamlcore.org/signals.html#sec/sigsyscalls

As for the difference with Unix.sleep, the latter does not raise EINTR
on signal interruption it just returns earlier with the unslept amount
(which is not reported in OCaml), see sleep(3).

Best,

Daniel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Thread.delay afraid of SIGTERM
  2010-08-16 13:43 ` [Caml-list] " Daniel Bünzli
@ 2010-08-16 14:58   ` Paolo Donadeo
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Donadeo @ 2010-08-16 14:58 UTC (permalink / raw)
  To: OCaml mailing list

Thanks Daniel and Philippe, problem solved.

The lesson here is: read "Unix system programming in Objective Caml"
*before* starting Unix system programming in OCaml :-)


-- 
Paolo ⠠⠵


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-08-16 14:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-16 11:56 Thread.delay afraid of SIGTERM Paolo Donadeo
2010-08-16 13:43 ` [Caml-list] " Daniel Bünzli
2010-08-16 14:58   ` Paolo Donadeo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).