Closed embano1 closed 2 years ago
nice! Want to make a PR?
nice! Want to make a PR?
Sure, I just need to dig a little deeper as there might be another anomaly in the way requests are handled. Not sure I get the full picture yet.
Update:
I can reproduce the issue with net/http
the following handlers:
func(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
The second handler does not always show the http: ContentLength=34 with Body length 0
*url.Error
, but I managed to catch it several times with a debugger.
I was also able to catch it once using the cloudevents
receiver and a simple event handler:
func receive(ctx context.Context, event cloudevents.Event) error {
fmt.Printf("got event: %v\n", event)
return http.NewResult(nethttp.StatusInternalServerError, nethttp.StatusText(nethttp.StatusInternalServerError))
}
Still not sure where and how it gets triggered in all these (rare) scenarios, but perhaps there's a race which only shows during debugging - typical Heisenbug :D
Also, I was wondering why the unit tests were not exhibiting the problem: this is caused by only using a RoundTripper
mock instead of a "full" HTTP server which reads/modifies the body (also the tests didn't use a body which was another reason we did not see this).
Scenario:
When using a
net/http
HTTP server as remote/receiver (instead of the receiver from this package),ceClient
has issues when retrying a non-successful HTTP response.Error:
"msg":"Invalid result type, not HTTP Result: Post \"http://always-fail.default.127.0.0.1.sslip.io\": http: ContentLength=34 with Body length 0 (type: *protocol.Receipt)"}
This happens when the remote server returns a message in the response, e.g. „server down“ (not CE) but not when just returning a status code and empty response body.
The issue is caused by
func (p *Protocol) doWithRetry()
because it does not reset thereq.Body
during subsequent attempts.Expected behavior:
Return encoding error, but correctly reset
req.Body
, e.g. return"failed to convert response into event: unknown Message encoding\n500: (3x)"
Client code:
HTTP server code (note: just using
net/http
)Patch