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

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!

图片转代码服务由CSDN问答提供 功能建议

我试图在golang中使用 httptest 包。 我发现了我不明白的东西。 这是代码

  包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){{n w.WriteHeader(100)
 w.Write([]  byte(“ Hello3”))
})))
 
 res,err:= http.Get(ts.URL)
 if err!= nil {
 log.Fatal(err)
} 
 问候语,错误:= ioutil.ReadAll(res.Body)
 res.Body.Close()
如果错误!= nil {
 log.Fatal(err)
} 
 
 ts.Close()  
 fmt.Printf(“%s”,问候)
} 
   
 
 

在此 的代码示例,我试图多次打开和关闭 httptest 服务器。 不知何故,它在The Go Playground中造成了僵局。 我在自己的环境(Go版本:go1.7.4 darwin / amd64)上尝试过,它导致挂起,但根本没有响应。

我的问题是:为什么 w.WriteHeader(100 )导致死锁,但 w.WriteHeader(200)不会吗? 是来自Golang核心库的错误还是只是我误解了一些用法? 谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmei9203
    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
    
    点赞 评论

相关推荐