I faced a critical issue while using go1.9.2 windows/386 on Windows XP SP3.
The Accept() blocks forever, even if I set a timeout using SetDeadline(), until the program exits.
I consider it critical because I can't stop listeners when I need to. Additionally, conn.Read() blocks forever as well, but I didn't include it to the example to make is simpler.
package main
import (
"fmt"
"net"
"time"
)
func main() {
listener, err := net.Listen("tcp", ":8888")
if err != nil {
fmt.Println(err.Error())
return
}
tcplistener := listener.(*net.TCPListener)
tcplistener.SetDeadline(time.Now().Add(time.Second * 2))
fmt.Println("Listener started")
go func() {
defer func() {
tcplistener.Close()
fmt.Println("Listener closed")
}()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println(err.Error())
return
}
conn.Write([]byte("OK
"))
conn.Close()
}
}()
time.Sleep(time.Second * 10)
fmt.Println("Exit")
}
Terminal output:
Listener started
Exit
In contrast, when I run this program on Windows 7 under VirtualBox (on the same Windows XP SP3), Accept() successfully unblocks after a timeout period.
Terminal output:
Listener started
accept tcp [::]:8888 i/o timeout
Listener closed
Exit