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: Transport.CancelRequest no longer cancels in-flight request #40453

Closed
neild opened this issue Jul 28, 2020 · 1 comment
Closed

net/http: Transport.CancelRequest no longer cancels in-flight request #40453

neild opened this issue Jul 28, 2020 · 1 comment
Assignees
Milestone

Comments

@neild
Copy link
Contributor

@neild neild commented Jul 28, 2020

(*net/http.Transport).CancelRequest's documentation states:

CancelRequest should only be called after RoundTrip has returned.

Despite this warning, CancelRequest does usually cancel an in-flight request currently blocked in RoundTrip. CL 234894 has inadvertently broken this for requests with a body.

Crude reproduction case follows.

package main

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

func init() {
        http.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintln(w, "ok")
        })
        http.HandleFunc("/hang", func(w http.ResponseWriter, r *http.Request) {
                <-r.Context().Done()
        })
        go http.ListenAndServe("localhost:8080", nil)
}

func TestHTTP(t *testing.T) {
        for {
                _, err := http.Get("http://localhost:8080/ok")
                if err == nil {
                        break
                }
        }

        buf := bytes.NewBuffer([]byte{1, 2, 3})

        req, err := http.NewRequest("GET", "http://localhost:8080/hang", buf)
        if err != nil {
                t.Fatal(err)
        }
        donec := make(chan int)
        go func() {
                defer close(donec)
                http.DefaultTransport.RoundTrip(req)
        }()

        time.Sleep(100 * time.Millisecond)
        http.DefaultTransport.(*http.Transport).CancelRequest(req)
        <-donec
}
@neild neild self-assigned this Jul 28, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented Jul 28, 2020

Change https://golang.org/cl/245357 mentions this issue: net/http: fix cancelation of requests with a readTrackingBody wrapper

@neild neild added this to the Go1.15 milestone Jul 30, 2020
@gopherbot gopherbot closed this in f235275 Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.