Skip to content
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

Child process erroring out instead of blocking on write to stdout on Ruby 3.0? #90

Open
ClearlyClaire opened this issue Apr 16, 2021 · 13 comments

Comments

@ClearlyClaire
Copy link

I'm trying to port the application I am working on to Ruby 3.0, and one of the issues I have is some dependency “randomly” failing to run commands with large output. I have tracked down the issue to posix-spawn as removing that gem makes the library fall back to Process.spawn which seems to work as expected.

It seems that for some reason, when using posix-spawn on Ruby 3.0, the child process will error out with “Error 11: Resource temporarily unavailable” when filling the pipe's buffer, while on Ruby 2.7, it will simply block as expected.

The following simple code demonstrates the issue:

require 'posix/spawn'

pid, stdin, stdout, stderr = POSIX::Spawn.popen4('yes | head -n 20000000')
stdin.close
puts "output size: #{stdout.read.size}"
puts "stderr: #{stderr.read}"
stdout.close
stderr.close
Process.waitpid pid
@ClearlyClaire
Copy link
Author

ClearlyClaire commented Apr 16, 2021

Writing a simple C program confirms that for some reason, stdout is set to non-blocking on Ruby 3.0, but not on Ruby 2.7:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(void) {
  printf("%d\n", fcntl(STDOUT_FILENO, F_GETFL) & O_NONBLOCK);

  return 0;
}

Indeed, unlike Ruby 2.7, Ruby 3.0 makes all pipes non-blocking by default, and as far as I understand it, manually undoes that when passing stdout etc. to child processes with built-in popen/exec etc.

This means that posix-spawn probably has to do the same thing, not only for POSIX::Spawn.popen4, but probably for POSIX::Spawn.spawn as well.

@catwell
Copy link

catwell commented May 10, 2022

For reference this fork apparently solves the problem for spawn: jhawthorn@529e214

@jhawthorn Is that patch used at GitHub? If so is there any chance to upstream it?

Regarding popen4 it can be patched directly into the posix-spawn Ruby code.

@jhawthorn
Copy link

@catwell we are no longer using this gem. Ruby has had a built-in and efficient Process.spawn for a long time so I don't think there's any reason to use this gem anymore.

@catwell
Copy link

catwell commented May 11, 2022

OK, thanks for the answer! I was still using it in part because I prefer its API - see this blobpost by @jnunemaker for an example - but I can switch to native Process.spawn in this case.

I also suggest the maintainers of posix-spawn mark it as officially deprecated in the README.

@midnight-wonderer
Copy link

@catwell
I was late to the party and could not quite follow.

Why did you suggest that the gem should be deprecated? What changed in the built-in Process.spawn?
What do you suggest in place of POSIX::Spawn::Child?

@catwell
Copy link

catwell commented May 28, 2022

@midnight-wonderer I only suggest the gem should be deprecated if the maintainers don't use it anymore and don't intend to fix its bugs.

Process.spawn is not what changed, it's IO (and pipes in particular) that is non-blocking by default. But pipes used for stdin / stderr / stdout should not be non-blocking, so they should be made blocking explicitly.

@midnight-wonderer
Copy link

Hey, thanks a bunch for the explanation; that answered most of my questions.

However,
from a couple of dialogs ago, it seems that GitHub didn't use the gem anymore and switched back to Process::spawn.
Made me wonder what's different from now and then when POSIX::Spawn was created; what was changed in Process::spawn?
How does it make a comeback?

I don't quite understand low-level details of OS kinds of stuff, and since you considered switching to Process::spawn too, so I asked in succession because you seem to understand that one too.

@catwell
Copy link

catwell commented May 28, 2022

Not entirely sure that is what he was referring to but Ruby now uses vfork when it can. Like I said that wasn't my reason for using this gem anyway.

@midnight-wonderer
Copy link

That's it, thank you.
Basically, Ruby is using vfork natively now.

I just modified and ran a benchmark of POSIX::Spawn on my computer using ruby:3-slim Docker image on an Ubuntu 20.04 host. Here's the result:

