拨号unixgram:没有此类文件或目录错误

I've written a small library that talks to wpa_supplicant. I've verified it works with a test application but I wanted to add a unit test as well. My unit test is not able to connect to the unix socket. I get the error 'no such file or directory' but both socket files get created.

lib.go

package libwpa

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net"
    "os"
    "strconv"
    "strings"
)

type Conn struct {
    uconn     *net.UnixConn //Linux specific
    localSock *os.File
}

func Connect(usock string) (*Conn, error) {
    var (
        uc  *Conn
        err error
    )
    uc = &Conn{}

    if uc.localSock, err = ioutil.TempFile("/tmp", "wpa_supplicant"); err != nil {
        return uc, err
    }
    os.Remove(uc.localSock.Name())

    uc.uconn, err = net.DialUnix("unixgram",
        &net.UnixAddr{Name: uc.localSock.Name(), Net: "unixgram"},
        &net.UnixAddr{Name: usock, Net: "unixgram"})
    if err != nil {
        return uc, err
    }

    return uc, nil
}

lib_test.go

package libwpa

import (
    "fmt"
    "io/ioutil"
    "net"
    "os"
    "testing"
)

func listen(reply chan<- []byte) {
    conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: "/tmp/foobar", Net: "unixgram"})
    if err != nil {
        fmt.Printf("failed to listen: %v
", err)
        panic(err)
    }
    defer os.Remove("/tmp/foobar")

    f, _ := ioutil.ReadDir("/tmp")
    for _, d := range f {
        fmt.Printf("%v
", d.Name())
    }

    buf := make([]byte, 2048)
    n, uaddr, err := conn.ReadFromUnix(buf)
    if err != nil {
        fmt.Printf("LISTEN: Error: %v
", err)
    } else {
        fmt.Printf("LISTEN: received %v bytes from %+v
", n, uaddr)
        fmt.Printf("LISTEN: %v
", string(buf))
    }

    conn.Close()
    reply <- buf
}

func Test_Connect(t *testing.T) {
    reply := make(chan []byte, 2)
    go listen(reply)
    _, err := Connect("/tmp/foobar")
    if err != nil {
        t.Fatalf("Failed to connect: %v", err)
    }
}

Running it I get

$ go test                                                                                                                                                                                                                                                    
--- FAIL: Test_Connect (0.00s)
        lib_test.go:42: Failed to connect: dial unixgram /tmp/wpa_supplicant208023735->/tmp/foobar: connect: no such file or directory
FAIL
exit status 1
FAIL    _/home/code/apps/go/src/crown/libwpa 0.006s

$ ls /tmp/{foobar,wpa*}                                                                                                                                                                                                                                        
/tmp/foobar=  /tmp/wpa_supplicant923064975=

My issue turned out to be a race condition in lib_test.go. I had a goroutine to create my listen socket and then immediately called Connect() from the library, but the listen socket wasn't ready yet. Adding a small delay fixed the issue.

Original:

go listen(reply)
_, err := Connect("/tmp/foobar")

Fix:

go listen(reply)
time.Sleep(time.Second * 1)
_, err := Connect("/tmp/foobar")

I know there are better long term solutions but this at least got me rolling again.

I think you forgot to put "defer" keyword before:

os.Remove(uc.localSock.Name())

in your lib.go file. Once you fix it you'll get "address already in use" error.