Skip to content
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
20 changes: 9 additions & 11 deletions src/main/java/graphql/execution/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import graphql.Internal;
import graphql.Profiler;
import graphql.PublicApi;
import graphql.collect.ImmutableKit;
import graphql.execution.directives.OperationDirectivesResolver;
import graphql.execution.directives.QueryAppliedDirective;
import graphql.execution.incremental.IncrementalCallState;
Expand All @@ -27,16 +26,15 @@
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static graphql.Assert.assertNotNull;
import static graphql.normalized.ExecutableNormalizedOperationFactory.Options;
import static graphql.normalized.ExecutableNormalizedOperationFactory.createExecutableNormalizedOperation;

Expand All @@ -61,7 +59,8 @@ public class ExecutionContext {
private final GraphQLContext graphQLContext;
private final @Nullable Object localContext;
private final Instrumentation instrumentation;
private final AtomicReference<ImmutableList<GraphQLError>> errors = new AtomicReference<>(ImmutableKit.emptyList());
// errors and errorPaths are guarded by errorsLock
private final List<GraphQLError> errors = new ArrayList<>();
private final LockKit.ReentrantLock errorsLock = new LockKit.ReentrantLock();
private final Set<ResultPath> errorPaths = new HashSet<>();
private final DataLoaderRegistry dataLoaderRegistry;
Expand Down Expand Up @@ -104,7 +103,7 @@ public class ExecutionContext {
this.locale = builder.locale;
this.valueUnboxer = builder.valueUnboxer;
this.responseMapFactory = builder.responseMapFactory;
this.errors.set(builder.errors);
this.errors.addAll(builder.errors);
this.localContext = builder.localContext;
this.executionInput = builder.executionInput;
this.dataLoaderDispatcherStrategy = builder.dataLoaderDispatcherStrategy;
Expand Down Expand Up @@ -288,7 +287,7 @@ public void addError(GraphQLError error, ResultPath fieldPath) {
if (!errorPaths.add(fieldPath)) {
return;
}
this.errors.set(ImmutableKit.addToList(assertNotNull(this.errors.get(), "errors list must not be null"), error));
this.errors.add(error);
});
}

Expand All @@ -307,7 +306,7 @@ public void addError(GraphQLError error) {
ResultPath path = ResultPath.fromList(error.getPath());
this.errorPaths.add(path);
}
this.errors.set(ImmutableKit.addToList(assertNotNull(this.errors.get(), "errors list must not be null"), error));
this.errors.add(error);
});
}

Expand All @@ -321,8 +320,7 @@ public void addErrors(List<GraphQLError> errors) {
if (errors.isEmpty()) {
return;
}
// we are locked because we set two fields at once - but we only ever read one of them later
// in getErrors so no need for synchronised there.
// we are locked because we set two fields at once
errorsLock.runLocked(() -> {
Set<ResultPath> newErrorPaths = new HashSet<>();
for (GraphQLError error : errors) {
Expand All @@ -335,7 +333,7 @@ public void addErrors(List<GraphQLError> errors) {
}
}
this.errorPaths.addAll(newErrorPaths);
this.errors.set(ImmutableKit.concatLists(assertNotNull(this.errors.get(), "errors list must not be null"), errors));
this.errors.addAll(errors);
});
}

Expand All @@ -348,7 +346,7 @@ public ResponseMapFactory getResponseMapFactory() {
* @return the total list of errors for this execution context
*/
public List<GraphQLError> getErrors() {
return assertNotNull(errors.get(), "errors list must not be null");
return errorsLock.callLocked(() -> ImmutableList.copyOf(errors));
}

public ExecutionStrategy getQueryStrategy() {
Expand Down
29 changes: 29 additions & 0 deletions src/test/groovy/graphql/execution/ExecutionContextTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package graphql.execution

import graphql.GraphqlErrorBuilder
import spock.lang.Specification
import spock.lang.Timeout

class ExecutionContextTest extends Specification {

@Timeout(10)
def "adding field errors does not copy the error list on every add"() {
// aliased fields over a list can produce one error per field per element. each add used to
// copy the whole error list, so the cost grew with the square of the number of errors
given:
def executionContext = new ExecutionContextBuilder().executionId(ExecutionId.generate()).build()
def error = GraphqlErrorBuilder.newError().message("bad").build()
def count = 200_000

when:
for (int i = 0; i < count; i++) {
executionContext.addError(error)
}
def errors = executionContext.getErrors()
executionContext.addError(error, ResultPath.rootPath().segment("late"))

then:
errors.size() == count
executionContext.getErrors().size() == count + 1
}
}
Loading