ddliu / go-httpclient

Advanced HTTP client for golang
MIT License
465 stars 105 forks source link

deadlock when using Begin() with PostJson() #50

Closed just1900 closed 1 year ago

just1900 commented 1 year ago

Problem

Refer to the doc, httpclient provides a concurrent-safe as https://github.com/ddliu/go-httpclient#concurrent-safe described, However there are corner cases where deadlock exists when using httpclient.Begin().PostJson().

How to Reproduce

func Test_httpclient(t *testing.T) {
    type Node struct {
        Name string `json:"name"`
        Next *Node  `json:"next"`
    }
    n1 := Node{Name: "1", Next: nil}
    n2 := Node{Name: "2", Next: &n1}
    n1.Next = &n2
        // send an object that can't be marshalled.
    _, err := httpclient.Begin().PostJson("xxx", n2)
    if err != nil {
        fmt.Println(err.Error())
    }
        // block here as Begin() can not require the lock.
    httpclient.Begin().Get("xxx")
    fmt.Println("ok")
}

By reading the code further, there are also some other function returned without calling reset() to release the lock, such as https://github.com/ddliu/go-httpclient/blob/d779327486e06c9e47ae2448356941954e95395d/httpclient.go#L729-L754.

ddliu commented 1 year ago

Fixed