Skip to content

Payload 2.0: saving on MongoDB crash the server with plugin NestedDocs #4077

Closed

Description

@matteo-naif

Link to reproduction

Describe the Bug

When I try to save a new document payload crash with the following error:

[15:46:31] ERROR (payload): NotFound: The requested resource was not found.
    at findByID (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/payload/src/collections/operations/findByID.ts:114:15)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async findByIDHandler (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/payload/src/collections/requestHandlers/findByID.ts:19:17)
[15:46:37] WARN (payload): rollbackTransaction called when no transaction exists
/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/cmap/connection.ts:449
          callback(new MongoServerError(document));
                   ^
MongoServerError: Transaction with { txnNumber: 15 } has been committed.
    at Connection.onMessage (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/cmap/connection.ts:449:20)
    at MessageStream.<anonymous> (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/cmap/connection.ts:241:56)
    at MessageStream.emit (node:events:513:28)
    at MessageStream.emit (node:domain:489:12)
    at processIncomingData (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/cmap/message_stream.ts:188:12)
    at MessageStream._write (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/cmap/message_stream.ts:69:5)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at MessageStream.Writable.write (node:internal/streams/writable:337:10)
    at TLSSocket.ondata (node:internal/streams/readable:766:22) {
  ok: 0,
  code: 256,
  codeName: 'TransactionCommitted',
  '$clusterTime': {
    clusterTime: Timestamp { low: 3, high: 1699544797, unsigned: true },
    signature: { hash: [Binary], keyId: [Long] }
  },
  operationTime: Timestamp { low: 2, high: 1699544797, unsigned: true },
  [Symbol(errorLabels)]: Set(0) {}
}
[nodemon] app crashed - waiting for file changes before starting...

Despite the error, in the database there is the document.
Instead if I add the revisions I found two revisions and one doc from this saving.

After the saving the UI tells me that the document doesn't exists.

The error occour with the database in digitalOcean instead it didn't happen in local (I created a new database to test it).

Before migrating to Payload 2 everything worked well.

If I remove the nestedDocs plugin, it work correctly.

To Reproduce

  1. Add the following config in payload.config.ts
export default buildConfig({
  db: mongooseAdapter({
    url: process.env.MONGODB_URI,
    connectOptions: {},
  }),
  editor: lexicalEditor({}),
  admin: {
    bundler: webpackBundler(),
  },
  collections: [
    {
      slug: "posts",
      fields: [
        {
          name: "title",
          type: "text",
        },
      ],
    },
  ],
  plugins: [
    nestedDocs({
      collections: ["posts"],
      generateLabel: (docs, currentDoc) => {
        if (typeof currentDoc.title === "string") return currentDoc.title;
      },
      generateURL: (docs, currentDoc) => {
        return docs.reduce((url, doc) => `${url}/${doc.slug}`, "") + "/";
      },
    }),
  ],
});
  1. Login in the admin interface
  2. Add a new document
  3. the UI tells you that the document doesn't exist
  4. the backend crash and nothing works
  5. restart the server
  6. If you enable revisions in the list you see two documents
  7. If you don't enable revisions in the list you see one document
  8. click on the document, edit it and save it
  9. the backend crash again

Payload Version

2.1.0

Adapters and Plugins

	"@payloadcms/bundler-webpack": "1.0.5", 		"@payloadcms/db-mongodb": "1.0.7", 		"@payloadcms/plugin-nested-docs": "1.0.8", 		"@payloadcms/richtext-lexical": "0.1.16",

Activity

  1. added
    status: needs-triage
    on Nov 9, 2023
  2. revnelson commented on Nov 9, 2023

    @revnelson

    Had this same issue, so I extracted the code from the plugin into hooks for a Categories collection. It seems like the issue is where the req object is passed to the LocalAPI calls. By changing this line from ({ req: { payload, locale }, req, doc }) to ({ req, doc }) and changing all locale and payload references to req.locale and req.payload respectively, as well as removing req from all payload LocalAPI calls (i.e. payload.find payload.update etc) it seems to be working fine.

    Using req twice in the arguments such as ({ req: { payload, locale }, req, doc }) seems odd to me, so I'm not sure if that's an issue, or if it was req being passed to LocalAPI (and what the purpose for that is in this instance). I also found in testing my "fixes" that it will still crash if there is circular nesting (i.e. B has parent A, C has parent B, A has parent C). Can't remember if the plugin had a guard against that, but I'll probably need to build out some filtering rule to not let that happen.

  3. DanRibbens commented on Nov 9, 2023

    @DanRibbens
    Contributor

    The purpose for passing req to the local API is that it will force Payload to use the same transaction if transactions are being used by the database adapter. Transactions make sure you aren't updating one document, getting an error without rolling back changes in the DB.

    Since 2.1.0 included some DB transaction changes, I'm guessing that must be related.

    Thanks for reporting. I should have all the info I need to provide a fix.

  4. matteo-naif commented on Nov 13, 2023

    @matteo-naif
    Author

    The problem isn't resolved, I have the following error saving a new doc in an empty collection:

    /Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/transactions.ts:168
        throw new MongoRuntimeError(
              ^
    MongoRuntimeError: Attempted illegal state transition from [TRANSACTION_COMMITTED] to [TRANSACTION_ABORTED]
        at Transaction.transition (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/transactions.ts:168:11)
        at commandHandler (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/sessions.ts:740:27)
        at /Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/sessions.ts:810:7
        at /Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/utils.ts:462:14
        at processTicksAndRejections (node:internal/process/task_queues:95:5) {
      [Symbol(errorLabels)]: Set(0) {}
    }
    [nodemon] app crashed - waiting for file changes before starting...
  5. BrianJM commented on Feb 22, 2024

    @BrianJM

    The problem isn't resolved, I have the following error saving a new doc in an empty collection:

    /Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/transactions.ts:168
        throw new MongoRuntimeError(
              ^
    MongoRuntimeError: Attempted illegal state transition from [TRANSACTION_COMMITTED] to [TRANSACTION_ABORTED]
        at Transaction.transition (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/transactions.ts:168:11)
        at commandHandler (/Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/sessions.ts:740:27)
        at /Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/sessions.ts:810:7
        at /Users/matteotortelli/progetti/evoluzione/evoluzione-backend/node_modules/mongodb/src/utils.ts:462:14
        at processTicksAndRejections (node:internal/process/task_queues:95:5) {
      [Symbol(errorLabels)]: Set(0) {}
    }
    [nodemon] app crashed - waiting for file changes before starting...

    @matteo-naif Still seeing this in #4350.

  6. TechSynthesis commented on Feb 27, 2024

    @TechSynthesis

    @BrianJM Did you find the solution ? I'm getting this sporadically as well

  7. BrianJM commented on Feb 28, 2024

    @BrianJM

    @TechSynthesis Are you seeing this sporadically only with Nested Docs or also without?

    #4350 fixed the errors I saw without Nested Docs. It looks to be in the next release. I have not been able to test with Nested Docs yet.

  8. TechSynthesis commented on Feb 28, 2024

    @TechSynthesis

    @BrianJM I started experiencing this when I updated one of my clients projects from payload 1.5 to 2.x. Went thought the whole migration process. Not sure where it's coming from. I do have a few collections that are nestedDocs

  9. DanRibbens commented on Feb 28, 2024

    @DanRibbens
    Contributor

    Anything more you can share @TechSynthesis? Ideally a reproducable issue you can document, but I can even debug from your project if you want to add me privately.

    Also, what version of @payloadcs/plugin-nested-docs?

  10. 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.

  11. 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

No one assigned

    Labels

    status: needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions