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 · 4 comments
Closed

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

neild opened this issue Jul 28, 2020 · 4 comments

Comments

@neild
Copy link
Contributor

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
Contributor

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
@dmitshur
Copy link
Contributor

@gopherbot Please backport to Go 1.14.

This is a second fixup to a previous fix (CL 242117) for an approved backport issue #39279. We can't submit that CL without this one (and #40973). This needs to be backported to Go 1.14 only, since the fix is already included in Go 1.1​5.

@gopherbot
Copy link
Contributor

Backport issue(s) opened: #41016 (for 1.14).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/250299 mentions this issue: [release-branch.go1.14] net/http: fix cancelation of requests with a readTrackingBody wrapper

gopherbot pushed a commit that referenced this issue Aug 27, 2020
…readTrackingBody wrapper

Use the original *Request in the reqCanceler map, not the transient
wrapper created to handle body rewinding.

Change the key of reqCanceler to a struct{*Request}, to make it more
difficult to accidentally use the wrong request as the key.

Updates #40453.
Fixes #41016.

Change-Id: I4e61ee9ff2c794fb4c920a3a66c9a0458693d757
Reviewed-on: https://go-review.googlesource.com/c/go/+/245357
Run-TryBot: Damien Neil <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Russ Cox <[email protected]>
Reviewed-on: https://go-review.googlesource.com/c/go/+/250299
Run-TryBot: Dmitri Shuralyov <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
@golang golang locked and limited conversation to collaborators Aug 24, 2021
@rsc rsc unassigned neild Jun 23, 2022
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