Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http/httptest: http.Get succeed after Server.Close #12781

Closed
carter2000 opened this issue Sep 29, 2015 · 3 comments
Closed

net/http/httptest: http.Get succeed after Server.Close #12781

carter2000 opened this issue Sep 29, 2015 · 3 comments

Comments

@carter2000
Copy link

After close a httptest.Server, the following request should failed, but sometimes it succeed.
example code: https://play.golang.org/p/IFv6BKjRgt
more simple one: https://play.golang.org/p/FcpbVEf_HI (detect deadlock on play.golang.org, but hit "unexpected get succeed" on my server)
go version: go version go1.5.1 linux/amd64

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"
)

func main() {

    var i int
    for {
        i++
        fmt.Println(i)
        ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        }))
        resp, err := http.Get(ts.URL)
        if err != nil {
            fmt.Println("unexpected get failed", err)
            break
        }
        resp.Body.Close()

        ts.Close()

        resp, err = http.Get(ts.URL)
        if err == nil {
            resp.Body.Close()
            fmt.Println("unexpected get succeed")
            break
        }
    }
}
@gopherbot
Copy link

CL https://golang.org/cl/15151 mentions this issue.

@carter2000
Copy link
Author

@bradfitz

CL https://golang.org/cl/15151 focus on Server.Close race.

But the second example shows Listener.Accept succeed after Listener.Close, I'm not sure if it is right.

Listener.Close: https://github.com/golang/go/blob/go1.5.1/src/net/http/httptest/server.go#L167
Listener.Accept: https://github.com/golang/go/blob/go1.5.1/src/net/http/httptest/server.go#L48

example code: https://play.golang.org/p/FcpbVEf_HI (detect deadlock on play.golang.org, but hit "unexpected get succeed" on my server)

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"
)

func main() {

    var i int
    for {
        i++
        fmt.Println(i)

        ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        }))

        ts.Close()

        resp, err := http.Get(ts.URL)
        if err == nil {
            resp.Body.Close()
            fmt.Println("unexpected get succeed")
            break
        }
    }
}

go version: go version go1.5.1 linux/amd64

@vbatts
Copy link
Contributor

vbatts commented Oct 15, 2015

@carter2000 latest commit (a35d7b3) on CL https://golang.org/cl/15151 fixes your examples for me.

@golang golang locked and limited conversation to collaborators Oct 24, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants