调用http.Get时出现golang恐慌

error message:

goroutine 11357 [runnable]:
net.runtime_pollWait(0x1737f28, 0x77, 0x4fa90)
    /usr/local/go/src/runtime/netpoll.go:157 +0x60
net.(*pollDesc).Wait(0xc829571bf0, 0x77, 0x0, 0x0)
    /usr/local/go/src/net/fd_poll_runtime.go:73 +0x3a
net.(*pollDesc).WaitWrite(0xc829571bf0, 0x0, 0x0)
    /usr/local/go/src/net/fd_poll_runtime.go:82 +0x36
net.(*netFD).connect(0xc829571b90, 0x0, 0x0, 0x1714f80, 0xc829572f20, 0xece412272, 0x33aa2325, 0x473ea0, 0x0, 0x0)
    /usr/local/go/src/net/fd_unix.go:114 +0x1f6
net.(*netFD).dial(0xc829571b90, 0x1714f38, 0x0, 0x1714f38, 0xc8295755c0, 0xece412272, 0x33aa2325, 0x473ea0, 0x0, 0x0)
    /usr/local/go/src/net/sock_posix.go:137 +0x351
net.socket(0x3050e8, 0x3, 0x2, 0x1, 0x0, 0xc829575500, 0x1714f38, 0x0, 0x1714f38, 0xc8295755c0, ...)
    /usr/local/go/src/net/sock_posix.go:89 +0x411
net.internetSocket(0x3050e8, 0x3, 0x1714f38, 0x0, 0x1714f38, 0xc8295755c0, 0xece412272, 0xc833aa2325, 0x473ea0, 0x1, ...)
    /usr/local/go/src/net/ipsock_posix.go:160 +0x141
net.dialTCP(0x3050e8, 0x3, 0x0, 0xc8295755c0, 0xece412272, 0xc833aa2325, 0x473ea0, 0x2, 0x0, 0x0)
    /usr/local/go/src/net/tcpsock_posix.go:171 +0x11e
net.dialSingle(0xc829580b80, 0x1714ea8, 0xc8295755c0, 0xece412272, 0x33aa2325, 0x473ea0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/dial.go:364 +0x3f5
net.dialSerial.func1(0xece412272, 0x33aa2325, 0x473ea0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/dial.go:336 +0x75
net.dial(0x3050e8, 0x3, 0x1714ea8, 0xc8295755c0, 0xc8232f96e8, 0xece412272, 0x33aa2325, 0x473ea0, 0x0, 0x0, ...)
    /usr/local/go/src/net/fd_unix.go:40 +0x60
net.dialSerial(0xc829580b80, 0xc829572f00, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/dial.go:338 +0x760
net.(*Dialer).Dial(0xc8200783c0, 0x3050e8, 0x3, 0xc823166ed0, 0x10, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/dial.go:232 +0x50f
net.(*Dialer).Dial-fm(0x3050e8, 0x3, 0xc823166ed0, 0x10, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/http/transport.go:38 +0x6e
net/http.(*Transport).dial(0xc820092090, 0x3050e8, 0x3, 0xc823166ed0, 0x10, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/http/transport.go:499 +0x79
net/http.(*Transport).dialConn(0xc820092090, 0x0, 0x7fff5fbff92a, 0x4, 0xc823166ed0, 0x10, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/http/transport.go:596 +0x19a9
net/http.(*Transport).getConn.func4(0xc820092090, 0x0, 0x7fff5fbff92a, 0x4, 0xc823166ed0, 0x10, 0xc8232a9560)
    /usr/local/go/src/net/http/transport.go:549 +0x66
created by net/http.(*Transport).getConn
    /usr/local/go/src/net/http/transport.go:551 +0x265

source

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "strconv"
    "time"
)

func golanggo(url string, round int, c chan int64) {
    for i := 0; i < round; i++ {
        call(url, c)
    }
}

func call(url string, c chan int64) {
    eslap := int64(0)
    defer func() {
        c <- eslap
        //fmt.Println("put", eslap)
        // if err := recover(); err != nil {
        //  fmt.Println(err)
        // }
    }()
    now := time.Now()
    resp, err := http.Get(url)
    defer resp.Body.Close()
    if err != nil {
        fmt.Println("err:", err)
        return
    }
    if resp.StatusCode == 200 {
        ioutil.ReadAll(resp.Body)
        //fmt.Println(string(data[:]))
    }
    eslap = time.Now().Sub(now).Nanoseconds()
}

func main() {
    url := os.Args[1]
    size, _ := strconv.Atoi(os.Args[2])
    round, _ := strconv.Atoi(os.Args[3])
    c := make(chan int64)

    for i := 0; i < size; i++ {
        go golanggo(url, round, c)
    }

    var total int64 = 0

    var nanos int64
    for i := 0; i < (size * round); i++ {
        //fmt.Print(i)
        nanos = <-c
        //fmt.Println(i, "get", nanos)
        total += nanos
    }

    fmt.Println(total / 1000000)
}

when exec go run client.go http://www.baidu.com 10000 1, and after a few seconds program crash. when exec go run client.go http://www.baidu.com 100 1, it's ok.

How much of goroutine will cause this error?

Please help me! Thanks.

douji0108
douji0108 如果我理解正确,则您的代码中有错误。检查错误后应推迟resp.Body.Close(),因为resp.Body可能为nil。
4 年多之前 回复

2个回答



  resp,err:= http.Get(url)
defer resp.Body.Close()
if err!= nil {
fmt.Println(“ err:”,err)
返回
}
</ code> </ pre>

如果 err </ code>!= nil ,然后是 resp </ code> == nil,因此崩溃时会取消nil指针的引用。
使用以下代码修复您的代码:</ p>

  resp,err:= http.Get(  url)
if err!= nil {
fmt.Println(“ err:”,err)
return
}
defer resp.Body.Close()
</ code> </ pre>
< / DIV>

展开原文

原文

resp, err := http.Get(url)
defer resp.Body.Close()
if err != nil {
    fmt.Println("err:", err)
    return
}

If err!= nil, then resp==nil, so crash with nil pointer dereference. Fix ur code with:

resp, err := http.Get(url)
if err != nil {
    fmt.Println("err:", err)
    return
}
defer resp.Body.Close()



由于使用系统(基于Cgo)解析同时进行主机名解析,您可能会达到单行进程的os线程限制。 要使用不为每个解析请求占用一个系统线程的纯解析方式,请尝试设置 GOBEBUG </ code>环境变量,例如 export GODEBUG = netdns = go </ code>(请参见 https://golang.org/pkg/net doc,“名称解析”部分)。</ p>
\ n

单个Go进程的默认操作系统线程限制设置为10000: https:// golang.org/pkg/runtime/debug/#SetMaxThreads </ p>

似乎您正在运行最新的Go运行时,默认情况下它会输出单个goroutine的跟踪信息; 在 GOTRACEBACK = all </ code>环境变量集下运行将为您提供更大的堆栈跟踪-最好将其完整地发布。</ p>
</ div>

展开原文

原文

You probably hit os thread limit for single go process due to simultaneous hostname resolving using system (cgo-based) resolving. For using pure-go resolving which does not occupy one system thread per resolve request, try setting GOBEBUG environment variable like this export GODEBUG=netdns=go (see https://golang.org/pkg/net doc, "Name Resolution" section).

The default os thread limit for single Go process is set to 10000: https://golang.org/pkg/runtime/debug/#SetMaxThreads

Also it seems that you're running latest Go runtime which outputs trace of a single goroutine by default; running with GOTRACEBACK=all environment variable set will give you much bigger stacktrace — it's always better to post it in full.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