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

[agent] Fix a race condition when writing a response to a cancelled request. #135

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions agent/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func (w *streamingResponseWriter) WriteHeader(status int) {
protoMajor = w.r.ProtoMajor
protoMinor = w.r.ProtoMinor
}
w.respChan <- &http.Response{
resp := &http.Response{
Proto: proto,
ProtoMajor: protoMajor,
ProtoMinor: protoMinor,
Expand All @@ -497,6 +497,11 @@ func (w *streamingResponseWriter) WriteHeader(status int) {
Body: w.bodyReader,
Trailer: w.trailer,
}
select {
case w.respChan <- resp:
case <-w.r.Context().Done():
w.bodyReader.Close()
}
}

func (w *streamingResponseWriter) Write(bs []byte) (int, error) {
Expand Down Expand Up @@ -543,7 +548,7 @@ func (w *streamingResponseWriter) CloseWithError(err error) error {
func NewResponseForwarder(client *http.Client, proxyHost, backendID, requestID string, r *http.Request, metricHandler *metrics.MetricHandler) (ResponseWriteCloser, error) {
// Construct a streaming response writer so we can process the written response
// in separate goroutines as it is written.
respChan := make(chan *http.Response, 1)
respChan := make(chan *http.Response)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just to clarify, is this change needed for the fix to work, or is it more of a defensive change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is needed for the fix to work.

The issue was that on one end the response was being buffered, but on the other end it was never being read and consumed.

The fact that it wasn't consumed (written to the proxy request) meant that the response body was never being closed, and thus subsequent writes to the response body would block indefinitely.

To fix this, we have to make the goroutine reading the response and the one writing the response be synchronized through this channel.

NOTE: This reasoning is subtle, but no one has to remember it; if anyone tries to change this in the future the newly changed test will fail.

rw := NewStreamingResponseWriter(respChan, r)

// Construct two ends of a pipe that we will use for concurrently writing the response
Expand Down
7 changes: 6 additions & 1 deletion agent/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,16 @@ func TestResponseForwarderWithRequestCancellation(t *testing.T) {
defer proxyServer.Close()
defer proxyServer.CloseClientConnections()
proxyClient := proxyServer.Client()
_, err := NewResponseForwarder(proxyClient, proxyServer.URL+"/", backendID, requestID, endUserRequest, nil)
rf, err := NewResponseForwarder(proxyClient, proxyServer.URL+"/", backendID, requestID, endUserRequest, nil)
if err != nil {
t.Fatal(err)
}
cancel()
rf.WriteHeader(http.StatusOK)
_, err = rf.Write([]byte(http.StatusText(http.StatusOK)))
if got, want := err, io.ErrClosedPipe; got != want {
t.Errorf("Unexepcted error writing a response for a cancelled request: got %v, want %v", got, want)
}
select {
case err := <-errsChan:
if err != nil {
Expand Down
Loading