Skip to content

Commit 1d5d4f6

Browse files
committed
fix concurrent problem in dispatching strategy
improve test
1 parent 971f2e4 commit 1d5d4f6

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/main/java/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategy.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,10 @@ private void dispatchDLCFImpl(Set<String> resultPathsToDispatch, @Nullable Integ
513513

514514
// filter out all DataLoaderCFS that are matching the fields we want to dispatch
515515
List<ResultPathWithDataLoader> relevantResultPathWithDataLoader = new ArrayList<>();
516-
for (ResultPathWithDataLoader resultPathWithDataLoader : callStack.allResultPathWithDataLoader) {
516+
// we need to copy the list because the callStack.allResultPathWithDataLoader can be modified concurrently
517+
// while iterating over it
518+
ArrayList<ResultPathWithDataLoader> resultPathWithDataLoaders = new ArrayList<>(callStack.allResultPathWithDataLoader);
519+
for (ResultPathWithDataLoader resultPathWithDataLoader : resultPathWithDataLoaders) {
517520
if (resultPathsToDispatch.contains(resultPathWithDataLoader.resultPath)) {
518521
relevantResultPathWithDataLoader.add(resultPathWithDataLoader);
519522
}

src/test/groovy/graphql/execution/instrumentation/dataloader/Issue1178DataLoaderDispatchTest.groovy

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring
2121

2222
class Issue1178DataLoaderDispatchTest extends Specification {
2323

24-
public static final int NUM_OF_REPS = 100
2524

26-
@RepeatUntilFailure(maxAttempts = 20)
25+
@RepeatUntilFailure(maxAttempts = 100)
2726
def "shouldn't dispatch twice in multithreaded env"() {
2827
setup:
2928
def sdl = """
@@ -81,11 +80,10 @@ class Issue1178DataLoaderDispatchTest extends Specification {
8180
.build()
8281

8382
then: "execution shouldn't error"
84-
for (int i = 0; i < NUM_OF_REPS; i++) {
85-
def ei = ExecutionInput.newExecutionInput()
86-
.graphQLContext([(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING): enableDataLoaderChaining])
87-
.dataLoaderRegistry(dataLoaderRegistry)
88-
.query("""
83+
def ei = ExecutionInput.newExecutionInput()
84+
.graphQLContext([(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING): enableDataLoaderChaining])
85+
.dataLoaderRegistry(dataLoaderRegistry)
86+
.query("""
8987
query {
9088
getTodos { __typename id
9189
related { id __typename
@@ -118,10 +116,9 @@ class Issue1178DataLoaderDispatchTest extends Specification {
118116
}
119117
}
120118
}""").build()
121-
def resultCF = graphql.executeAsync(ei)
122-
Awaitility.await().until { resultCF.isDone() }
123-
assert resultCF.get().errors.empty
124-
}
119+
def resultCF = graphql.executeAsync(ei)
120+
Awaitility.await().until { resultCF.isDone() }
121+
assert resultCF.get().errors.empty
125122
where:
126123
enableDataLoaderChaining << [true, false]
127124

0 commit comments

Comments
 (0)