Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dial fails unless I disable the available() check #24

Closed
fasaxc opened this issue Sep 19, 2017 · 3 comments
Closed

Dial fails unless I disable the available() check #24

fasaxc opened this issue Sep 19, 2017 · 3 comments

Comments

@fasaxc
Copy link

fasaxc commented Sep 19, 2017

First time using the library, I tried doing a Dial() (only, I'm not using Listen()) on linux, x86-64, kernel 4.4.0-92-generic but it returns an error protocol not available from available(). If I comment out the available() check then it seems to work as expected, reusing the port as I wanted.

My code looked like this:

	var d reuse.Dialer
	d.D.Timeout = 5 * time.Second
	d.D.LocalAddr, err = net.ResolveTCPAddr("tcp", "0.0.0.0:"+sourcePort)
	if err != nil {
		return err
	}
	conn, err := d.Dial("tcp", ipAddress+":"+port)
	if err != nil {
		return err
	}
	defer conn.Close()
@Stebalien
Copy link
Member

That looks like it should work. Out of curiosity, does Listen work if you do that? I'm wondering if some security software is getting in the way of us listening on localhost (to check if the port is available).

Also, if possible, could you run your program using strace and post the trace? That'll tell us what error the OS is returning.

@Coderlane
Copy link
Contributor

I'm wondering if some security software is getting in the way of us listening on localhost (to check if the port is available).

If that is the issue, it should be possible to modify checkReusePort() to use a socket instead of listening on localhost.

diff --git a/available_unix.go b/available_unix.go
index 96b74f52c01b3bb4c1c74511be6e39fad32c0140..ecd234ccbb9c3ea897614523ff1ff761bdf46e0c 100644
--- a/available_unix.go
+++ b/available_unix.go
@@ -7,6 +7,8 @@ import (
        "sync"
        "syscall"
        "time"
+
+       sockaddrnet "github.com/libp2p/go-sockaddr/net"
 )
 
 var (
@@ -15,7 +17,7 @@ var (
 )
 
 // Available returns whether or not SO_REUSEPORT is available in the OS.
-// It does so by attepting to open a tcp listener, setting the option, and
+// It does so by attepting to open a tcp socket, setting the option, and
 // checking ENOPROTOOPT on error. After checking, the decision is cached
 // for the rest of the process run.
 func available() bool {
@@ -24,13 +26,14 @@ func available() bool {
 }
 
 func checkReusePort() {
-       // there may be fluke reasons to fail to add a listener.
+       // there may be fluke reasons to fail to add a socket.
        // so we give it 5 shots. if not, give up and call it not avail.
        for i := 0; i < 5; i++ {
-               // try to listen at tcp port 0.
-               l, err := listenStream("tcp", "127.0.0.1:0")
+               // try to setup a TCP socket.
+               fd, err := socket(sockaddrnet.AF_INET,
+                       sockaddrnet.SOCK_STREAM, sockaddrnet.IPPROTO_TCP)
                if err == nil {
-                       l.Close() // Go back to the Shadow!
+                       unix.Close(fd) // Go back to the Shadow!
                        // no error? available.
                        hasReusePort = true
                        return

@Stebalien
Copy link
Member

@Coderlane good idea. That's much simpler. PR: #40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants