dpzff20644 2017-12-21 10:47
浏览 388
已采纳

http.ResponseWriter.WriteHeader()导致死锁

I was trying to use the httptest package in golang. I found out something I don't understand. Here is the code:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "net/http/httptest"
)

func main() {
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello1"))
    }))
    ts.Close()
    ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello2"))
    }))
    ts.Close()
    ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(100)
        w.Write([]byte("Hello3"))
    }))

    res, err := http.Get(ts.URL)
    if err != nil {
        log.Fatal(err)
    }
    greeting, err := ioutil.ReadAll(res.Body)
    res.Body.Close()
    if err != nil {
        log.Fatal(err)
    }

    ts.Close()
    fmt.Printf("%s", greeting)
}

In this code example, I was trying to open and close httptest servers several times. Somehow it caused deadlock in The Go Playground. I tried on my own environment (Go version: go1.7.4 darwin/amd64) and it caused hanging without responding at all.

My question is: Why w.WriteHeader(100) caused deadlock but w.WriteHeader(200) doesn't? Is it the bug from the core library of Golang or just I misunderstood some usage? Tks!

  • 写回答

1条回答

  • dongmei9203 2017-12-21 12:59
    关注

    If you slightly modify code:

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
        "net/http/httptest"
    )
    
    func main() {
        ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            w.Write([]byte("Hello1"))
        }))
        ts.Close()
        ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            w.Write([]byte("Hello2"))
        }))
        ts.Close()
        ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            w.WriteHeader(100)
            w.Write([]byte("Hello3"))
        }))
    
        fmt.Println("before get")   ///// . <----
        res, err := http.Get(ts.URL)
        fmt.Println("after get")    ///// . <----
        if err != nil {
            log.Fatal(err)
        }
        greeting, err := ioutil.ReadAll(res.Body)
        res.Body.Close()
        if err != nil {
            log.Fatal(err)
        }
    
        ts.Close()
        fmt.Printf("%s", greeting)
    }
    

    and run it - you'll see only first line.

    That means Go http client want more data from you. So it hangs on line

        res, err := http.Get(ts.URL)
    

    and cannot get to the ts.Close() below.

    Next - lets modify a test, so it will close a connection and this way release a clients waiting lock:

    ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(100)
        w.Write([]byte("Hello3"))
        hj, _ := w.(http.Hijacker)
        conn, _, _ := hj.Hijack()
        conn.Close()
    }))
    

    I close connection explicitly but this way you get both check strings and test finishes ok. Try it.

    Of course it's a HTTP protocol violation so I get an error:

    Get http://127.0.0.1:54243: net/http: HTTP/1.x transport connection broken: unexpected EOF
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序