diff --git a/lib/fluent/plugin_helper/server.rb b/lib/fluent/plugin_helper/server.rb index 3ac8ec251b..7b9cea0649 100644 --- a/lib/fluent/plugin_helper/server.rb +++ b/lib/fluent/plugin_helper/server.rb @@ -355,7 +355,11 @@ def server_create_tcp_socket(shared, bind, port) sock = if shared server_socket_manager_client.listen_tcp(bind, port) else - TCPServer.new(bind, port) # this method call can create sockets for AF_INET6 + # TCPServer.new doesn't set IPV6_V6ONLY flag, so use Addrinfo class instead. + # backlog will be set by the caller, we don't need to set backlog here + tsock = Addrinfo.tcp(bind, port).listen + tsock.autoclose = false + TCPServer.for_fd(tsock.fileno) end # close-on-exec is set by default in Ruby 2.0 or later (, and it's unavailable on Windows) sock.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) # nonblock diff --git a/test/plugin_helper/test_server.rb b/test/plugin_helper/test_server.rb index e54c20c8e5..d71dc5788e 100644 --- a/test/plugin_helper/test_server.rb +++ b/test/plugin_helper/test_server.rb @@ -538,6 +538,19 @@ class Dummy < Fluent::Plugin::TestBase assert_equal ["yayfoo\n", "yayfoo\n", "yayfoo\n"], lines assert_equal ["closed", "closed", "closed"], callback_results end + + test 'can listen IPv4 / IPv6 together' do + omit "IPv6 unavailable here" unless ipv6_enabled? + + assert_nothing_raised do + @d.server_create_tcp(:s_ipv4, PORT, bind: '0.0.0.0', shared: false) do |data, conn| + # ... + end + @d.server_create_tcp(:s_ipv6, PORT, bind: '::', shared: false) do |data, conn| + # ... + end + end + end end sub_test_case '#server_create_udp' do