Repository navigation
Description
Describe the bug
juniper's async executor resolves a mutation operation's root selection set through the same code
path as a query: every root field's future is pushed into a single FuturesOrdered and they are
polled concurrently. §6.3.1 requires that a serially executed collected fields map
"determine the corresponding entry in the result map for each item to completion before it continues
on to the next entry", and §6.2.2 puts a mutation's root selection set in exactly that mode.
Two consequences follow:
- Top-level mutation fields interleave, so the side effects they exist to perform race against each
other. - When a root field of a non-
Nulltype fails, the executor returnsValue::null()immediately and
drops theFuturesOrdered, cancelling every sibling still in flight. Those siblings never record
their errors, so theerrorsarray is nondeterministic, and any side effect one of them had
already started (a write dispatched to a blocking pool, an outbound request) is neither reflected
in the response nor rolled back.
The synchronous executor does not have this problem: resolve_selection_set_into() is a plain for
loop, so it is already correct on ordering and on aborting at the first non-Null failure. (It
shares the async path's other, unrelated deviations — no field grouping by response key, no
visitedFragments — which are out of scope here.)
To Reproduce
§6.3.1 gives a canonical illustration of serial execution. Given a changeTheNumber mutation field
that stores newNumber and returns an object exposing it as theNumber:
mutation {
first: changeTheNumber(newNumber: 1) { theNumber }
second: changeTheNumber(newNumber: 3) { theNumber }
third: changeTheNumber(newNumber: 2) { theNumber }
}the spec requires exactly:
{ "first": { "theNumber": 1 }, "second": { "theNumber": 3 }, "third": { "theNumber": 2 } }juniper's async executor cannot guarantee this. All three field futures are pushed before any is
polled to completion, so a resolver that .awaits anything between its write and its read observes
its siblings' writes. execute_sync() returns the spec result; execute() does not.
The failure we hit in production is the error-handling half. Two root mutation fields whose resolvers
both fail, with non-Null payload types:
mutation DoIt {
confirm(input: { id: "…" }) { id }
engage(input: { id: "…" }) { id }
}returns data: null with either one or two entries in errors, varying run to run with database
and pool latency. Two entries when the second field recorded its error before the executor processed
the first field's failure; one entry when the second field was cancelled mid-flight and its error was
lost. Under serial execution the result would be deterministic: the first field's error only, with
the second field never started.
I'm preparing a regression test against tests/integration/ and will attach it here.
Expected behavior
The root selection set of a mutation operation executes serially: each field's execution completes
before the next begins, in collected-field order. A failing non-Null root field then aborts the
remainder deterministically (the spec's error-propagation behavior, per §6.4.4), and no field
is cancelled after it has already begun performing side effects.
Additional context
What the spec says. The normative requirement is in §6.3.1 Normal and Serial Execution:
When executing a mutation, the selections in the top most selection set will be executed in serial
order, starting with the first appearing field textually.When executing a collected fields map serially, the executor must consider each entry from the
collected fields map in the order provided in the collected fields map. It must determine the
corresponding entry in the result map for each item to completion before it continues on to the
next entry in the collected fields map.
A mutation reaches that mode through the algorithms: ExecuteMutation() (§6.2.2) passes
"serial" as the execution mode to ExecuteRootSelectionSet(), which runs
ExecuteCollectedFields() serially rather than normally. §6.2.2 also states the rationale:
It is expected that the top level fields in a mutation operation perform side-effects on the
underlying data system. Serial execution of the provided mutations ensures against race conditions
during these side-effects.
The same requirement is in the October 2021 edition that most of juniper's docs link to
(§6.2.2,
§6.3.1).
Where this happens. All links against master @ 65cbce2.
Mutations take the same branch as queries, with no notion of an execution mode:
executor/mod.rs#L989-L999—OperationType::QueryandOperationType::Mutationboth
callExecutor::resolve_into_value_async()on the root type.
That reaches the default GraphQLValueAsync::resolve_async() impl and then
resolve_selection_set_into_async_recursive(), where:
async_await.rs#L199— a singleFuturesOrderedis created for the whole selection set.async_await.rs#L263— every field's resolver future is pushed into it.FuturesOrdered
preserves output order, not execution order; all futures are in flight at once.async_await.rs#L426-L437— the drain loop returnsValue::null()on the first
non-Nullfield failure, dropping theFuturesOrderedand cancelling every unfinished sibling.
A second, deterministic ordering defect in the same function. A fragment spread (#L315-L321)
and a typed inline fragment (#L374-L380) are resolved with an .await inside the
collection loop, so they run to completion before any plain sibling field future has been polled at
all. Given mutation { a ...F } where F selects b, b executes fully before a starts. An
untyped inline fragment is not affected — it is pushed as a real future at #L411. A fix
therefore needs to collect fields across fragments first (spec CollectFields()) and execute the
resulting map in order, rather than only serializing the top-level Selection::Field arms.
For contrast, the sync path is already correct: types/base.rs#L432 iterates the selection
set in order, and #L487-L499 stops at the first non-Null failure — matching graphql-js,
where handleFieldError() rethrows for non-Null types and unwinds out of executeFieldsSerially().
Versions. Reproduced on 0.17.0 and 0.17.1; juniper/src/types/async_await.rs is byte-identical
across juniper-v0.17.0, juniper-v0.17.1 and master @ 65cbce2. git log -S on the drain loop
shows this has been the behavior since the async executor was introduced (a14f0dd, Aug 2019), so
all async releases are affected. Nothing in the main changelog section relates to it, and I could
not find an existing issue or PR.
Notes toward a fix. resolve_async() is only overridden by codegen for enums and scalars (leaf
types), never for objects or interfaces, so a serial entry point can be called from the
OperationType::Mutation arm of execute_validated_query_async() without a breaking change to
GraphQLValueAsync. The one caveat is a hand-written GraphQLValueAsync impl that overrides
resolve_async() on the mutation root, which such a path would bypass.
Happy to put up a PR with the regression test if the approach sounds right.
Activity
ddavison commentedon Sep 25, 2026
@tyranron , PR Filed: #1399 . Let me know if there's any questions that I can answer!