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

Comments

Projects
None yet
3 participants
@rs
Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented Jan 21, 2016

Good find.

@gopherbot

This comment has been minimized.

Copy link

commented Jan 21, 2016

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

@gopherbot

This comment has been minimized.

Copy link

commented Jan 21, 2016

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

@gopherbot gopherbot closed this in f8f4cfa Jan 21, 2016

@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.
You can’t perform that action at this time.