News for hackers. Stuff that matters.
Re:Fork() without exec() is dangerous in large programs (2016) by ridiculous_fish on Tuesday June 14, 2022 @01:51PM (#31743230)
fish shell uses posix_spawn sometimes because of its performance benefits. We can't use it in the following cases:

1. No analog to tcsetpgrp, so it's no good if job control is enabled

2. No analog to fchdir, meaning you have to synchronize with fchdir elsewhere in the progarm

3. Error codes do not convey enough information for good error messages (e.g. if a file doesn't exist, posix_spawn doesn't tell you which file)

4. Inconsistent behavior around dup2 fd redirections and CLO_EXEC.

5. Inconsistent behavior for shebangless scripts

These are basically deal-breakers so fish also supports a fork/exec path. However the performance benefits of posix_spawn are too real to ignore so fish uses posix_spawn when it can, and fork/exec when it must.

Re:Fork() without exec() is dangerous in large programs (2016) by lgg on Tuesday June 14, 2022 @05:50PM (#31746504)
Not sure if it worth the platform specific code, but for #2 macOS 10.15+ has `posix_spawn_file_actions_addfchdir_np()`.

I think most of these are deficiencies in the available posix_spawn actions, not anything inherent. Of course getting all the relevant OSes to add new functionality is a huge pain. The error handling seems bad though.

Re:Fork() without exec() is dangerous in large programs (2016) by chubot on Tuesday June 14, 2022 @02:03PM (#31743404)
OK very interesting ... is the performance benefit on certain platforms, or everywhere? A previous thread says Ninja is faster on OS X and Solaris because of it.

Though that does seem like a large number of corner cases, probably learned through painful experience :-/

Re:Fork() without exec() is dangerous in large programs (2016) by ridiculous_fish on Tuesday June 14, 2022 @02:54PM (#31744226)
It has performance benefits on both Linux (where it can use vfork) and macOS/BSDs (where it has a kernel implementation).

I tweeted a little about it here, with some perf numbers: https://twitter.com/ridiculous_fish/status/12328893907639336...

Re:Fork() without exec() is dangerous in large programs (2016) by cryptonector on Tuesday June 14, 2022 @01:53PM (#31743261)
Shells should really use `vfork()` to exec, and `fork()` for subshells (maybe).
Re:Fork() without exec() is dangerous in large programs (2016) by mort96 on Tuesday June 14, 2022 @07:08PM (#31747113)
Except that the set of things you are allowed to do after vfork() and before exec() is extremely small. I agree that things should use vfork or posix_spawn when possible, but it wouldn't surprise me at all if there's a similar list of cases when a vfork() path wouldn't be possible.
Re:Fork() without exec() is dangerous in large programs (2016) by cryptonector on Tuesday June 14, 2022 @07:11PM (#31747142)
That set was made artificially small by the Open Group, but is not actually that small.
Re:Fork() without exec() is dangerous in large programs (2016) by jepler on Tuesday June 14, 2022 @02:04PM (#31743421)
ah a good list as a companion to my simple posix_spawn pipe example in another sub-thread here! thank you!