$ bundle exec rake benchmark
cp tmp/x86_64-linux/posix_spawn_ext/3.1.2/posix_spawn_ext.so lib/posix_spawn_ext.so
/usr/local/bin/ruby -Ilib bin/posix-spawn-benchmark
benchmarking fork/exec vs. posix_spawn over 1000 runs at 100M res
                                               user     system      total        real
fspawn (fork/exec):                        0.097599   2.031621   6.810560 (  6.756999)
pspawn (posix_spawn):                      0.006890   0.025629   0.251916 (  0.244938)
spawn (native):                            0.003529   0.032049   0.195628 (  0.188914)
Child (posix_spawn):                       0.037435   0.037484   0.294511 (  0.275843)
open3 (native):                            0.058668   0.037705   0.262761 (  0.226236)

The open3 implementation is using similar IO::select as POSIX::Spawn::Child.
The posix-spawn gem is worse than native, time-wise.

The gem should not be used in new applications because it doesn't provide advantages over native Ruby implementation anymore. And GitHub + the gem maintainers have moved on.

For the Child API, I think it is trivial to implement something similar with Open3.

@catwell
Copy link

catwell commented May 29, 2022

Yes, that is probably what I will do now. In the meantime we monkey patched this gem to have it work on Ruby 3 since we wanted to avoid such changes at the same time as our Ruby 3 upgrade.

@jjb
Copy link

jjb commented Apr 6, 2023

does anyone know where I can find discussion about the changes with Process::spawn in ruby 3?

Searching the web, ruby codebase, and ruby changelogs for vfork doesn't come up with anything

This code was committed 9 years ago

image

https://github.com/ruby/ruby/blame/7b27ad9ad36dbfd2ec6571b0ed15fbc4aa65d460/process.c#L58

If anyone has better search terms for me to use, much appreciate, thanks!

@catwell
Copy link

catwell commented Apr 9, 2023

@jjb Yes the vfork change is older than Ruby 3, it shipped with Ruby 2.2.

POSIX::Spawn is much older though, it was created for Ruby 1.9, back when fork performance was bad, as said at the top of the README:

2023-04-09_15-44

So since Ruby 2.2 it is no longer necessary to use POSIX:Spawn to get good fork performance, but it still worked well until Ruby 3.

The breaking change in Ruby 3 is that pipes, used internally by POSIX:Spawn, became non-blocking by default, as explained earlier in the thread. If you're looking for the commit, that change happened here.

@jjb
Copy link

jjb commented Apr 9, 2023

Thanks for that info! Wow, interesting - I didn't discover posix-spawn until far after 2.2 and used it everywhere I could.

I did understand that breaking change prompted the discussion in this ticket - what I was looking for was info about the change in ruby which made posix-spawn irrelevant, so I guess back in 2.2. Looks like the release notes cover it! https://www.ruby-lang.org/en/news/2014/12/25/ruby-2-2-0-released/

Experimental support for using vfork(2)....

ruby/ruby@9b16f90

and looks like it's never used on macos and solaris: https://github.com/ruby/ruby/blob/master/configure.ac

luanzeba added a commit to luanzeba/progeny that referenced this issue May 1, 2023
The `posix-spawn` gem was created to address performance issues in Ruby
when spawning child processes. Those issues have been addressed since
Ruby 2.2 so the gem is technically not necessary anymore.

The performance of Ruby's `Process.spawn` is equivalent and sometimes a
little faster than the custom `POSIX::Spawn#_pspawn` method created by
the `posix-spawn` gem.

Additionally, the `posix-spawn` C extension is not compatible with Ruby
3.0. In Ruby 3.0, `IO.pipe` and others return nonblocking pipes - see
ruby/ruby@6a65f2b.

As you can see in the commit above, these are moved back to blocking (at
least for `stdin`, `stdout` and `stderr`) inside Ruby's `Process.spawn`
implementation. When using `posix-spawn` this results in the child
process getting an `EAGAIN` sometimes, which it doesn't expect as it
doesn't expect pipes to be non-blocking. This seems to happen when
writing more than 64KB on Linux.

More details in rtomayko#90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants