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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化