Skip to content

net/http: CancelRequest twice on the same request panics #10405

@yichengq

Description

@yichengq

If i call CancelRequest twice on the same request, CancelRequest will panic because of close a closed channel at here:
https://golang.org/src/net/http/transport.go#L517

I argue that CancelRequest should not panic here:

  1. There is no need to panic when cancelling a cancelled request. And no document says that is not allowed.
  2. It is hard to ensure a request is canceled without CancelRequest retry:
    go func() { c <- f(client.Do(req)) }()
    select {
    case <-ctx.Done():
        tr.CancelRequest(req)
        <-c // Wait for f to return.
        return ctx.Err()
    case err := <-c:
        return err
    }

(code from https://blog.golang.org/context)
It is possible that the request is canceled before Do is called, and it needs CancelRequest retry to ensure it is canceled.

from https://github.com/coreos/go-etcd/issues/197

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions