-
Notifications
You must be signed in to change notification settings - Fork 20
Add setsid
method to CommandExt
trait
#184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What if it is not supported? Is it possible to have a fallback? e.g. python issue 37586 discusses a problem with support added in macOS 10.15, where apparently older versions would just ignore the unknown flag. (AFAICS the For Linux, my manpage says glibc added this flag in 2.26 (but our minimum is 2.17), and musl WHATSNEW says they added it in 1.1.17. I don't see support in uClibc at all. |
One option would be to return Another option would be to provide a more generic But if it's okay to just ignore when it's not supported, I think that's probably the best option. As long as it's documented well. |
Is setsid unsupported on any platforms, or just the posix_spawn flag? Certainly on macOS This means we could opportunistically implement it with fork/exec where the posix_spawn flag isn't available — the posix_spawn use would just be an optimization on top of that. (We already have some cases like this in the code) |
Yeah, |
Are there any drawbacks to always using |
Thanks for the comments about backwards compatibility, I hadn't really thought about that so thanks for the input! I think that transparently using
I'm not aware of any drawbacks, I spent a bit of time looking at the history of Here's the The Austin Group discussion about adding it to POSIX is here: https://www.austingroupbugs.net/view.php?id=1044 |
I feel like it should be possible to do at compile time. |
We compile |
Ah, good point. At best, it would only be possible at compile time if using
Does that matter though? If we're just using it opportunistically and make no guarantees, can't we just always pass the flag when it won't cause an error? |
It matters because it's a silent failure -- we won't know that we need to use the fork/exec fallback to call |
We could exclude macos from the posix_spawn path until we raise the minimum to 10.15? |
@cuviper Ah yeah makes sense. I misunderstood some context. I agree with the plan to have
That would be unfortunate. Can we detect if we're on a supported OS version? Maybe perform the check once in the pre-main init code? |
Yes, the check of macOS version would be easy (although we don't have anywhere that does it already). We shouldn't do it in pre-main because Rust may not be in control of main, but that doesn't really matter, I would not really be in favor of doing it there anyway. |
Good point. Any idea how expensive the version check is? Would it need to be memoized? |
It's pretty cheap. Apple doesn't memoize the calls to the function that |
Thanks for the discussion here, sorry it's taken me some time to respond. It looks like the next step is to implement logic to check whether setsid is supported by POSIX spawn at run time. I can start working on that and update the linked PR with the changes. Are there any good examples or pointers to how this is done in other parts of the stdlib? Thanks! |
We discussed this during today's libs-api meeting. We're on board with it especially due to the performance savings from posix_spawn which would not otherwise be available to user code. |
Proposal
Problem statement
Currently, if you want to use
Command
to spawn a new process and create a new session, then you need to use thepre_exec
method. However, callingpre_exec
means that the POSIX spawn fast path can't be used which makes spawning a new process more expensive than necessary.By exposing the ability to pass the
POSIX_SPAWN_SETSID
flag to the POSIX spawn method it becomes possible to use the fast path to create processes. It also removes the need forunsafe
code.Motivation, use-cases
I don't have example code to share here, but I've been developing and maintaining a system that spawns multiple processes a second. These processes are started in new sessions to allow the main process to handle graceful shutdown upon receiving CTRL+C from the terminal.
Using the existing
pre_exec
method, the fork + exec required uses considerable kernel CPU. Switching to using the POSIX spawn fast path reduced the kernel CPU usage by ~60% IIRC.Solution sketches
The
std::os::unix::process::CommandExt
trait can be updated to include a newsetsid
method, with the following signature:If a user calls
.setsid()
then thePOSIX_SPAWN_SETSID
flag will be passed to POSIX spawn.A slightly different interface could have the following signature:
This would allow the caller to call
setsid(false)
to remove thesetsid
option from aCommand
that previously had it set. However, I'm not sure how useful this is in practice and it makes the method more verbose to use.Links and related work
In my excitement to add this, I've done things in the wrong order and have submitted a PR and tracking issue already.
Rust PR: rust-lang/rust#105377
Rust Tracking issue: rust-lang/rust#105376
Man page for
posix_spawn
: https://man7.org/linux/man-pages/man3/posix_spawn.3.htmlWhat happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: