Skip to content

client.HttpGetJSON panics with nil pointer dereference when Client.Timeout exceeded #27

@johnmccabe

Description

@johnmccabe

Hi,
I've been hitting problems with some API calls to Hub timing out resulting in a panic due to an error being returned along with *http.Response being nil.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x12fbc67]

goroutine 1 [running]:
github.com/blackducksoftware/hub-client-go/hubclient.readResponseBody(0x0, 0xc000208500, 0x0, 0x141d980)
        /Users/xxxxxxx/workspace/go/src/github.com/blackducksoftware/hub-client-go/hubclient/client.go:420 +0x37
github.com/blackducksoftware/hub-client-go/hubclient.(*Client).HttpGetJSON(0xc0000ae370, 0xc0002d61c0, 0x70, 0x132ce40, 0xc00000c460, 0xc8, 0x70, 0xc00006ac00)
        /Users/xxxxxxx/workspace/go/src/github.com/blackducksoftware/hub-client-go/hubclient/client.go:225 +0x3ed
github.com/blackducksoftware/hub-client-go/hubclient.(*Client).ListProjectVersions(0xc0000ae370, 0xc00001a410, 0x8, 0xc000020300, 0x5e, 0x0, 0x0, 0x0, 0x0, 0xc00000c3e0, ...)
        /Users/xxxxxxx/workspace/go/src/github.com/blackducksoftware/hub-client-go/hubclient/projects-client.go:98 +0x171
main.main()
        /Users/xxxxxxx/workspace/go/src/xxxxxxx/xxxxxxx/hub-query/main.go:68 +0xc7e
exit status 2

resp is nil here with the panic in readResponseBody occurring when an attempt to access resp.Body is made:

if resp, err = c.httpClient.Do(req); err != nil {
body := readResponseBody(resp)
return newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from %s: %+v", url, err))
}

I've tweaked the hubclient locally (handled the nil in this single location and set a dial timeout) and was able to see that in my case the underlying error being thrown (resulting in resp == nil) was:

net/http: request canceled (Client.Timeout exceeded while awaiting headers)

The error handling should be tweaked to account for the possibility of resp being nil.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions