Skip to content

IPv4 stack should be preferred to IPv6 #775

aselder opened this Issue May 29, 2013 · 13 comments

7 participants

aselder commented May 29, 2013

We're trying to move an application to JRuby and request.ip is failing with an "invalid address" error.

Looking into things, env["REMOTE_ADDR"] is set to '0:0:0:0:0:0:0:1%0''0:0:0:0:0:0:0:1%0') blows up in all Ruby implementations.

When our app is running under MRI, env["REMOTE_ADDR"] is '".

So somehow under JRuby, env["REMOTE_ADDR"] is being set to something that IPAddr can't parse.

OS X 10.8, JRuby 1.7.4, Rails 3.2.14


+1 Yes, this can be reproduced via Rails, using the following steps:

$ rvm jruby
$ rails new foo
$ cd foo
$ rails generate scaffold bar
$ bundle exec rake db:migrate
# Edit Gemfile to add: gem 'pry-rails'
# In bars_controller.rb#index, add this:
  format.html {
$ bundle exec rails server
# Hit localhost:3000/bars
# Go to your server (pry) window and type:
[1] pry(#<BarsController>)> RUBY_DESCRIPTION
=> "jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) Client VM 1.6.0_45-b06-451-11M4406 [darwin-i386]"
[2] pry(#<BarsController>)> request.ip
=> "0:0:0:0:0:0:0:1%0"

This does not occur in MRI. In MRI (using WeBrick for both), you get "" ...

Btw, this makes ipaddr blow up, if you're using that, with an "invalid address" error. See also rails/rails#3641 and charliesome/better_errors#144 for similar reports.

JRuby Team member
atambo commented May 30, 2013

Can you try running jruby with your JRUBY_OPTS set to:

export JRUBY_OPTS=""

The option above does fix the issue and forced IPv4. I was having an issue running the better_errors gem and setting fixed the issue with getting an IPv6 ip in the request that jruby just could not handle.

Not sure how or why or what the solution is for this but its but atambo's JRUBY_OPT is a step towards a solution 👍

Shouldn't jruby just set IPv4 preferred to true since MRI seems to always stick with IPv4?

@BrandonMathis BrandonMathis referenced this issue in charliesome/better_errors Jun 12, 2013

More IPV6 and better_errors incompatability #144

@BanzaiMan BanzaiMan added a commit that closed this issue Jun 27, 2013
@BanzaiMan BanzaiMan Fix #775
Prefer IPv4 by default.
@BanzaiMan BanzaiMan closed this in f323c80 Jun 27, 2013
@BanzaiMan BanzaiMan added a commit to jruby/jruby-launcher that referenced this issue Jun 27, 2013
@BanzaiMan BanzaiMan Fix jruby/jruby#775
Prefer IPv4 stack by default.
JRuby Team member

IPAddr does not seem to be IPv6-friendly;'0:0:0:0:0:0:0:1%0') does blow up for MRI, too.

The JVM option must be passed to the JVM before JRuby spins up.

This would be done via bin/jruby.{,ba}sh (see f323c80) and JRuby launcher (jruby/jruby-launcher@cf84481, which should be included in the upcoming 1.0.18 release).


Absolutely overriding a user preference is uncool.

This point release of jruby-launcher broke the hell out of my carefully crafted automation.

Why? Because my VMs are IPv6 only. I have to have (and, ideally, also ) or nothing works. With those set, everything was working fine, but with this latest point release, because of the position of JAVA_OPTS, I get:

/opt/jdk/bin/java -Djdk.home= -Djruby.home=/usr/local/rvm/rubies/jruby-1.6.8 -Djruby.script=jruby
/sh -Djffi.boot.library.path=/usr/local/rvm/rubies/jruby-1.6.8/lib/native:/usr/local/rvm/rubies/jruby-1.6.8/lib/native/ppc-Linux:/usr/local/rvm/rubies/jruby-1.6.8/lib/native/i386-Linux:/usr/local/rvm/rubies/jruby-
1.6.8/lib/native/x86_64-Linux -cp -Xbootclasspath/a:/usr/local/rvm/rubies/jruby-1.6.8/lib/jruby.jar -Xms64m -Xmx1024m -Xss1024k
ferIPv4Stack=true org/jruby/Main /usr/local/rvm/rubies/jruby-1.6.8/bin/gem install -V --debug rubygems-update -v 1.3.5

(not the double "prefer" option, with the IPv4 one appearing later), and nothing works.

If I copy that command line exactly, but remove the bit, everything works fine.

I suggest that you:

  1. You move the bit to before the JAVA_OPTS section

  2. You detect any that's already there, and if there is one, you don't set this option

  3. Both

This took me basically an entire work day to debug, so I'm pretty cranky right now. Again: silently overriding a stated user preference is not cool. Please don't do it.


Since rvm appears to insist on installing the latest version of jruby-launcher, and since jruby-launcher, once installed, now prevents all remote operations, including gem installs, so I can't fix it, I actually don't even know how to work around this besides manually editing out the offending line. Any suggestions as to how I can get rvm to install an older jruby-launcher during its jruby install would be welcome.

JRuby Team member

@rlpowell Good points. I will figure out a better solution. Sorry for the trouble.

@BanzaiMan BanzaiMan reopened this Aug 19, 2013

:) That was very kind of you, thank you.

In the meantime, here's what I added to my automation in case anyone else hits this issue:

sed -i "/" $(ls -drt /usr/local/rvm/gems/jruby-1.6.8\@global/gems/jruby-launcher-* | tail -n 1)/argparser.cpp ; cd $(ls -drt /usr/local/rvm/gems/jruby-1.6.8\@glo
bal/gems/jruby-launcher-* | tail -n 1) ; make ; make install

FWIW, I agree that in every case except a pure IPv6-only machine, this solution was correct.

@headius headius added a commit to jruby/jruby-launcher that closed this issue Sep 3, 2013
@headius headius Revert "Fix jruby/jruby#775"
This reverts commit cf84481.
JRuby Team member
headius commented Sep 3, 2013

This is still not fixed, but I have pushed a jruby-launcher 1.0.19 that reverts the hardcoding of preferIPv4Stack.

It seems like the truly correct fix would be to fix the offending libraries to handle IPv6 properly, but that's a bigger job. For now the workaround of specifying the property may be best, either via a -J-D flag to JRuby or the JAVA_OPTS env var.

@headius headius reopened this Sep 3, 2013
JRuby Team member
headius commented Sep 3, 2013

@rlpowell We have done that for other properties like file.encoding (hardcoded to UTF-8 on Mac unless specified), so there's precedent. @BanzaiMan maybe you can try to duplicate the file.encoding logic for the preferIPv4Stack property?

@BanzaiMan BanzaiMan added a commit that closed this issue Sep 4, 2013
@BanzaiMan BanzaiMan Add ability to set preferred network stack
Set the preferred IP network stack at runtime, using the extended
property `net.preferIPv4`, with the default being `true`.

This fixes #775 without involving `jruby-launcher`.
@BanzaiMan BanzaiMan closed this in 691bf16 Sep 4, 2013
JRuby Team member

You can now use the net.preferIPv4 property to set the preferred IP network stack version.

$ jruby -v -Xnet.preferIPv4=false -e 'puts java.lang.System.getProperty("")'
jruby (1.9.3p392) 2013-09-04 ed7fe67 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_25-b15 [darwin-x86_64]

Note that is untouched, and that jruby-launcher upgrade is not required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.