nix icon indicating copy to clipboard operation
nix copied to clipboard

nix::unistd::daemon will kill all other threads

Open blp opened this issue 4 months ago • 4 comments

Because libc's daemon() forks and then exits in the parent, only the thread that calls daemon() will survive in the child. Usually this means that one would only want to call daemon() from a single-threaded process. I am not certain whether it makes nix::unistd::daemon unsafe, but it seems like noting it in some way in the documentation would be a good idea.

blp avatar Aug 22 '25 20:08 blp

That's an excellent question. I myself have marked a function as unsafe for doing the same thing: https://docs.rs/capsicum/latest/capsicum/casper/struct.Casper.html#method.new . But it's not totally clear to me that that's correct. You might consider starting a thread on the Rust forums to discuss whether such functions ought to be considered unsafe.

asomers avatar Aug 22 '25 20:08 asomers

This issue indicates that forking a multithreaded process was considered unsafe enough that before_exec was marked unsafe and deprecated and replaced by pre_exec. So it seems to me that, by the same logic, daemon is unsafe.

blp avatar Aug 22 '25 20:08 blp

Good find. Would you care to submit a patch?

asomers avatar Aug 22 '25 21:08 asomers

Good find. Would you care to submit a patch?

Done, see https://github.com/nix-rust/nix/pull/2664.

It was counterintuitive having to add a changelog file with the PR number in the name, since I couldn't get the PR number before I submitted the PR, but I dealt with that by submitting it and then force-pushing afterward.

blp avatar Aug 22 '25 21:08 blp