I'm trying to implement a simple echo server in Go (go1.8.3 windows/amd64) on Windows 10, but I have this weird panic happening some time after getting a connection from a client (connecting with ncat). I can send and receive messages, but it will crash after a while no matter what I do, and I don't know why.
The code is
package main
import (
"bufio"
"log"
"net"
)
func echo(conn net.Conn, c chan int) {
c <- 1
reader := bufio.NewReader(conn)
msg, err := reader.ReadBytes('
')
if err != nil {
log.Printf("error reading from the reader: %v", err)
return
}
conn.Write(msg)
}
func handleConnection(conn net.Conn) {
log.Print("Got connection from: ", conn.RemoteAddr())
conn.Write([]byte("Hello, this is a Go echo server
"))
c := make(chan int)
for {
go echo(conn, c)
<-c
}
}
func main() {
listener, err := net.Listen("tcp", "127.0.0.1:3000")
if err != nil {
log.Fatalf("couldn't create a tcp server: %v", err)
}
for {
conn, err := listener.Accept()
defer conn.Close()
if err != nil {
log.Printf("couldn't create a connection: %v", err)
return
}
go handleConnection(conn)
}
}
The error is
panic: net: inconsistent fdMutex
goroutine 1048589 [running]:
net.(*fdMutex).rwlock(0xc0420741c0, 0x1, 0x0)
C:/Go/src/net/fd_mutex.go:145 +0x1ab
net.(*netFD).readLock(0xc0420741c0, 0x0, 0x0)
C:/Go/src/net/fd_mutex.go:218 +0x39
net.(*netFD).Read(0xc0420741c0, 0xc365329000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
C:/Go/src/net/fd_windows.go:444 +0x5d
net.(*conn).Read(0xc04205e030, 0xc365329000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
C:/Go/src/net/net.go:181 +0x77
bufio.(*Reader).fill(0xc36532bf08)
C:/Go/src/bufio/bufio.go:97 +0x11e
bufio.(*Reader).ReadSlice(0xc36532bf08, 0xc36532be0a, 0xc36532be00, 0x0, 0x100000000000000, 0x0, 0x1000)
C:/Go/src/bufio/bufio.go:338 +0xc2
bufio.(*Reader).ReadBytes(0xc36532bf08, 0x100a, 0x1000, 0xc365329000, 0x1000, 0x1000, 0x0)
C:/Go/src/bufio/bufio.go:416 +0x6d
main.echo(0x574ce0, 0xc04205e030, 0xc042098d80)
C:/Users/fcfn/goecho/server.go:13 +0x10b
created by main.handleConnection
C:/Users/fcfn/goecho/server.go:27 +0x271