mdlayher / vsock

Package vsock provides access to Linux VM sockets (AF_VSOCK) for communication between a hypervisor and its virtual machines. MIT Licensed.
MIT License
336 stars 65 forks source link

Accept remains blocked after closing listener #19

Closed mxpv closed 5 years ago

mxpv commented 6 years ago

Hello. I suspect that there is a bug in vsock package (or could be I'm missing some thing). It looks like Accept remains blocked after listener is closed (and should immediately return an error instead). Here is an example:

package main

import (
    "log"
    "sync"
    "time"

    "github.com/mdlayher/vsock"
)

func main() {
    listener, err := vsock.Listen(1024)
    if err != nil {
        panic(err)
    }

    var wg sync.WaitGroup

    wg.Add(1)

    go func() {
        defer wg.Done()

        log.Print("before accept")
        _, err := listener.Accept()
        log.Printf("accept error: %v", err)
    }()

    time.Sleep(1*time.Second)

    log.Print("closing listener")
    err = listener.Close()
    if err != nil {
        log.Printf("close error: %v", err)
    }

    log.Print("waiting")
    wg.Wait()

    // Never happens because Accept never unblocks
    log.Print("exiting")
}

And the output is:

./vsock
2018/11/08 22:38:37 before accept
2018/11/08 22:38:38 closing listener
2018/11/08 22:38:38 waiting

...

I was able to reproduce this on 2 machines:

There is similar issue https://github.com/mdlayher/vsock/issues/13 but the fix is not working on go1.11.1.

Thanks!

CC @samuelkarp