Unless I missed a bit from the manual there seem to be no documentation about how domains handle signals which is very sad.
Here are a few questions:
Should I assume that signals for domains behave like it does for threads (which is documented)?
If yes, does it mean that if I want to block a signal in a domain I need to use the Unix library? The Unix.sigprocmask function ? It seems to me that something is missing in the domains API here.
What is the exact pattern to block a given signal in a domain without races? Temporarily block the parent, spawn, and unblock the parent ? Something like:
let domain_spawn ~block_sigs:sigs f =
if Sys.win32 then Domain.spawn f else
let old = Unix.sigprocmask SIG_BLOCK sigs in
let finally () = ignore (Unix.sigprocmask SIG_BLOCK old) in
Fun.protect ~finally (fun () -> Domain.spawn f)
I can’t conclusively answer your question (I have not looked at the source code), but the documentation history is interesting. You have cited the documentation for OCaml-5.3 as regards Thread.sigmask, which is the same as that for OCaml-4.14 (as also is the documentation for Unix.sigprocmask ( OCaml library : Unix ). The latter mentions that “When the systhreads version of the Thread module is loaded, this function redirects to Thread.sigmask. I.e., sigprocmask only changes the mask of the current thread”.
The documentation for OCaml-5.5 somewhat improves the documentation for domains by substituting for those words “Each domain, and each thread when the Thread module is loaded, has its own signal mask. sigprocmask only changes the mask of the current domain or current thread” ( OCaml library : Unix ). It also prepends the words “Same function as Unix.sigprocmask” in the documentation for Thread.sigmask.
From this I conclude that (i) the description of signal handling with threads that you have referred to, which follows the POSIX model, is applicable also to domains in OCaml-5, (ii) you might as well treat Thread.sigmask as superceded and use Unix.sigprocmask instead, and (iii) your approach to setting sigmasks on spawning a new domain or thread (which is the standard POSIX way) should work OK (not tested).
Ah interesting, thanks. I’m not sure how I ended up reading the 5.3 manual. A few things are still left answered like how the mask is inherited – though you can answer them if you expect the POSIX way of working.
Yes, the revised text still does not explicitly say that on spawning a domain, the new domain inherits its sigmask, but it would be extraordinary if it was implemented differently.
Nor, until OCml-5.4, does OCaml provide a Unix.sigwait function and indicate in the documentation that Thread.wait_signal is the same as that function, thereby implying it works with domains as well as Thread module threads. But it would be extraordinary if Thread.wait_signal didn’t work with domains in OCaml-5.3; and there is little in it anyway, as who would waste an entire domain by having it wait on a signal? You would surely bring up a Thread.t thread for the purpose.