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.RoundTrip errors could be more informative #13667

Open
alexflint opened this Issue Dec 18, 2015 · 8 comments

Comments

Projects
None yet
7 participants
@alexflint

alexflint commented Dec 18, 2015

go version go1.5 linux/amd64
Ubuntu 14.04.3 LTS

I made an HTTP request to a server that accepts all TCP connections and then immediately closes them. I was expecting a meaningful error such as "connection closed before request could be sent" but http.RoundTrip returned EOF.

Here are client and server snippets that reproduce this. The client:

package main

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

func main() {
    dialer := &net.Dialer{}
    transport := &http.Transport{
        Dial: dialer.Dial,
    }

    request, _ := http.NewRequest("GET", "http://127.0.0.1:19870/", nil)
    _, err := transport.RoundTrip(request)
    fmt.Println(err)
}

Output is:

EOF

And the server:

package main

import "net"

func main() {
    ln, _ := net.Listen("tcp", ":19870")
    conn, _ := ln.Accept()
    conn.Close()
}

Here is a dump of the TCP exchange:

17:16:31.017342 IP localhost.52287 > localhost.19870: Flags [S], seq 3991906392, win 43690, options [mss 65495,sackOK,TS val 27880590 ecr 0,nop,wscale 7], length 0
E..<..@.@.w..........?M....X.........0.........
..l.........
17:16:31.017356 IP localhost.19870 > localhost.52287: Flags [S.], seq 530413255, ack 3991906393, win 43690, options [mss 65495,sackOK,TS val 27880590 ecr 27880590,nop,wscale 7], length 0
E..<..@.@.<.........M..?..v....Y.....0.........
..l...l.....
17:16:31.017367 IP localhost.52287 > localhost.19870: Flags [.], ack 1, win 342, options [nop,nop,TS val 27880590 ecr 27880590], length 0
E..4..@.@.w..........?M....Y..v....V.(.....
..l...l.
17:16:31.017514 IP localhost.19870 > localhost.52287: Flags [F.], seq 1, ack 1, win 342, options [nop,nop,TS val 27880591 ecr 27880590], length 0
E..4.F@.@."|........M..?..v....Y...V.(.....
..l...l.
17:16:31.019472 IP localhost.52287 > localhost.19870: Flags [F.], seq 1, ack 2, win 342, options [nop,nop,TS val 27880591 ecr 27880591], length 0
E..4..@.@.w..........?M....Y..v....V.(.....
..l...l.
17:16:31.019493 IP localhost.19870 > localhost.52287: Flags [.], ack 2, win 342, options [nop,nop,TS val 27880591 ecr 27880591], length 0
E..4.G@.@."{........M..?..v....Z...V.(.....
..l...l.

@bradfitz bradfitz changed the title from http.Transport.RoundTrip returns EOF for empty response to net/http: Transport.RoundTrip errors could be more informative Dec 18, 2015

@bradfitz

This comment has been minimized.

Member

bradfitz commented Dec 18, 2015

True, but I think it's basically always been like this.

And how often does this happen?

I'm not against fixing this and making the errors more descriptive, but it's pretty low priority. I'd be happy to review code if others want to work on it.

@bradfitz bradfitz added this to the Unplanned milestone Dec 18, 2015

@alexflint

This comment has been minimized.

alexflint commented Dec 18, 2015

The reason this came up was that if you start a docker container with an exposed port but the process inside the container is not listening on the internal port, then the (admittedly braindead) docker daemon will SYNACK all incoming connections and then immediately FIN them. This seems like a generic-enough situation given the popularity of docker to care about this case.

I'm up for putting in a cl to fix this.

@odeke-em

This comment has been minimized.

Member

odeke-em commented Dec 18, 2015

So interestingly, am able to get a meaningful error response as requested in the issue but not consistently. Please see gist https://gist.github.com/odeke-em/dc45c763d311210fbdd1 -- this is without modifying the logic in net/http except for just mere log.Println's
and snippets
screen shot 2015-12-17 at 6 59 34 pm

@alexflint

This comment has been minimized.

alexflint commented Dec 18, 2015

Yes I have also seen the "transport closed before response was received" error on occasion. Could there be a race condition here?

@bradfitz

This comment has been minimized.

Member

bradfitz commented Dec 18, 2015

I don't think there is a data race. There is definitely a higher-level race of many things happening network-wise, though. Read net/http/transport.go's persistConn.roundTrip code.

(which isn't a bug... failure events can just happen in different orders)

@gopherbot

This comment has been minimized.

gopherbot commented Dec 21, 2015

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

@bradfitz bradfitz modified the milestones: Go1.7, Unplanned Feb 1, 2016

@rsc rsc modified the milestones: Go1.8, Go1.7 May 18, 2016

@bradfitz

This comment has been minimized.

Member

bradfitz commented May 18, 2016

I think this was already fixed by https://golang.org/cl/23160

But it could use a new test to confirm.

@quentinmit quentinmit added the NeedsFix label Oct 7, 2016

@alexflint alexflint added the Testing label Nov 7, 2016

@bradfitz

This comment has been minimized.

Member

bradfitz commented Nov 8, 2016

Actually, this is somewhat intentional. See ea2376f which came out of #16465.

But we should probably have different behavior on the first request on a connection at least.

Much has changed in the past two releases, so it's probably time to revisit this. But too much changes this cycle too (Request.GetBody in Go 1.8) so I'll punt this to Go 1.9.

(Incidentally, while thinking about this I realized Go 1.8 adding Request.GetBody meant we could've done more in terms of retries in the Transport. I just filed #17844 for Go 1.9)

@bradfitz bradfitz removed this from the Go1.12 milestone May 18, 2018

@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jun 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment