Giter Club home page Giter Club logo

Comments (15)

weissi avatar weissi commented on August 15, 2024 3

thanks @pushkarnk you uncovered an amazing embarrassing NIO bug: We currently only store one socket option value per type. Ie. only one of ChannelOptions.socket. So setting SO_REUSEADDR will unset all other ChannelOptions.socket(*) that were set before... Working on a fix, have a failing test already.

from kitura-nio.

weissi avatar weissi commented on August 15, 2024 3

I'm working on apple/swift-nio#597 to fix it and will release in 1.9.3

from kitura-nio.

nethraravindran avatar nethraravindran commented on August 15, 2024 1

@pushkarnk @saiHemak setting SO_REUSEPORT first resolves the issue.

import NIO 

let bootstrap1 = ServerBootstrap(group: MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount))
     .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: 0)
    .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)  
  
    .childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
var serverChannel1 = try! bootstrap1.bind(host: "0.0.0.0", port: 8080).wait()

let channel = try! ClientBootstrap(group: MultiThreadedEventLoopGroup(numberOfThreads: 1)) 
    .connect(host: "localhost", port: 8080).wait()

print("channel", channel)

try! serverChannel1.close().wait()

_ = try! bootstrap1.bind(host: "0.0.0.0", port: 8080).wait()

from kitura-nio.

nethraravindran avatar nethraravindran commented on August 15, 2024 1

Thanks @weissi and @pushkarnk

PR #82

from kitura-nio.

pushkarnk avatar pushkarnk commented on August 15, 2024

We should try using ephemeral ports for all the tests.

from kitura-nio.

saiHemak avatar saiHemak commented on August 15, 2024

Few KiturNetTests ephemeral ports but not all the tests are using the same. Discussed with @djones6 and agreed to port the KituraTests to use ephemeral ports.

from kitura-nio.

weissi avatar weissi commented on August 15, 2024

@pushkarnk setting

.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)

should fix that. I reckon KituraNet just sets that automatically whilst NIO wants the user to specify it explicity.

from kitura-nio.

ianpartridge avatar ianpartridge commented on August 15, 2024

@pushkarnk ^ did you see this?

from kitura-nio.

pushkarnk avatar pushkarnk commented on August 15, 2024

@ianpartridge Yes, we tried SO_REUSEADDR in the past and it doesn't solve this problem.

@weissi I remember discussing this issue with you over Slack back in May. SO_REUSEADDR wasn't helping us and you suggested we try SO_REUSEPORT.

from kitura-nio.

pushkarnk avatar pushkarnk commented on August 15, 2024

@weissi Now I recall it better, we needed SO_REUSEPORT for a particular test called testServersSharingPort. Looks like SO_REUSEADDR is enough for this issue.

from kitura-nio.

weissi avatar weissi commented on August 15, 2024

@pushkarnk yes, testServersSharingPort sounds like two servers bind on the same port which would be SO_REUSEPORT (and a new enough Linux version)

from kitura-nio.

weissi avatar weissi commented on August 15, 2024

in any case, I'd recommend SO_REUSEADDR for all tests and SO_REUSEPORT for the ones where you actually need to have two (or more) servers bind to the same port at the same time.

from kitura-nio.

pushkarnk avatar pushkarnk commented on August 15, 2024

@weissi This is the gist of the problem we're facing using both SO_REUSEADDR and SO_REUSEPORT

Consider this NIO program (note the commented line of code):

import NIO 

let bootstrap1 = ServerBootstrap(group: MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount))
    .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)  
  //.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: 0)
    .childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
var serverChannel1 = try! bootstrap1.bind(host: "0.0.0.0", port: 8080).wait()

let channel = try! ClientBootstrap(group: MultiThreadedEventLoopGroup(numberOfThreads: 1)) 
    .connect(host: "localhost", port: 8080).wait()

print("channel", channel)

try! serverChannel1.close().wait()

_ = try! bootstrap1.bind(host: "0.0.0.0", port: 8080).wait()

Run the above program. You should see no error.

Now, remove the comment and include that line of code:

import NIO 

let bootstrap1 = ServerBootstrap(group: MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount))
    .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)  
    .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: 0)
    .childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
var serverChannel1 = try! bootstrap1.bind(host: "0.0.0.0", port: 8080).wait()

let channel = try! ClientBootstrap(group: MultiThreadedEventLoopGroup(numberOfThreads: 1)) 
    .connect(host: "localhost", port: 8080).wait()

print("channel", channel)

try! serverChannel1.close().wait()

_ = try! bootstrap1.bind(host: "0.0.0.0", port: 8080).wait()

Wait until netstat -antup does NOT show an entry for 127.0.0.1:8080.

Compile and run the second program. The second bind() fails with an error!

This means, not setting SO_REUSEPORT has a different effect than setting SO_REUSEPORT = 0. I'd think they are the same!

Is this expected? Perhaps its something to do with the loopback connection?

cc @nethraravindran

from kitura-nio.

pushkarnk avatar pushkarnk commented on August 15, 2024

@saiHemak @nethraravindran
We need to set SO_REUSEADDR to 1 by default and if allowPortReuse = false we must try to simply not include this line of code.

from kitura-nio.

nethraravindran avatar nethraravindran commented on August 15, 2024

@saiHemak ^^^

from kitura-nio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.