fix(core):Skip pruned Set entries to prevent panic in unique predicate #9449
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We encountered a panic issue caused by the @unique directive and found it related to https://github.com/hypermodeinc/dgraph/issues/9437
After review, I determined that the root cause lies in the mismatch between the updateMutations pruning logic and the @unique validation. Specifically:
addQueryIfUnique executes before updateMutations, generating uniqueVars entries based on all mutation predicates.
Later, updateMutations may prune some of those mutations based on @if conditions, setting their .Set to nil.
However, the unique-check logic in verifyUniqueWithinMutation does not account for pruned mutations, and still attempts to access entries from .Set, leading to a nil dereference and index out-of-range panic.
But when a mutation has only one conditional clause, this line:
if len(qc.gmuList[gmuIndex].Set) == 0 { continue }
effectively bypasses the faulty access.
When there are multiple conditional mutations, due to uniqueVars being a map, the iteration order is random. This randomness causes the issue to appear intermittently.
For unit testing, I directly copied the test case provided by @matthewmcneely to reproduce the panic reliably.