net/http: Request with canceled context produces undefined behaviour #43082
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (go version)?
Does this issue reproduce with the latest release?
Yes. Just do any request on canceled context.
What did you do?
Hi there. I got undefined behaviour of stdlib http client. The library (stripe-go) uses stdlib and relies on cancelation of request by context.Context.
For some local tests I cancel passed Context manually (no timeout) calling cancel() method and then call Stripe library. I was surprised to get result together:
Problem code here in function named
roundTrip
:go/src/net/http/h2_bundle.go
Line 7535 in 212d385
Select statement don't guarantee of execution order. So with manually canceled context before request I got:
go/src/net/http/h2_bundle.go
Line 7700 in 212d385
cs.getStartedWrite() == true
and error(context canceled)
go/src/net/http/h2_bundle.go
Line 7677 in 212d385
Could you implement some
cs.completelyWritten()
check and don't return error in case of body completed written? Or maybe reset Context and don't processctx.Done
for case if it happen in last nanosecond or even with already written as in my case. Or maybe even checkctx.Err()
before request just for test cases here:go/src/net/http/h2_bundle.go
Line 7638 in 212d385
My point is - don't return error for allow to do fewer count of retries. Unfortunately stripe-go used a retry for request with canceled so chance to send request was multiplied by retry count. It was fixed in recent: stripe/stripe-go#1184
And yes I understand about sent request is not guarantee and we can get error while reading response. But stdlib should give guarantee with predefined environment I think.
And one question more: is it documented behaviour? I will mail Stripe with this issue link . Maybe that's better to check
ctx.Err()
on their side before sending request with already canceled context.What did you expect to see?
Sent request or error. No both together.
What did you see instead?
Server receive and process request && I got an error
(context canceled)
from library (not by reading response)I need in one more retry and should implement Idempotency key
The text was updated successfully, but these errors were encountered: