恐慌:网络:fdMutex不一致

huangapple go评论82阅读模式
英文:

panic: net: inconsistent fdMutex

问题

我正在尝试在Windows 10上使用Go(go1.8.3 windows/amd64)实现一个简单的回显服务器,但是在从客户端(使用ncat连接)获取连接后的一段时间后,出现了奇怪的恐慌。我可以发送和接收消息,但无论我做什么,它都会在一段时间后崩溃,我不知道为什么。

代码如下:

package main

import (
    "bufio"
    "log"
    "net"
)

func echo(conn net.Conn, c chan int) {
    c <- 1
    reader := bufio.NewReader(conn)
    msg, err := reader.ReadBytes('\n')
    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\n"))
    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)
    }
}

错误信息如下:

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

帮助你解决这个问题。

英文:

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 (
    &quot;bufio&quot;
    &quot;log&quot;
    &quot;net&quot;
)

func echo(conn net.Conn, c chan int) {
    c &lt;- 1
    reader := bufio.NewReader(conn)
    msg, err := reader.ReadBytes(&#39;\n&#39;)
    if err != nil {
        log.Printf(&quot;error reading from the reader: %v&quot;, err)
        return
    }
    conn.Write(msg)
}

func handleConnection(conn net.Conn) {
    log.Print(&quot;Got connection from: &quot;, conn.RemoteAddr())
    conn.Write([]byte(&quot;Hello, this is a Go echo server\n&quot;))
    c := make(chan int)
    for {
        go echo(conn, c)
        &lt;-c
    }
}

func main() {
    listener, err := net.Listen(&quot;tcp&quot;, &quot;127.0.0.1:3000&quot;)
    if err != nil {
        log.Fatalf(&quot;couldn&#39;t create a tcp server: %v&quot;, err)
    }
    for {
        conn, err := listener.Accept()
        defer conn.Close()
        if err != nil {
            log.Printf(&quot;couldn&#39;t create a connection: %v&quot;, 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

答案1

得分: 1

感谢@JimB,我更好地理解了问题的本质。以下是他说的内容:

实际的错误是因为你正在从无限数量的goroutine中调用Read。net包假设net.Conn上永远不会有超过1<<20个并发操作的原因。- JimB


这里有一些错误的代码,它并不能完全修复问题。根据JimB的建议,我将其删除,以免误导任何人。

我从JimB那里学到的:

  1. 除非必要,否则不应使用goroutine(echo函数不应该是一个goroutine,因为读取是一个阻塞操作,在我们了解客户端发送给我们的内容之前,我们无法对连接做任何操作,所以不需要并发;另一方面,handleConnection在一个goroutine中运行,因为它应该同时处理多个连接)。
  2. 错误是宝贵的东西,要优雅地处理它们,不要忽略它们。
英文:

Thanks to @JimB, I understand the nature of the problem better. Here's what he said:

>
The actual error is because you're calling Read from an unbounded number of goroutines. The net package assumes there's no reason to ever have more than 1<<20 concurrent operations on a net.Conn. – JimB


Some bad code was here, which wasn't quite a fix for the problem at hand. I removed it so it won't mislead anyone, as advised by JimB.

What I learned from JimB:

  1. You shouldn't use goroutines unless it is necessary (echo function shouldn't be a goroutine, as reading is a blocking operation, and we can't really do anything with the connection until we understand what the client is sending to us, so no concurrency is needed; handleConnection, on the other hand, is run inside a goroutine because it's supposed to handle multiple connections simultaneously).
  2. Errors are precious things, handle them gracefully, do not ignore them.

huangapple
  • 本文由 发表于 2017年6月21日 21:57:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/44678216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定