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 multiple header writes for connect unary on error #726

Merged
merged 2 commits into from
Apr 17, 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
7 changes: 4 additions & 3 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,10 @@ func receiveUnaryMessage[T any](conn receiveConn, initializer maybeInitializer,
if err := initializer.maybe(conn.Spec(), &msg2); err != nil {
return nil, err
}
if err := conn.Receive(&msg2); err == nil {
return nil, NewError(CodeUnimplemented, fmt.Errorf("unary %s has multiple messages", what))
} else if err != nil && !errors.Is(err, io.EOF) {
if err := conn.Receive(&msg2); !errors.Is(err, io.EOF) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to the change but the error check err != nil was superfluous, so simplified to the happy path first.

if err == nil {
err = NewError(CodeUnimplemented, fmt.Errorf("unary %s has multiple messages", what))
}
return nil, err
}
return &msg, nil
Expand Down
14 changes: 7 additions & 7 deletions protocol_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,6 @@ type connectUnaryHandlerConn struct {
marshaler connectUnaryMarshaler
unmarshaler connectUnaryUnmarshaler
responseTrailer http.Header
wroteBody bool
}

func (hc *connectUnaryHandlerConn) Spec() Spec {
Expand All @@ -709,8 +708,7 @@ func (hc *connectUnaryHandlerConn) RequestHeader() http.Header {
}

func (hc *connectUnaryHandlerConn) Send(msg any) error {
hc.wroteBody = true
hc.writeResponseHeader(nil /* error */)
hc.mergeResponseHeader(nil /* error */)
if err := hc.marshaler.Marshal(msg); err != nil {
return err
}
Expand All @@ -726,16 +724,16 @@ func (hc *connectUnaryHandlerConn) ResponseTrailer() http.Header {
}

func (hc *connectUnaryHandlerConn) Close(err error) error {
if !hc.wroteBody {
hc.writeResponseHeader(err)
if !hc.marshaler.wroteHeader {
hc.mergeResponseHeader(err)
// If the handler received a GET request and the resource hasn't changed,
// return a 304.
if len(hc.peer.Query) > 0 && IsNotModifiedError(err) {
hc.responseWriter.WriteHeader(http.StatusNotModified)
return hc.request.Body.Close()
}
}
if err == nil {
if err == nil || hc.marshaler.wroteHeader {
Copy link
Contributor Author

@emcfarlane emcfarlane Apr 17, 2024

Choose a reason for hiding this comment

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

We could error here to report to the Close that the error wasn't encoded. I've left for now, as a best effort solution.

return hc.request.Body.Close()
}
// In unary Connect, errors always use application/json.
Expand All @@ -757,7 +755,7 @@ func (hc *connectUnaryHandlerConn) getHTTPMethod() string {
return hc.request.Method
}

func (hc *connectUnaryHandlerConn) writeResponseHeader(err error) {
func (hc *connectUnaryHandlerConn) mergeResponseHeader(err error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to mergeResponseHeaders as this doesn't trigger a write.

header := hc.responseWriter.Header()
if hc.request.Method == http.MethodGet {
// The response content varies depending on the compression that the client
Expand Down Expand Up @@ -923,6 +921,7 @@ type connectUnaryMarshaler struct {
bufferPool *bufferPool
header http.Header
sendMaxBytes int
wroteHeader bool
}

func (m *connectUnaryMarshaler) Marshal(message any) *Error {
Expand Down Expand Up @@ -961,6 +960,7 @@ func (m *connectUnaryMarshaler) Marshal(message any) *Error {
}

func (m *connectUnaryMarshaler) write(data []byte) *Error {
m.wroteHeader = true
payload := bytes.NewReader(data)
if _, err := m.sender.Send(payload); err != nil {
err = wrapIfContextError(err)
Expand Down
Loading