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

Dont hold error lock when calling into error presenters #1399

Merged
merged 1 commit into from
Nov 30, 2020
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
5 changes: 3 additions & 2 deletions graphql/context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ func AddErrorf(ctx context.Context, format string, args ...interface{}) {
func AddError(ctx context.Context, err error) {
c := getResponseContext(ctx)

presentedError := c.errorPresenter(ctx, ErrorOnPath(ctx, err))

c.errorsMu.Lock()
defer c.errorsMu.Unlock()

c.errors = append(c.errors, c.errorPresenter(ctx, ErrorOnPath(ctx, err)))
c.errors = append(c.errors, presentedError)
}

func Recover(ctx context.Context, err interface{}) (userMessage error) {
Expand Down
13 changes: 13 additions & 0 deletions graphql/context_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,16 @@ func TestAddError(t *testing.T) {
})
}
}

func TestGetErrorFromPresenter(t *testing.T) {
ctx := WithResponseContext(context.Background(), func(ctx context.Context, err error) *gqlerror.Error {
errs := GetErrors(ctx)

// because we are still presenting the error it is not expected to be returned, but this should not deadlock.
require.Len(t, errs, 0)
return DefaultErrorPresenter(ctx, err)
}, nil)

ctx = WithFieldContext(ctx, &FieldContext{})
AddError(ctx, errors.New("foo1"))
}