Skip to content

WEBRick::HTTPServer creates ipv6only socket for host :: #99

Open

Description

@pohlarized

The IPv6 host :: is kind of analogous to the IPv4 address 0.0.0.0, however it should usually (depending on the OS) listen for traffic coming from IPv4 and IPv6 addresses, unless specified differently by the OS.

However, i have observed that across different linux distros i have tried (manjaro, arch, debian), WEBRick will open its socket in ipv6-only mode.
I assume that the reason is this line as using the Socket.tcp_server_sockets function manually leads to the same result.
However, e.g. using the TCPServer from the socket stdlib opens a socker that's not in ipv6-only mode.

Sample code i have used:

require 'webrick'

server = WEBrick::HTTPServer.new Port: 8080, Host: '::', DocumentRoot: '~/'

trap 'INT' do
  server.shutdown
end

server.start

I have tested this with webrick 1.7.0 on ruby 3.1.1 and on ruby 2.6.3, both yielding the same result.

I have observed the mode using ss -tlne, where the local address is shown as [::]:8080, which indicates ipv6-only mode in ss, while it should be *:8080 in non-ipv6-only mode.
It also says v6only:1 in the process description, while it should say v6only:0.

Also here's the sample code using TCPServer that correctly opens the socket:

require 'socket'

server = TCPServer.new('::', 8080)

loop do
  client = server_socket.accept
  client.puts 'hello'
  client.close
end

Activity

  1. MarkLodato commented on Feb 8, 2023

    @MarkLodato

    Does anyone have a workaround for this to force webrick to serve on both IPv4 and IPv6?

    I'm using jekyll serve, which uses WEBrick under the hood, and chrome randomly chooses IPv4 vs IPv6, even if I explicitly go to http://127.0.0.1:4000 or http://[::1]:4000. This means that half the assets on my page fail to load because some connect over IPv4 and some over IPv6, and only one or the other are actually hitting a listening port.

  2. tisba commented on Aug 15, 2024

    @tisba
    Contributor

    Same issue with WEBrick 1.8.1 on macOS under Ruby 3.3.4 (arm64-darwin23).

  3. tisba commented on Sep 21, 2024

    @tisba
    Contributor

    hmm, I don't know what I did test back in August, but for me using Host: '::' (or not specifying Host at all) does the right thing. 🙈

    $ ruby -rwebrick -e "WEBrick::HTTPServer.new(Port: 8888, Host: '::', DocumentRoot: '.').start"
    

    Will bind correctly:

    $ ss -tlne
    State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process
    LISTEN    0         4096                 0.0.0.0:8888              0.0.0.0:*        ino:697550 sk:1 cgroup:/ <->
    LISTEN    0         4096                    [::]:8888                 [::]:*        ino:697551 sk:2 cgroup:/ v6only:1 <->
    

    This also works on macOS too. Used Ruby 3.3.5 and WEBrick 1.8.1 for testing.

    I also tested with Ruby 3.1.1 and WEBrick 1.7.0, like @pohlarized did, and it also worked as expected.

  4. schneems commented on Nov 27, 2024

    @schneems

    I confirm that I see webrick bind to both tcp4 and 6

    $  ruby -rwebrick -e "WEBrick::HTTPServer.new(Port: 8888, Host: '::', DocumentRoot: '.').start"
    [2024-11-27 09:50:09] INFO  WEBrick 1.9.0
    [2024-11-27 09:50:09] INFO  ruby 3.3.1 (2024-04-23) [arm64-darwin23]
    [2024-11-27 09:50:09] INFO  WEBrick::HTTPServer#start: pid=80538 port=8888
    
    $ netstat -an | grep 'LISTEN' | grep 8888
    tcp4       0      0  *.8888                 *.*                    LISTEN
    tcp6       0      0  *.8888                 *.*                    LISTEN
    

    Though it's weird that it's not showing up as tcp46 like with puma https://gist.github.com/schneems/ffc51f61e6ee213b29e676c89e0579d0.

  5. schneems commented on Nov 27, 2024

    @schneems

    However, when booting via rackup it only binds to tcp6 and not tcp4. My conclusion would be that the problem is the interplay between rackup and webrick. I'm unsure which side (webrick or rackup) holds this problem.

    $ cat Gemfile
    source "https://rubygems.org"
    
    gem "rack"
    gem "rackup"
    gem "webrick"
    $ cat config.ru
    # config.ru
    
    require 'rack'
    
    # Define a simple Rack application
    app = Proc.new do |env|
      ['200', {'Content-Type' => 'text/html'}, ['Hello, world!']]
    end
    
    # Use Rack::Builder to build the application
    run Rack::Builder.new {
      run app
    }
    $ be rackup --host "::"
    [2024-11-27 09:54:42] INFO  WEBrick 1.9.0
    [2024-11-27 09:54:42] INFO  ruby 3.3.1 (2024-04-23) [arm64-darwin23]
    [2024-11-27 09:54:42] INFO  WEBrick::HTTPServer#start: pid=95732 port=9292
    
    $ netstat -an | grep 'LISTEN' | grep 9292
    tcp6       0      0  *.9292                 *.*                    LISTEN
    
  6. tisba commented on Nov 27, 2024

    @tisba
    Contributor

    Though it's weird that it's not showing up as tcp46 like with puma[…]

    I have to try finding my debugging notes on this one. This is a combination of the local network interface setup and how the TCP socket gets created. The the effect should be the same.

    However, when booting via rackup it only binds to tcp6 and not tcp4.

    99% sure that's a bug in rackup. There is some weird defaulting logic in the WEBrick adapter which results in localhost being passed into as Host: for WEBrick::HTTPServer.new. AFAIK setting RACK_ENV to something other than development changes this. Super weird, IMO. See https://github.com/rack/rackup/blob/main/lib/rackup/handler/webrick.rb#L19-L35.

  7. tisba commented on Nov 27, 2024

    @tisba
    Contributor

    oh and also: I think WEBrick::HTTPServer.new ignores Host anyway. BindAddress is what you want to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions