Skip to content

Attempted illegal state transition (Nested-Docs + MongoDB Atlas) #4072

Closed

Description

@davelsio

Link to reproduction

https://github.com/davelsan/payload-issues/tree/main/repros/nested-docs-transaction-error

Describe the Bug

I was only able to reproduce this problem when using MongoDB Atlas. I cannot reproduce in my local development environment (Community Edition)

This bug affects the @payloadcms/plugin-nested-docs plugin.

When editing a parent relationship (either add or remove), and after the operation was successful, when I navigate to a collection list sometimes I get this MongoDB runtime error.

<path/to/project>/node_modules/mongodb/src/transactions.ts:168
throw new MongoRuntimeError(
^
MongoRuntimeError: Attempted illegal state transition from [TRANSACTION_COMMITTED] to [TRANSACTION_ABORTED]
at Transaction.transition (<path/to/project>/node_modules/mongodb/src/transactions.ts:168:11)
at commandHandler (<path/to/project>/node_modules/mongodb/src/sessions.ts:740:27)
at <path/to/project>/node_modules/mongodb/src/sessions.ts:810:7
at <path/to/project>/node_modules/mongodb/src/utils.ts:462:14
at processTicksAndRejections (node:internal/process/task_queues:95:5)

It does not always happen, sometimes I have to edit the parent a couple of times before the error is thrown. It also does not matter what collection I navigate to afterwards, but it only happens in collection views. That is, if I navigate to the admin home page, no errors until I try requesting a collection list.

For some reason I cannot reproduce this issue using a local database, only the Atlas instance. Both my local and cloud instances have the same collections setup, hooks, etc. There are some data differences, but that's it. In the repro I have a simplified setup with only the nested collection. I still need to test this simplified setup in Atlas.

Any idea what could be happening here?

To Reproduce

Follow the instruction on the repro.

  1. Install dependencies.
  2. Create parent and child documents.
  3. Navigate to any collection view to get the list of documents.
  4. Stuck in loading skeleton?
    [YES] Check console errors.
    [NO] Edit the child location by adding or removing the parent until navigating to the collection view crashes.

Payload Version

2.1.0

Adapters and Plugins

"@azure/storage-blob": "^12.16.0", "@payloadcms/bundler-webpack": "1.0.5", "@payloadcms/db-mongodb": "1.0.7", "@payloadcms/plugin-cloud": "2.2.6", "@payloadcms/plugin-cloud-storage": "1.1.1", "@payloadcms/plugin-form-builder": "^1.0.15", "@payloadcms/plugin-nested-docs": "^1.0.8", "@payloadcms/richtext-lexical": "0.1.16", "payload-plugin-resolve-alias": "^0.1.0"

Activity

  1. added
    status: needs-triage
    on Nov 9, 2023
  2. changed the title [-]Attempted Illegal State Transition (MongoDB Atlas)[/-] [+]Attempted illegal state transition (MongoDB Atlas)[/+] on Nov 9, 2023
  3. davelsio commented on Nov 9, 2023

    @davelsio
    ContributorAuthor

    I noticed this code in plugin-nested-docs/src/index.ts

    beforeChange: [
      async ({ req, data, originalDoc }) => populateBreadcrumbs(req, pluginConfig, collection, data, originalDoc),
    ],

    Judging by the docs and what the other hooks in the plugin do, could it be caused because it does not await the populateBreadcrumbs function?

  4. changed the title [-]Attempted illegal state transition (MongoDB Atlas)[/-] [+]Attempted illegal state transition (Nested-Docs + MongoDB Atlas)[/+] on Nov 10, 2023
  5. matteo-naif commented on Nov 14, 2023

    @matteo-naif

    I have the same problem, any updates?

  6. georgeharito commented on Nov 14, 2023

    @georgeharito

    Same problem here too, however for me it occurs whenever I save any collections that use the nested-docs plugin, and not just when I go to view the list of records in a nested-docs-enabled collection.

  7. Keyideas-Rohan-Tiwari commented on Nov 17, 2023

    @Keyideas-Rohan-Tiwari

    Same problem with me any update? When I'm running with local mongoDb connection its working fine, But when connecting with Atlas i'm getting this error.

  8. hassanthegooner commented on Nov 18, 2023

    @hassanthegooner

    Getting the same issue using MongoDb Atlas but indeed, the seed works when using a local mongoDB instance

  9. jon8800 commented on Nov 19, 2023

    @jon8800

    Same issue here. This is related: #4077. @DanRibbens looks like it wasn't resolved with the recent update.

  10. added
    plugin: nested-docs
    db: mongodb
    on Nov 20, 2023
  11. DanRibbens commented on Nov 20, 2023

    @DanRibbens
    Contributor

    I tried very hard to reproduce this issue. Since I cannot I am going to commit the suggested fix from @Davelsan
    #4072 (comment). It probably should be awaiting that anyways.

    Another thing I considered is that the connection options being used in your mongodb connection string might have something to do with the variance in results. I haven't confirmed this, but I will share it anyways in case somebody else has experienced this or wants to try. I used a combination of retryWrites=true and w=majority when testing, perhaps there are other settings that are the cause.

  12. added
    status: cant-reproduce
    on Nov 20, 2023
  13. davelsio commented on Dec 3, 2023

    @davelsio
    ContributorAuthor

    Hey @DanRibbens, thanks for adding the fix. I have finally been able to update payload to 2.3.1. I haven't been able to reproduce the issue after the update, so that looks good so far. I can't say for sure if the fix was my suggestion or what you did on 2.1.1, but either way it seems fixed.

    I found another interesting (maybe-)bug with the plugin. I probably should report that separately, but just a comment in case someone else is running into this. It seems that using the createParentField factory function can produce the following error when trying to update the parent field.

    TypeError: Cannot read properties of undefined (reading 'read')                                                
        at <path/to/project>/node_modules/payload/src/database/queryValidation/validateSearchParams.ts:160:37      
        at processTicksAndRejections (node:internal/process/task_queues:95:5)                                                                  
        at async Promise.all (index 0)                                                                                                         
        at async validateSearchParam (<path/to/project>/node_modules/payload/src/database/queryValidation/validateS
    earchParams.ts:198:3)                                                                                                                      
        at async Promise.all (index 1)                                                                                                         
        at async validateQueryPaths (<path/to/project>/node_modules/payload/src/database/queryValidation/validateQu
    eryPaths.ts:89:5)                                                                                                                          
        at async find (<path/to/project>/node_modules/payload/src/collections/operations/find.ts:137:7)            
        at async findHandler (<path/to/project>/node_modules/payload/src/collections/requestHandlers/find.ts:30:20)
    

    I assume this must be a problem with access.read in the field config. Anyway, I'll provide more details and a repro when I get around to creating the issue. For now, not using the function is good enough for me.

    Thanks again for your efforts! ❤️

  14. bsides commented on Dec 6, 2023

    @bsides

    Hi guys, I'm getting the same error with payload-demo. I didn't do anything, just ran the project, after a while, it threw this message. Now every time I try to run the app via pnpm dev I get the same message. I'm using a new mongodb atlas and fresh install. My connection string is the one given by mongodb atlas, which is something like this: mongodb+srv://MY_USERNAME:MY_PASSWORD@cluster-CLUSTER_NAME.SOME_STRING.mongodb.net/

    image
  15. github-actions commented on Sep 7, 2024

    @github-actions
    Contributor

    This issue has been automatically locked.
    Please open a new issue if this issue persists with any additional detail.

  16. locked as resolved and limited conversation to collaborators on Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

db: mongodbplugin: nested-docsstatus: cant-reproducestatus: needs-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions