You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
v2.3.0 causes mongo transaction issue "Attempted illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED]" #4350
Apologies in advance, I'm not going to be able to provide many details here. I'm experiencing this in a large project I can't share and I have very little insight into what's causing it. What I do know is my unit/integration tests are working and passing on my project on 2.2.1, when I upgrade to 2.3.0 with no other changes, my seeds break with this error during a locale create call.
This only occurs in the seed script, calling create via local API. Creating a record in this same collection via admin works fine.
/Users/seth/dev/backend/node_modules/mongodb/src/transactions.ts:168
throw new MongoRuntimeError(
^
MongoRuntimeError: Attempted illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED]
at Transaction.transition (/Users/seth/dev/backend/node_modules/mongodb/src/transactions.ts:168:11)
at commandHandler (/Users/seth/dev/backend/node_modules/mongodb/src/sessions.ts:740:27)
at /Users/seth/dev/backend/node_modules/mongodb/src/sessions.ts:810:7
at /Users/seth/dev/backend/node_modules/mongodb/src/utils.ts:462:14
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
[Symbol(errorLabels)]: Set(0) {}
}
And seeing this response on the local API create call:
Due to the complexity of my project and the relative lack of details in the error, I really have no way to isolate this issue or provide a simplified reproduction. My best guess is this is related to something happening in a hook executing during create. The one other thing that's relevant is that this problem does not occur on the first 10 or so collection seeds which all complete as expected.
The error message is slightly different than the one in #4072, though.
[TRANSACTION_COMMITTED] to [TRANSACTION_ABORTED]
// versus
[TRANSACTION_ABORTED] to [TRANSACTION_ABORTED]
I was not having the problem after I updated. I'm going to check again though, I've been developing locally since then. I'm using a mongodb+srv connection with retryWrites=true&w=majority options. In case it helps.
I was not having the problem after I updated. I'm going to check again though, I've been developing locally since then. I'm using a mongodb+srv connection with retryWrites=true&w=majority options. In case it helps.
I have a little extra info here @DanRibbens upon closer inspection my seed script successfully creates one record on the collection in question (posts) in English, it then runs update in a loop and creates a translation for each locale available (in my case that's ~8 locales. All that executes without issue and I see the record and translations in the CMS. It then attempts to create a second post in English and that's when I get this error. I'm still investigating what about posts is different from the 12 collections this error doesn't occur on, it must be something happening in a hook that specific to posts but why this works on 2.1 and not 2.3 is confounding. I'm really hoping something about how transactions changed between those two versions will be obvious to someone!
I'm trying so hard to recreate the issues in this thread without success.
I was able to get some errors when seeding the public-demo against an atlas connection with transactions. This makes sense as the seeding function is creating and updating documents synchronously in a way that it shouldn't work. That said, I was getting write conflict errors which are expected with how it is currently written.
I think there is a documentation gap we need to cover. Ideally when you're doing your document changes like the seeding in public-demo you have two options:
Wrap all your local api calls in one transaction assigned to the req and pass it through to every operation. This way document changes occur in one session and will not have any write conflicts with (unless some other source is also actually conflicting).
await all local api calls so that they are asynchronously changing the database
This has been done throughout the core of payload and plugins and the /templates. It seems I also need to update our payload-demo.
The only known issue I've come across has to do with multiple synchrounous operations that error abortTransaction at the same time will cause a 500 and there is a PR and some discussion ongoing into that one.
I don't see what has changed from 2.1 that would cause this looking at the changes released.
I have a branch that will allow passing transactionOptions or disabling transaction usage altogether for mongodb. I don't want that to be the fix though.
Thanks for your patience while I figure things out.
I've got a bit more data here from my custom "payload shell", active transaction number -1 doesn't seem good! Also just to confirm I'm not doing any explicit transaction stuff at all. No accessing req.transactionID or proactively committing/aborting.
payload> r = await payload.create({collection:'posts',locale:'en',data})
Uncaught:
MongoServerError: Given transaction number 1 does not match any in-progress transactions. The active transaction number is -1
at Connection.onMessage (/Users/seth/dev/****/backend/node_modules/mongodb/src/cmap/connection.ts:449:20) {
ok: 0,
code: 251,
codeName: 'NoSuchTransaction',
'$clusterTime': {
clusterTime: new Timestamp({ t: 1702056495, i: 185 }),
signature: {
hash: new Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: 0
}
},
operationTime: new Timestamp({ t: 1702056495, i: 185 }),
[Symbol(errorLabels)]: Set(1) { 'TransientTransactionError' }
}
payload> Uncaught:
MongoRuntimeError: Attempted illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED]
at /Users/seth/dev/****/backend/node_modules/mongodb/src/sessions.ts:810:7
at commandHandler (/Users/seth/dev/****/backend/node_modules/mongodb/src/sessions.ts:740:27)
at Transaction.transition (/Users/seth/dev/****/backend/node_modules/mongodb/src/transactions.ts:168:11) {
[Symbol(errorLabels)]: Set(0) {}
}
payload> Uncaught:
MongoRuntimeError: Attempted illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED]
at /Users/seth/dev/****/backend/node_modules/mongodb/src/sessions.ts:810:7
at commandHandler (/Users/seth/dev/****/backend/node_modules/mongodb/src/sessions.ts:740:27)
at Transaction.transition (/Users/seth/dev/****/backend/node_modules/mongodb/src/transactions.ts:168:11) {
[Symbol(errorLabels)]: Set(0) {}
}
I need to switch gears here, but I have been able to reliably reproduce it and more importantly reliably avoid it. I can't say what I've found is super promising or helpful, but here it is.
I commented out data generators for my posts seeds and one by one uncommented and re-run seeds. What I determined was when I generated data for a relationship field I got the transaction error. If I commented out that generator, the error went away (I repeated this many times to make sure this was in fact triggering it). It's also worth noting I experimented a bit with some other relationship fields and some of them always work and some of them never work. I can't find any pattern, some relationships to upload collections work, others do not, some relationships to other non-upload collections work, others do not. I was even able to reproduce it in admin and this is where it gets even weirder:
I kept clicking Save over and over and the transaction id was always off by 1 and I kept getting that error, and then on the 9th time it saved successfully! It only happens on new posts, if I select that seed file on any other existing post I never get an error on save...
Also running into this issue with Payload 2.3.1 running on AWS app runner with DocumentDB. Our app runner instance stops/crash/restart with the following backtrace:
MongoServerError: Given transaction number 859 does not match any in-progress transactions.
at Connection.onMessage (/home/node/node_modules/mongodb/lib/cmap/connection.js:231:30)
at MessageStream.<anonymous> (/home/node/node_modules/mongodb/lib/cmap/connection.js:61:60)
at MessageStream.emit (node:events:514:28)
at processIncomingData (/home/node/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
at MessageStream._write (/home/node/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Writable.write (node:internal/streams/writable:337:10)
at Socket.ondata (node:internal/streams/readable:766:22)
at Socket.emit (node:events:514:28)
/home/node/node_modules/mongodb/lib/transactions.js:120
throw new error_1.MongoRuntimeError(`Attempted illegal state transition from [${this.state}] to [${nextState}]`);
MongoRuntimeError: Attempted illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED]
at Transaction.transition (/home/node/node_modules/mongodb/lib/transactions.js:120:15)
at commandHandler (/home/node/node_modules/mongodb/lib/sessions.js:478:33)
at /home/node/node_modules/mongodb/lib/sessions.js:530:9
at /home/node/node_modules/mongodb/lib/utils.js:348:66
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[Symbol(errorLabels)]: Set(0) {}
}
Node.js v18.17.0
@DanRibbens I believe I've found the culprit (at least in our setup). I've been able to avoid this by disabling filterOptions on the relationship field, which indicates to me that the associated filter/validation/find is somehow not part of the session/transaction. That's the best I can guess at this point!
This is a really disgusting bug - our product team have spent 2 months building a Payload implementation only for it to fall down at deployment time.
Really very worried about picking Payload now - because this error will only ever show when you try to deploy - and when you deploy it will crash your whole stack out - it needs some serious attention because you get to the end of your dev cycle and find out you can't send it live!! feels like a massive bait and switch.
Perhaps an update to the docs encouraging people to build and test on replicasets is needed so they don't get too far down and get stuck like we have? We would not have picked Payload if we had found this earlier.
looks like quite a few people in this thread are having 'go live' issues with Payload @jmikrut is this a P1 for your team?
Sorry but I don't get you. I'm using a map to get my seed data (ex. getFishersData) but it's synchronous and don't write to Payload. And my Payload writing function createCollectionItems is using a for loop rather than a map, and is awaiting each Promise to resolve before the next create operation. So I don't think that the transaction errors are related to this SO thread.
You still have access to my repo if you want to reproduce easily. The seed errors are definitely not generated by poorly handled async map. All the payload.create are awaited properly.
@iamacup What's in allProductDataRequirementBlocks?
I SUSPECT it is to do with the media collection and uploads but i will do more today to see if i can get this more minimal. EDIT: i have since removed the media collection entirely and the error is still there, in the example there are no relationship fields now.
I have shared it directly with you now @BrianJM. it ain't pretty though :)
I have shared it directly with you now @BrianJM. it ain't pretty though :)
Thanks @iamacup. I see you do not have relationships configured, but you do have Lexical (which can create relationships) so that may be what we have in common.
I have two collections that reference each other with relationships and virtual fields.
Pipelines have a (virtual) field with an afterRead hook to Stages. There is also a filterOption on the virtual field.
Stages have a relationship field back to Pipelines, and a (virtual) field back to another collection.
@DanRibbens
I can reduce the frequency of errors if I add if (findMany) return null at the top of my afterRead hooks. This reduces the number of queries server-side.
I can increase the frequency of errors by increasing the number or relationships and/or afterRead hooks (that use the Payload local API).
However, I consistently see two network requests (duplicates) and three server-side field hook executions.
Aside from this having a very negative impact on performance, the race may be occurring there (although not likely the root cause).
This code is responsible for duplicate network requests.
The screenshots below show loading a record from either collection. The last id is the transactionID.
I have depth set to 0 on all Local API queries, and they are as simple a a Local API query that is mapped.
@BrianJM i just removed lexical and still getting errors
both illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED] and Given transaction number 9 does not match any in-progress transactions. The active transaction number is 8
@BrianJM i just removed lexical and still getting errors
both illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED] and Given transaction number 9 does not match any in-progress transactions. The active transaction number is 8
does Slate add relationships?
@iamacup I'm not sure, but I think so. You can disable the features in Lexical. Link, relationship, and upload.
Can you try that? It may not matter since you removed Lexical.
I've gone and pruned out every single field apart from blocks and i am still getting the error reliably and instantly starting with no data - by creating a single record - my structure as you have seen is a bunch of nested blocks - i am certain somehow its getting messy down in the code when you get to a big nesting.
... my structure as you have seen is a bunch of nested blocks - i am certain somehow its getting messy down in the code when you get to a big nesting.
Yes, good point. This is consistent with what I see with relationships and hooks. The more relationships in a collection, or the more field hooks (with Local API calls) that are triggered as a result, the faster the error occurs.
@DanRibbens i have created a 'minimal' repo and shared it with you directly and am happy for you to share inside Payload - iamacup/payload-transactions - please read the readme.
I pulled your configuration into a new npx create-payload-app blank template. Connected to a mongodb Atlas instance to reproduce the errors.
With #5156, I've packed payload on main and copied it over to the test repo and the error is no longer occuring when I save new
You can read the description, but what was happening was a race condition in the access function where the more fields you have, the more likely it was to error.
There are still improvements to be made on transactions overall, as I am looking at feedback posted here: #4078 (comment)
Thanks everyone contributing to the conversation! This is high priority for us to get right.
Reacted by Michael Nelson, Enrico, Brian, Franck Martin, David Velasco and iamacup
Activity
bsides commentedon Dec 6, 2023
Same error here on a fresh install, using mongodb atlas and payload-demo, also reported here: #4072
davelsio commentedon Dec 6, 2023
The error message is slightly different than the one in #4072, though.
I was not having the problem after I updated. I'm going to check again though, I've been developing locally since then. I'm using a
mongodb+srvconnection withretryWrites=true&w=majorityoptions. In case it helps.bsides commentedon Dec 6, 2023
Thanks, I’m going to try those
ssyberg commentedon Dec 6, 2023
I've tried adding
retryWrites=true&w=majoritybut no change. We're stuck on 2.1 for the moment 😓ssyberg commentedon Dec 8, 2023
I have a little extra info here @DanRibbens upon closer inspection my seed script successfully
createsone record on the collection in question (posts) in English, it then runsupdatein a loop and creates a translation for each locale available (in my case that's ~8 locales. All that executes without issue and I see the record and translations in the CMS. It then attempts tocreatea second post in English and that's when I get this error. I'm still investigating what aboutpostsis different from the 12 collections this error doesn't occur on, it must be something happening in a hook that specific topostsbut why this works on 2.1 and not 2.3 is confounding. I'm really hoping something about how transactions changed between those two versions will be obvious to someone!DanRibbens commentedon Dec 8, 2023
I'm trying so hard to recreate the issues in this thread without success.
I was able to get some errors when seeding the public-demo against an atlas connection with transactions. This makes sense as the seeding function is creating and updating documents synchronously in a way that it shouldn't work. That said, I was getting write conflict errors which are expected with how it is currently written.
I think there is a documentation gap we need to cover. Ideally when you're doing your document changes like the seeding in public-demo you have two options:
reqand pass it through to every operation. This way document changes occur in one session and will not have any write conflicts with (unless some other source is also actually conflicting).awaitall local api calls so that they are asynchronously changing the databaseThis has been done throughout the core of payload and plugins and the
/templates. It seems I also need to update our payload-demo.The only known issue I've come across has to do with multiple synchrounous operations that error
abortTransactionat the same time will cause a 500 and there is a PR and some discussion ongoing into that one.I don't see what has changed from 2.1 that would cause this looking at the changes released.
I have a branch that will allow passing transactionOptions or disabling transaction usage altogether for mongodb. I don't want that to be the fix though.
Thanks for your patience while I figure things out.
ssyberg commentedon Dec 8, 2023
I've got a bit more data here from my custom "payload shell", active transaction number -1 doesn't seem good! Also just to confirm I'm not doing any explicit transaction stuff at all. No accessing
req.transactionIDor proactively committing/aborting.ssyberg commentedon Dec 8, 2023
I've gone through and confirmed every
createandupdateis awaited and passingreqalong.ssyberg commentedon Dec 8, 2023
I need to switch gears here, but I have been able to reliably reproduce it and more importantly reliably avoid it. I can't say what I've found is super promising or helpful, but here it is.
I commented out data generators for my

postsseeds and one by one uncommented and re-run seeds. What I determined was when I generated data for arelationshipfield I got the transaction error. If I commented out that generator, the error went away (I repeated this many times to make sure this was in fact triggering it). It's also worth noting I experimented a bit with some other relationship fields and some of them always work and some of them never work. I can't find any pattern, some relationships to upload collections work, others do not, some relationships to other non-upload collections work, others do not. I was even able to reproduce it in admin and this is where it gets even weirder:I kept clicking Save over and over and the transaction id was always off by 1 and I kept getting that error, and then on the 9th time it saved successfully! It only happens on new
posts, if I select that seed file on any other existing post I never get an error on save...mvdve commentedon Dec 11, 2023
Also running into this issue with Payload 2.3.1 running on AWS app runner with DocumentDB. Our app runner instance stops/crash/restart with the following backtrace:
Will update this post if i find more.
ssyberg commentedon Dec 12, 2023
@DanRibbens I believe I've found the culprit (at least in our setup). I've been able to avoid this by disabling
filterOptionson the relationship field, which indicates to me that the associated filter/validation/find is somehow not part of the session/transaction. That's the best I can guess at this point!mvdve commentedon Dec 13, 2023
Could not find a stable version so went the nuclear option and disabled transactions all together on version 2.4.0. Seems to be stable.
29 remaining items
sandwit commentedon Feb 22, 2024
This is a really disgusting bug - our product team have spent 2 months building a Payload implementation only for it to fall down at deployment time.
Really very worried about picking Payload now - because this error will only ever show when you try to deploy - and when you deploy it will crash your whole stack out - it needs some serious attention because you get to the end of your dev cycle and find out you can't send it live!! feels like a massive bait and switch.
Perhaps an update to the docs encouraging people to build and test on replicasets is needed so they don't get too far down and get stuck like we have? We would not have picked Payload if we had found this earlier.
looks like quite a few people in this thread are having 'go live' issues with Payload @jmikrut is this a P1 for your team?
franckmartin commentedon Feb 22, 2024
Hi @DanRibbens
You still have access to my repo if you want to reproduce easily. The seed errors are definitely not generated by poorly handled async map. All the payload.create are awaited properly.
iamacup commentedon Feb 22, 2024
I have shared it directly with you now @BrianJM. it ain't pretty though :)
mvdve commentedon Feb 22, 2024
@sandwit, You can disable all transactions by setting
transactionOptions: falsein the db adapter settings which will resolve your issues for now.BrianJM commentedon Feb 22, 2024
Thanks @iamacup. I see you do not have relationships configured, but you do have Lexical (which can create relationships) so that may be what we have in common.
I have two collections that reference each other with relationships and virtual fields.
Pipelineshave a (virtual) field with anafterReadhook toStages. There is also afilterOptionon the virtual field.Stageshave a relationship field back toPipelines, and a (virtual) field back to another collection.@DanRibbens
I can reduce the frequency of errors if I add
if (findMany) return nullat the top of myafterReadhooks. This reduces the number of queries server-side.I can increase the frequency of errors by increasing the number or relationships and/or
afterReadhooks (that use the Payload local API).However, I consistently see two
network requests(duplicates) and threeserver-sidefield hook executions.Aside from this having a very negative impact on performance, the race may be occurring there (although not likely the root cause).
This code is responsible for duplicate
network requests.The screenshots below show loading a record from either collection. The last
idis thetransactionID.I have
depthset to 0 on all Local API queries, and they are as simple a a Local API query that is mapped.iamacup commentedon Feb 22, 2024
@BrianJM i just removed lexical and still getting errors
both
illegal state transition from [TRANSACTION_ABORTED] to [TRANSACTION_ABORTED]andGiven transaction number 9 does not match any in-progress transactions. The active transaction number is 8does Slate add relationships?
BrianJM commentedon Feb 22, 2024
@iamacup I'm not sure, but I think so. You can disable the features in Lexical. Link, relationship, and upload.
Can you try that? It may not matter since you removed Lexical.
iamacup commentedon Feb 22, 2024
I've gone and pruned out every single field apart from blocks and i am still getting the error reliably and instantly starting with no data - by creating a single record - my structure as you have seen is a bunch of nested blocks - i am certain somehow its getting messy down in the code when you get to a big nesting.
https://github.com/iamacup/payload-transactions/pull/1
I can remove the error entirely if i just chop the block structure off half way down.
edit: what i have said there is clearly a lie there is an array as well - brb removing
edit2: removing the array did not fix it - it is now just blocks in the project
BrianJM commentedon Feb 22, 2024
Yes, good point. This is consistent with what I see with relationships and hooks. The more relationships in a collection, or the more field hooks (with Local API calls) that are triggered as a result, the faster the error occurs.
DanRibbens commentedon Feb 23, 2024
Hey @iamacup!
I pulled your configuration into a new npx create-payload-app blank template. Connected to a mongodb Atlas instance to reproduce the errors.
With #5156, I've packed payload on main and copied it over to the test repo and the error is no longer occuring when I save new
You can read the description, but what was happening was a race condition in the access function where the more fields you have, the more likely it was to error.
There are still improvements to be made on transactions overall, as I am looking at feedback posted here: #4078 (comment)
Thanks everyone contributing to the conversation! This is high priority for us to get right.
BrianJM commentedon Feb 23, 2024
@DanRibbens I pulled #5156, built and tested today. This fixed the errors I reported using the following config. Thank you! 👍
Do you want me to open a new issue for the duplicate network requests?
DanRibbens commentedon Feb 23, 2024
@BrianJM
Yes that is a good call!
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.