Skip to content

Commit 183c80a

Browse files
authored
fix: panic handling in shadow resolver (openfga#2432)
1 parent 73f6d14 commit 183c80a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

internal/graph/shadow_resolver.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,22 @@ func (s ShadowResolver) ResolveCheck(ctx context.Context, req *ResolveCheckReque
6767
reqClone.VisitedPaths = nil // reset completely for evaluation
6868
s.wg.Add(1)
6969
go func() {
70-
ctx, cancel := context.WithTimeout(ctxClone, s.shadowTimeout)
7170
defer func() {
7271
if r := recover(); r != nil {
73-
s.logger.ErrorWithContext(ctx, "shadow check panic",
72+
s.logger.ErrorWithContext(ctx, "panic recovered",
7473
zap.String("resolver", s.name),
7574
zap.Any("error", err),
7675
zap.String("request", reqClone.GetTupleKey().String()),
7776
zap.String("store_id", reqClone.GetStoreID()),
7877
zap.String("model_id", reqClone.GetAuthorizationModelID()),
78+
zap.String("function", "ShadowResolver.ResolveCheck"),
7979
)
8080
}
81-
cancel()
82-
s.wg.Done()
8381
}()
82+
83+
defer s.wg.Done()
84+
ctx, cancel := context.WithTimeout(ctxClone, s.shadowTimeout)
85+
defer cancel()
8486
shadowRes, err := s.shadow.ResolveCheck(ctx, reqClone)
8587
if err != nil {
8688
s.logger.WarnWithContext(ctx, "shadow check errored",

internal/graph/shadow_resolver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/stretchr/testify/require"
1010
"go.uber.org/mock/gomock"
11+
"go.uber.org/zap"
1112

1213
"github.com/openfga/openfga/internal/mocks"
1314
)
@@ -105,7 +106,7 @@ func TestShadowResolver_ResolveCheck(t *testing.T) {
105106
shadow.EXPECT().ResolveCheck(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, request *ResolveCheckRequest) (*ResolveCheckResponse, error) {
106107
panic(errors.New("test error"))
107108
})
108-
logger.EXPECT().ErrorWithContext(gomock.Any(), "shadow check panic", gomock.Any())
109+
logger.EXPECT().ErrorWithContext(gomock.Any(), "panic recovered", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), zap.String("function", "ShadowResolver.ResolveCheck"))
109110
res, err := checker.ResolveCheck(context.Background(), &ResolveCheckRequest{})
110111
require.NoError(t, err)
111112
require.False(t, res.Allowed)

0 commit comments

Comments
 (0)