You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Open3::capture2 and friends hang on stdin_data on Java GTE 9 #5642
Any chance we can get some eyes here with a bit of urgency @headius ? Seems open3 is broken is more than 1 way on java 11. Here's another we're seeing during installation of logstash in debian buster, reduced to its most basic form:
Running container after building (docker build --tag=test .):
% docker run -t test
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jruby.util.SecurityHelper to field java.lang.reflect.Field.modifiers
WARNING: Please consider reporting this to the maintainers of org.jruby.util.SecurityHelper
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Errno::EBADF: Bad file descriptor - echo
spawn at org/jruby/RubyProcess.java:1567
spawn at org/jruby/RubyKernel.java:1632
popen_run at /root/jruby-9.2.6.0/lib/ruby/stdlib/open3.rb:206
popen3 at /root/jruby-9.2.6.0/lib/ruby/stdlib/open3.rb:102
<main> at -e:1
[edit] I should have said please, hopefully didn't come out as any form of demand
Some interesting results. Running on MacOS, I'm not having the same issues. The original script hangs on both 9 and 11, and the @jsvd script runs to completion. So there's definitely some platform differences here.
The repro in the ticket's description also hangs on JDK11 on Darwin (see threaddump in earlier comment, which was captured on JDK11); @jsvd's issue is likely related in some way, but not the same repro.
I have managed to get the original script to run to completion after patching IO.pipe to always use native file descriptors when possible.
I believe the problem here stems from our inability to unwrap the channels given by the JDK Pipe.pipe() call when on Java 9+, due to the module encapsulation. Without those actual descriptors I suspect we're failing to do all the proper subprocess logic to ensure they're wired up to the right places.
diff --git a/core/src/main/java/org/jruby/util/io/PosixShim.java b/core/src/main/java/org/jruby/util/io/PosixShim.java
index a8fe3ab309..fa245a52d0 100644
--- a/core/src/main/java/org/jruby/util/io/PosixShim.java+++ b/core/src/main/java/org/jruby/util/io/PosixShim.java@@ -12,6 +12,7 @@ import java.nio.channels.Pipe;
import jnr.constants.platform.Errno;
import jnr.constants.platform.Fcntl;
+import jnr.enxio.channels.NativeDeviceChannel;
import jnr.posix.FileStat;
import jnr.posix.POSIX;
@@ -268,18 +269,23 @@ public class PosixShim {
public Channel[] pipe() {
clear();
try {
- Pipe pipe = Pipe.open();- Channel source = pipe.source(), sink = pipe.sink();-
if (posix.isNative() && !Platform.IS_WINDOWS) {
- // set cloexec if possible- int read = FilenoUtil.filenoFrom(source);- int write = FilenoUtil.filenoFrom(sink);- setCloexec(read, true);- setCloexec(write, true);+ int[] readwrite = {-1, -1};+ if (posix.pipe(readwrite) == 0) {+ // set cloexec if possible+ int read = readwrite[0];+ int write = readwrite[1];++ setCloexec(read, true);+ setCloexec(write, true);++ return new Channel[]{new NativeDeviceChannel(read), new NativeDeviceChannel(write)};+ }
}
- return new Channel[]{source, sink};+ // JDK logic+ Pipe pipe = Pipe.open();+ return new Channel[]{pipe.source(), pipe.sink()};
} catch (IOException ioe) {
errno = Helpers.errnoFromException(ioe);
return null;
And here we have a Ruby equivalent workaround. It appears that the main issue is being unable to set these file descriptors to close_on_exec, which causes them to be open and appear to be still live in the child process. This results in the hang.
The sink we have in the parent process feeds into the child process's stdin. In order to let the child process know there's no more input, we close that sink, which shuts down one endpoint for child's stdin. However that same descriptor is still alive in the child, so it's still open, so it never completes execution and cat keeps waiting for input.
we have the getFD method declared in interface sun.nio.ch.SelChImpl (module: module java.base)
modulator will check for isOpen("sun.nio.ch") but that fails since it isn't open unconditionally yet is open to the org.jruby.dist module - we need to adjust the module checking part to also be able to check against a provided module and all should work as in Java 8.
also it makes me wonder whether adding such internal opens everywhere (launcher etc) makes sense.
for embedded cases its a bit annoying having to --add-opens java.base/sun.nio.ch=org.jruby.dist
what if we simply tried adding those at runtime (we could extend modulator's API to do that)?
Closing the loop: this is all done for 9.2.7. We needed to open up one additional package, sun.nio.ch, and make some enhancements to our module-wrapping library to allow that opening to be seen.
Activity
yaauie commentedon Apr 2, 2019
Thread dump here: https://gist.github.com/yaauie/f1e9968e61e607eba4e2653d3c552aff
jsvd commentedon Apr 2, 2019
Any chance we can get some eyes here with a bit of urgency @headius ? Seems open3 is broken is more than 1 way on java 11. Here's another we're seeing during installation of logstash in debian buster, reduced to its most basic form:
docker build --tag=test .):[edit] I should have said please, hopefully didn't come out as any form of demand
headius commentedon Apr 3, 2019
We can try to prioritize this. I'll have a chat with @enebo.
enebo commentedon Apr 3, 2019
It is interesting to see if hang on 9 but give a pretty odd error on 11. Seems clear to we are not going native here too.
headius commentedon Apr 3, 2019
Some interesting results. Running on MacOS, I'm not having the same issues. The original script hangs on both 9 and 11, and the @jsvd script runs to completion. So there's definitely some platform differences here.
yaauie commentedon Apr 3, 2019
The repro in the ticket's description also hangs on JDK11 on Darwin (see threaddump in earlier comment, which was captured on JDK11); @jsvd's issue is likely related in some way, but not the same repro.
headius commentedon Apr 3, 2019
@yaauie Yes, I think it's a separate issue.
I have managed to get the original script to run to completion after patching
IO.pipeto always use native file descriptors when possible.I believe the problem here stems from our inability to unwrap the channels given by the JDK
Pipe.pipe()call when on Java 9+, due to the module encapsulation. Without those actual descriptors I suspect we're failing to do all the proper subprocess logic to ensure they're wired up to the right places.headius commentedon Apr 3, 2019
And here we have a Ruby equivalent workaround. It appears that the main issue is being unable to set these file descriptors to
close_on_exec, which causes them to be open and appear to be still live in the child process. This results in the hang.So it's not strictly necessary that we be using true native IO.pipe; we just need to be able to set the descriptor cloexec.
headius commentedon Apr 3, 2019
To be more specific...
The sink we have in the parent process feeds into the child process's stdin. In order to let the child process know there's no more input, we close that sink, which shuts down one endpoint for child's stdin. However that same descriptor is still alive in the child, so it's still open, so it never completes execution and
catkeeps waiting for input.enebo commentedon Apr 3, 2019
@headius I confirm that the Java patch works for original reporter script on Linux. Even @jsvd script works for me with that patch.
kares commentedon Apr 4, 2019
haven't tried the docker way - but on my Linux its enough to add opens for the
sun.nio.chpackage :and also a patch in modulator ... the full opens needed for JRuby than becomes :
we have the
getFDmethod declared ininterface sun.nio.ch.SelChImpl(module: module java.base)modulator will check for
isOpen("sun.nio.ch")but that fails since it isn't open unconditionally yet is open to theorg.jruby.distmodule - we need to adjust the module checking part to also be able to check against a provided module and all should work as in Java 8.kares commentedon Apr 4, 2019
so we'll need: headius/modulator#1 and than should be able to check open-ness against JRuby's module
kares commentedon Apr 4, 2019
also it makes me wonder whether adding such internal opens everywhere (launcher etc) makes sense.
for embedded cases its a bit annoying having to
--add-opens java.base/sun.nio.ch=org.jruby.distwhat if we simply tried adding those at runtime (we could extend modulator's API to do that)?
kares commentedon Apr 4, 2019
and here's what I mean headius/modulator#2
kares commentedon Apr 4, 2019
if those modulator updates are in, here's the patch that gets things working :
headius commentedon Apr 8, 2019
Closing the loop: this is all done for 9.2.7. We needed to open up one additional package,
sun.nio.ch, and make some enhancements to our module-wrapping library to allow that opening to be seen.