Repository navigation
Description
Link to reproduction
Describe the Bug
Payload server randomly returns wrong result for specific GraphQL query
Example
Collections: https://github.com/ikenox/payload/blob/wrong-graphql-query-result/test/wrong-graphql-query-result/collections.ts
Initial data: https://github.com/ikenox/payload/blob/wrong-graphql-query-result/test/wrong-graphql-query-result/config.ts#L12-L23
Query:
query {
Shops {
docs {
name
items {
name
}
}
}
Items {
docs {
name
itemTags {
name
}
}
}
}Expected response is below:
{
"data": {
"Shops": {
"docs": [
{
"name": "shop1",
"items": [
{
"name": "item1"
}
]
}
]
},
"Items": {
"docs": [
{
"name": "item1",
"itemTags": [
{
"name": "tag1"
}
]
}
]
}
}
}But randomly, about one out of every two times, wrong result are returned that has no items on Shops.docs.items field.
{
"data": {
"Shops": {
"docs": [
{
"name": "shop1",
"items": []
}
]
},
"Items": {
"docs": [
{
"name": "item1",
"itemTags": [
{
"name": "tag1"
}
]
}
]
}
}
}To Reproduce
I created a failing test.
Please execute pnpm test:int:postgres wrong-graphql-query-result on my reproducible example code https://github.com/ikenox/payload/tree/wrong-graphql-query-result .
Or you can test the query manually.
pnpm dev:postgres wrong-graphql-query-result
open http://localhost:3000/api/graphql-playgroundPayload Version
2.11.1
Adapters and Plugins
db-postgres
Activity
[-]GraphQL query returns wrong result[/-][+]GraphQL query randomly returns wrong result[/+]ikenox commentedon Feb 18, 2024
I've investigated the detailed behaviour.
To obtain hasMany relationship data, dataloader is called with current
req.transactionID(usually the value isnull)payload/packages/payload/src/graphql/schema/buildObjectType.ts
Lines 354 to 366 in 453ac21
keysvariable in dataloader hasnulltransactionID.payload/packages/payload/src/collections/dataloader.ts
Line 23 in 453ac21
payload.findis called in dataloaderpayload/packages/payload/src/collections/dataloader.ts
Line 107 in 453ac21
initTransactionis called in find operation function.payload/packages/payload/src/collections/operations/find.ts
Line 41 in 453ac21
If any other concurrent task in the same request is trying to issue transaction, just wait it and return
false.payload/packages/payload/src/utilities/initTransaction.ts
Lines 13 to 17 in 453ac21
req.transactionIDis changed before and after this line, perhaps by other concurrent dataloader task. At this point, thereq.transactionIDis no longer null.The returned
falseis assigned toshouldCommit, soreq.transactionIDis not deleted at the end of find operation.payload/packages/payload/src/collections/operations/find.ts
Line 253 in 453ac21
As a result, transactionID of
docKeyis not null while that ofkeysisnull. It causesdocIndex=-1payload/packages/payload/src/collections/dataloader.ts
Lines 129 to 140 in 453ac21
[-]GraphQL query randomly returns wrong result[/-][+]Concurrency issue: GraphQL query randomly returns wrong result[/+]ikenox commentedon Feb 18, 2024
Moving
req.transactionID = transactionIDafterpayload.find(...)statement superficially resolves the problem, but I'm not confident that it is the right solution.payload/packages/payload/src/collections/dataloader.ts
Lines 104 to 124 in 453ac21
SimYunSup commentedon May 31, 2024
Bump because latest payload has same problem.
payload: 2.18.3@payloadcms/db-postgres: 0.8.4payload: beta (in my computer)I think that first
payload.findmust havereq.transactionIDto equalizetransactionID(This is my guess, so I could be wrong).So How about change first value in docKeys to
transactionID?Expected:
SimYunSup commentedon Jun 8, 2024
Addtional:
Moving
req.transactionID = transactionIDafterpayload.find(...)cause randomly nested null return 😢 .ikenox commentedon Jun 18, 2024
Maybe #6821 fixes this issue.
SimYunSup commentedon Jun 18, 2024
Unfortunately, I think this patch is a workaround.
As written above, as @tychedelia said in #6800, this patch appears to solve the problem by accepting the
req.transactionIDcleared bypayload.find, but it's not the way the payloadCMS team intended to handle GraphQL's loader in a single transaction. I don't know how @DanRibbens solves this, but I think there needs to be logic to not commit in payload.find when given a parent transactionId.In addition to, it doesn't find the key in the doc (and I don't know where it would show up as a bug).
payload/packages/payload/src/collections/dataloader.ts
Lines 145 to 150 in e8165b7
tychedelia commentedon Jun 18, 2024
It's possible that the transaction id simply needs to be removed from the cache key. However, as it stands, the existing logic contains a race condition that triggers the bug described in #6800.
SimYunSup commentedon Jun 19, 2024
Sorry, I misunderstood your code. I didn't see that if the req passed to find has a transactionId, it doesn't commitTransaction.
@tychedelia I think the error is due to the req.transactionId being tainted and not finding the key in the above part of the code, but it looks like #6821 doesn't really decontaminate the req.transactionID, and it seems necessary to pass the transactionId so that it can be rolled back or killed if given.
However, when I pass the req to isolateObjectProperty, the transactionId is not passed, so it is not easy to rollback or kill. So instead of using isolateObjectProperty, I think it is correct to use transactionID instead of req.transactionID when creating a docKey like the code in this comment.
I'm still not fixing the bug in #6821 where the relationship filter in the where clause doesn't include the results because of a different transactionId than the docKey I created above due to a tainted req.transactionId. I won't create a reproduction right now, but I'll try to do so as soon as I can.
fix(payload): #6800, #5108 - graphql query concurrency issues
fix(payload): #6800, #5108 - graphql query concurrency issues (#6857)
jmikrut commentedon Jun 25, 2024
Closed by #6857
THANK YOU EVERYONE for the reconnaissance, the reproductions, and the thinking!
fix: graphql query concurrency issues (#6925)
fix: graphql query concurrency issues (#6925)
github-actions commentedon Sep 7, 2024
This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.