-
Notifications
You must be signed in to change notification settings - Fork 196
Handle client connection issues better #2775
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright 2026 The Sigstore Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| func TestMapGRPCToHTTP(t *testing.T) { | ||
| tests := []struct { | ||
| code int | ||
| err error | ||
| want int | ||
| }{ | ||
| {http.StatusOK, status.Error(codes.Canceled, "context canceled"), http.StatusOK}, | ||
| {http.StatusInternalServerError, status.Error(codes.Canceled, "context canceled"), 499}, | ||
| {http.StatusInternalServerError, status.Error(codes.DeadlineExceeded, "deadline exceeded"), http.StatusGatewayTimeout}, | ||
| {http.StatusInternalServerError, status.Error(codes.DataLoss, "dataloss"), http.StatusInternalServerError}, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add test for wrapped errors? |
||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| if got := mapGRPCToHTTP(tt.code, tt.err); got != tt.want { | ||
| t.Errorf("mapGRPCToHTTP(%v, %e) = %v, want %v", tt.code, tt.err, got, tt.want) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,12 @@ import ( | |
| "crypto/tls" | ||
| go_errors "errors" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httputil" | ||
| "strconv" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| // using embed to add the static html page duing build time | ||
|
|
@@ -397,7 +400,22 @@ func recoverer(next http.Handler) http.Handler { | |
| fields = append(fields, zap.ByteString("request_headers", request)) | ||
| } | ||
|
|
||
| log.ContextLogger(ctx).With(fields...).Errorf("panic detected: %v", rvr) | ||
| // Check if the panic is due to a connection issue: Don't log these | ||
| // cases as serious errors | ||
| isNetworkError := false | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wading into "style" territory and I don't care either way, but we can compress this with a lambda or a separate function isClientConnError := func(err error) bool {
var netErr net.Error
return go_errors.Is(err, io.EOF) || go_errors.Is(err, syscall.EPIPE) || go_errors.Is(err, syscall.ECONNRESET) || (go_errors.As(err, &netErr) && netErr.Timeout())
}
if err, ok := rvr.(error); ok && isClientConnError(err) {
log.ContextLogger(ctx).With(fields...).Debugf("client connection closed: %v", rvr)
} else {
log.ContextLogger(ctx).With(fields...).Errorf("panic detected: %v", rvr)
} |
||
| if err, ok := rvr.(error); ok { | ||
| if go_errors.Is(err, io.EOF) || go_errors.Is(err, syscall.EPIPE) || go_errors.Is(err, syscall.ECONNRESET) { | ||
| isNetworkError = true | ||
| } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() { | ||
| isNetworkError = true | ||
| } | ||
| } | ||
|
|
||
| if isNetworkError { | ||
| log.ContextLogger(ctx).With(fields...).Debugf("client connection closed: %v", rvr) | ||
| } else { | ||
| log.ContextLogger(ctx).With(fields...).Errorf("panic detected: %v", rvr) | ||
| } | ||
|
|
||
| errors.ServeError(w, r, nil) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apparently 499s have no default message in the http.StatusText. So if we want to print
Client Closed Contextto logs, we would need to check if code is 499 and then set our own message.