diff --git a/api/http/offlinegate/offlinegate.go b/api/http/offlinegate/offlinegate.go index fd5a3a873..58fb29e61 100644 --- a/api/http/offlinegate/offlinegate.go +++ b/api/http/offlinegate/offlinegate.go @@ -45,7 +45,9 @@ func (o *OfflineGate) WaitingMiddleware(timeout time.Duration, next http.Handler httperror.WriteError(w, http.StatusRequestTimeout, "Request timed out while waiting for the backup process to finish", http.ErrHandlerTimeout) return } + + defer o.lock.RUnlock() + next.ServeHTTP(w, r) - o.lock.RUnlock() }) } diff --git a/api/http/offlinegate/offlinegate_test.go b/api/http/offlinegate/offlinegate_test.go index da9bbd787..8a120c317 100644 --- a/api/http/offlinegate/offlinegate_test.go +++ b/api/http/offlinegate/offlinegate_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func Test_canLockAndUnlock(t *testing.T) { @@ -146,3 +147,30 @@ func Test_waitingMiddleware_mayTimeout_whenLockedForTooLong(t *testing.T) { assert.Equal(t, http.StatusRequestTimeout, response.Result().StatusCode, "Request support to timeout waiting for the gate") } + +func Test_waitingMiddleware_handlerPanics(t *testing.T) { + o := NewOfflineGate() + + request := httptest.NewRequest(http.MethodPost, "/", nil) + response := httptest.NewRecorder() + + wg := sync.WaitGroup{} + wg.Add(1) + + go func() { + defer func() { + recover() + + wg.Done() + }() + + o.WaitingMiddleware(time.Second, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + panic("panic") + })).ServeHTTP(response, request) + }() + + wg.Wait() + + require.True(t, o.lock.TryLock()) + o.lock.Unlock() +}