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: Cancel channel is not passed upon redirects #14053

Closed
rs opened this issue Jan 21, 2016 · 3 comments
Closed

net/http: Cancel channel is not passed upon redirects #14053

rs opened this issue Jan 21, 2016 · 3 comments

Comments

@rs
Copy link
Contributor

rs commented Jan 21, 2016

On HTTP redirect, the HTTP client creates a new request and don't copy over the Cancel channel. This prevents any redirected request from being cancelled.

Here is a way to reproduce the issue:

package main

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

func main() {
        t := time.Now()
        _, err := requestTimeout("http://httpbin.org/delay/10", 2 * time.Second)
        fmt.Printf("Err: %v\n", err)
        fmt.Printf("Elapsed: %v\n", time.Now().Sub(t))

        t = time.Now()
        _, err = requestTimeout("http://httpbin.org/redirect-to?url=http://httpbin.org/delay/10", 2 * time.Second)
        fmt.Printf("Err: %v\n", err)
        fmt.Printf("Elapsed: %v\n", time.Now().Sub(t))
}

func requestTimeout(url string, timeout time.Duration) (res *http.Response, err error) {
        req, _ := http.NewRequest("GET", url, nil)
        cancel := make(chan struct{})
        done := make(chan struct{})
        req.Cancel = cancel
        go func() {
                res, err = http.DefaultClient.Do(req)
                close(done)
        }()
        time.Sleep(timeout)
        close(cancel)
        <-done
        return
}

Output:

Err: Get http://httpbin.org/delay/10: net/http: request canceled
Elapsed: 2.004138928s
Err: <nil>
Elapsed: 10.319406866s
@bradfitz
Copy link
Contributor

Good find.

@gopherbot
Copy link

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

@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators Jan 23, 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

3 participants