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

x/net/context/ctxhttp: race condition preventing response from being closed #14065

Closed
rs opened this issue Jan 22, 2016 · 8 comments
Closed

x/net/context/ctxhttp: race condition preventing response from being closed #14065

rs opened this issue Jan 22, 2016 · 8 comments

Comments

@rs
Copy link
Contributor

rs commented Jan 22, 2016

The ctxhttp.Do method contains a race condition when the context is cancelled before the client.Do performed in a go routine returns. The http.Response is discarded, leaving its Body opened and thus leading to sockets left in CLOSE_WAIT state.

To reproduce the issue, run this on Linux (can't reproduce on osx, I'm not sure why):

package main

import (
    "fmt"

    "golang.org/x/net/context"
    "golang.org/x/net/context/ctxhttp"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    cancel()
    resp, err := ctxhttp.Get(ctx, nil, "http://httpbin.org/get")
    fmt.Printf("%v, %v\n", resp, err)
    <-make(chan struct{})
}

The output is:

<nil>, context canceled

As the res is nil, I have no way for the caller to close the resp.Body.

Then look at netstat -lan | grep CLOSE_WAIT, you will see a new socket lying down forever.

@bradfitz
Copy link
Contributor

Which version of Go? ctxhttp operated differently depending on the Go version.

@bradfitz
Copy link
Contributor

Never mind, I see the problem.

@bradfitz bradfitz self-assigned this Jan 22, 2016
@bradfitz bradfitz added this to the Unreleased milestone Jan 22, 2016
@broady
Copy link
Contributor

broady commented Jan 22, 2016

I seem to recall seeing some workaround code for <=1.4. @jacobsa ?

@bradfitz
Copy link
Contributor

@broady, @okdave, I'm sending a fix. Polishing a test now.

@broady
Copy link
Contributor

broady commented Jan 22, 2016

Dupe of #13293.

@bradfitz
Copy link
Contributor

I'm not sure it is.

@bradfitz
Copy link
Contributor

@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators Feb 3, 2017
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

4 participants