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

Fix dial panic when ctx is nil #365

Merged
merged 2 commits into from
Oct 13, 2023
Merged

Fix dial panic when ctx is nil #365

merged 2 commits into from
Oct 13, 2023

Conversation

guseggert
Copy link
Contributor

When the ctx is nil, http.NewRequestWithContext returns a "net/http: nil Context" error and a nil request. In this case, the dial function panics because it assumes the req is never nil. This checks the returning error and returns it, so that callers get an error instead of a panic in that scenario.

When the ctx is nil, http.NewRequestWithContext returns a "net/http:
nil Context" error and a nil request. In this case, the dial function
panics because it assumes the req is never nil. This checks the
returning error and returns it, so that callers get an error instead
of a panic in that scenario.
Copy link
Owner

@nhooyr nhooyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guseggert

dial.go Outdated Show resolved Hide resolved
Co-authored-by: Anmol Sethi <hi@nhooyr.io>
@BigLep
Copy link

BigLep commented May 5, 2023

@nhooyr : is this going to get merged?

@elimisteve
Copy link

LGTM 👍

@nhooyr
Copy link
Owner

nhooyr commented Oct 13, 2023

I'm not sure about this anymore. ctx should never be nil. You should just use context.Background instead.

@BigLep @elimisteve why are you guys running into this?

@nhooyr
Copy link
Owner

nhooyr commented Oct 13, 2023

Yea there doesn't seem to be a consistent convention on this even in the go stdlib. net.DialContext for example panics on a nil context. https://cs.opensource.google/go/go/+/refs/tags/go1.21.3:src/net/dial.go;l=456

Seems right to me to panic. I'm happy to incorporate the missing err check on http.NewRequestWithContext though.

@nhooyr nhooyr changed the base branch from master to dev October 13, 2023 07:25
@nhooyr nhooyr merged commit 693fac9 into nhooyr:dev Oct 13, 2023
@nhooyr
Copy link
Owner

nhooyr commented Oct 13, 2023

Thanks @guseggert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants