Skip to content
Open
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
52 changes: 27 additions & 25 deletions internal/protocols/azureblob/azureblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,40 @@ func (azureBlob *AzureBlob) ServeHTTP(writer http.ResponseWriter, request *http.
}

func fail(writer http.ResponseWriter, request *http.Request, status int, msg string, args ...any) {
// Report failure to the Sentry
hub := sentry.GetHubFromContext(request.Context())

hub.WithScope(func(scope *sentry.Scope) {
scope.AddEventProcessor(func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
// Swap the exception type and value to work around
// https://github.com/getsentry/sentry/issues/17837
savedType := event.Exception[0].Type
event.Exception[0].Type = event.Exception[0].Value
event.Exception[0].Value = savedType

return event
})
// Report to Sentry only if a hub is configured: GetHubFromContext returns
// nil when Sentry is uninitialized, and dereferencing it would panic the
// handler mid-response, which the client sees as a "socket hang up".
if hub := sentry.GetHubFromContext(request.Context()); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.AddEventProcessor(func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
// Swap the exception type and value to work around
// https://github.com/getsentry/sentry/issues/17837
savedType := event.Exception[0].Type
event.Exception[0].Type = event.Exception[0].Value
event.Exception[0].Value = savedType

argsAsSentryContext := sentry.Context{}
return event
})

for _, chunk := range lo.Chunk(args, 2) {
key := fmt.Sprintf("%v", chunk[0])
argsAsSentryContext := sentry.Context{}

var value string
for _, chunk := range lo.Chunk(args, 2) {
key := fmt.Sprintf("%v", chunk[0])

if len(chunk) > 1 {
value = fmt.Sprintf("%v", chunk[1])
}
var value string

argsAsSentryContext[key] = value
}
if len(chunk) > 1 {
value = fmt.Sprintf("%v", chunk[1])
}

scope.SetContext("Arguments", argsAsSentryContext)
argsAsSentryContext[key] = value
}

hub.CaptureException(errors.New(msg))
})
scope.SetContext("Arguments", argsAsSentryContext)

hub.CaptureException(errors.New(msg))
})
}

message := craftAndLogMessage(slog.LevelError, msg, args...)

Expand Down
27 changes: 27 additions & 0 deletions internal/protocols/azureblob/fail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package azureblob

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

// TestFailWithoutSentryHubDoesNotPanic guards the regression where fail()
// dereferenced the Sentry hub unconditionally. When Sentry is not configured,
// sentry.GetHubFromContext returns nil, so the call panicked the handler
// goroutine and net/http closed the connection mid-response (the client saw a
// "socket hang up"). With no hub attached to the request context, fail() must
// skip Sentry reporting and still write the error response.
func TestFailWithoutSentryHubDoesNotPanic(t *testing.T) {
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, "/", nil)

require.NotPanics(t, func() {
fail(recorder, request, http.StatusInternalServerError, "boom", "key", "value")
})

require.Equal(t, http.StatusInternalServerError, recorder.Code)
require.Contains(t, recorder.Body.String(), "boom")
}
23 changes: 14 additions & 9 deletions internal/protocols/azureblob/getblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,26 @@ func (azureBlob *AzureBlob) proxyCacheEntryDownload(
})
bytesRead, err := io.CopyBuffer(writer, progressReader, largeBuffer)
if err != nil {
fail(nil, request, http.StatusInternalServerError, "failed to proxy cache entry download",
"err", err, "duration", time.Since(startProxyingAt), "read", bytesRead, "key", key)

// Try to recover by adjusting Range header and re-issuing the request
// before treating the error as fatal. This must happen before fail(),
// otherwise a recoverable mid-stream close is turned into a hard failure.
if errors.Is(err, io.ErrUnexpectedEOF) {
bytesRecovered, err := azureBlob.proxyRecover(request.Context(), rangeHeaderToUse, resp, url, bytesRead, writer)
if err != nil {
craftAndLogMessage(slog.LevelError, "failed to recover proxy cache entry download",
"err", err)
} else {
bytesRecovered, recoverErr := azureBlob.proxyRecover(request.Context(), rangeHeaderToUse, resp, url, bytesRead, writer)
if recoverErr == nil {
craftAndLogMessage(slog.LevelInfo, "successfully recovered proxy cache entry download",
"read", bytesRecovered, "key", key)
"read", bytesRead+bytesRecovered, "key", key)
stats.Default().RecordDownload(bytesRead+bytesRecovered, time.Since(startProxyingAt))

return true
}

craftAndLogMessage(slog.LevelError, "failed to recover proxy cache entry download",
"err", recoverErr)
}

fail(nil, request, http.StatusInternalServerError, "failed to proxy cache entry download",
"err", err, "duration", time.Since(startProxyingAt), "read", bytesRead, "key", key)

return true
}

Expand Down