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 6 months 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