dqyy38265 2017-06-21 13:57
浏览 34
已采纳

恐慌:净:不一致fdMutex

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
  • 写回答

1条回答 默认 最新

  • doutale7115 2017-06-21 13:57
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?