payload icon indicating copy to clipboard operation
payload copied to clipboard

Defining FieldLevel Access rules on more than one `GlobalConfig` results in `500` errors when hitting access endpoint

Open 1b0t opened this issue 1 year ago • 5 comments

Link to reproduction

/api/access

Describe the Bug

This is probably related to https://github.com/payloadcms/payload/commit/ad42d541b342ed56463b81cee6d6307df6f06d7f

Hi there,

we ran into seemingly random 500 errors being thrown when hitting /api/access in multiple of our projects. After a lot of debugging I was able to reproduce the issue with a (really) minimal example detailed below.

When you define field level access rules on more than one GlobalConfig this results in some consistency error regarding database transactions. That's really everything required to reproduce the issue. (MongoDB Transactions must be enabled.)

i.E.

MongoServerError: Given transaction number 28 does not match any in-progress transactions. The active transaction number is 27
...
MongoServerError: Given transaction number 36 does not match any in-progress transactions. The active transaction number is 35

This is a minimal example which is sufficient to reproduce the error:

// payload.config.ts
import { GlobalConfig } from "payload/types";

export const AdminConfig: GlobalConfig = {
  slug: "admin-config",
  fields: [
    {
      name: "feature1",
      type: "checkbox",
      access: {
        read: () => true,
      },
    },
  ],
};

export const Settings: GlobalConfig = {
  slug: "settings",
  fields: [
    {
      name: "option1",
      type: "checkbox",
      access: {
        read: () => true,
      },
    },
  ],
};
...
 globals: [Settings, AdminConfig],
...

Thanks for the help on this! -Tobi

To Reproduce

npx create-payload-app@latest

  Welcome to Payload. Let's create a project!

✔ Project name? … access-endpoint-bug
✔ Choose project template › blank
✔ Select a database › MongoDB
✔ Enter MongoDB connection string … mongodb://127.0.0.1/access-endpoint-bug

connect to mongo server with transactions enabled or use this docker-compose.yml

version: "3"

services:
  payload:
    image: node:18-alpine
    ports:
      - "3000:3000"
    volumes:
      - .:/home/node/app
      - node_modules:/home/node/app/node_modules
    working_dir: /home/node/app/
    command: sh -c "yarn install && yarn dev"
    depends_on:
      - mongo
    environment:
      DATABASE_URI: mongodb://mongo/access-endpoint-bug?replicaSet=rs0
    env_file:
      - .env

  mongo:
    image: mongo:6.0
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
    ports:
      - 27017:27017
    extra_hosts:
      - "host.docker.internal:host-gateway"
    healthcheck:
      test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'host.docker.internal:27017'}]}) }" | mongosh --port 27017 --quiet
      interval: 5s
      timeout: 30s
      start_period: 0s
      start_interval: 1s
      retries: 30
    volumes:
      - "mongo_data:/data/db"
      - "mongo_config:/data/configdb"

volumes:
  data:
  node_modules:
  mongo_data:
  mongo_config:
import { GlobalConfig } from "payload/types";

export const AdminConfig: GlobalConfig = {
  slug: "admin-config",
  fields: [
    {
      name: "feature1",
      type: "checkbox",
      access: {
        read: () => true,
      },
    },
  ],
};

export const Settings: GlobalConfig = {
  slug: "settings",
  fields: [
    {
      name: "option1",
      type: "checkbox",
      access: {
        read: () => true,
      },
    },
  ],
};
  • create first user
  • login
  • hit /api/access a couple of times

These errors will appear in console and it will cause 500 toast notifications in the admin UI.

payload-1  | [08:54:09] ERROR (payload): MongoServerError: Given transaction number 22 does not match any in-progress transactions. The active transaction number is 21
payload-1  |     at Connection.onMessage (/home/node/app/node_modules/mongodb/src/cmap/connection.ts:449:20)
payload-1  |     at MessageStream.<anonymous> (/home/node/app/node_modules/mongodb/src/cmap/connection.ts:241:56)
payload-1  |     at MessageStream.emit (node:events:517:28)
payload-1  |     at MessageStream.emit (node:domain:489:12)
payload-1  |     at processIncomingData (/home/node/app/node_modules/mongodb/src/cmap/message_stream.ts:188:12)
payload-1  |     at MessageStream._write (/home/node/app/node_modules/mongodb/src/cmap/message_stream.ts:69:5)
payload-1  |     at writeOrBuffer (node:internal/streams/writable:392:12)
payload-1  |     at _write (node:internal/streams/writable:333:10)
payload-1  |     at MessageStream.Writable.write (node:internal/streams/writable:337:10)
payload-1  |     at Socket.ondata (node:internal/streams/readable:809:22)

Payload Version

2.25.0

Adapters and Plugins

No response

1b0t avatar Aug 02 '24 09:08 1b0t

Hey @1b0t we've got this on our radar. It's been assigned internally and we'll report back with what we find out. Appreciate the detail here. Thanks!

zubricks avatar Aug 02 '24 15:08 zubricks

@1b0t how are you enabling MongoDB transactions without using the docker-compose file?

The run fails when I use the docker-compose.yml that you provided above. And I am not able to recreate the error when booting up without docker.

Could you confirm this error happens when using npx create-payload-app as you did above, then save the repository to GitHub and share it with us?

jessrynkar avatar Aug 05 '24 14:08 jessrynkar

@JessChowdhury, the docs indicate that transactions in MongoDB require a replica set, which isn't easy to set up locally. That's why we're using docker-compose. However, if you connect to a managed MongoDB cluster like Mongo Atlas, transactions should be available out of the box.

I've pushed my sample project here: https://github.com/1b0t/payload-issue-7482

1b0t avatar Aug 05 '24 14:08 1b0t

@1b0t not sure what I am missing, but I am unable to spin it up with your docker file.

I get:

ERROR (payload): Error: cannot connect to MongoDB. Details: connect ECONNREFUSED 127.0.0.1:27017

JarrodMFlesch avatar Aug 06 '24 16:08 JarrodMFlesch

@JarrodMFlesch is it possible that you have a MONGODB_URI specified in an .env file? That would override (I think - not 100% sure) the DATABASE_URI defined in the docker-compose.yml here? Otherwise i can't think of a reason why this would resolve to 127.0.0.1. For this setup the mongo uri must be mongodb://mongo/access-endpoint-bug?replicaSet=rs0.

1b0t avatar Aug 08 '24 06:08 1b0t

This issue was automatically closed due to lack of activity. If this issue is still relevant against the latest codebase, please create a new issue.

github-actions[bot] avatar Dec 06 '24 15:12 github-actions[bot]

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

github-actions[bot] avatar Dec 10 '24 04:12 github-actions[bot]