Skip to content

Concurrent scheduled publish and unpublish leave two versions flagged latest, and the unpublished document is still returned to an anonymous draft=true read #19451

Open

Description

@volodymyrshk

Describe the Bug

When a scheduled publish and a scheduled unpublish for the same document are both due and run together, the document ends up with two versions flagged latest. The document itself is unpublished, but a draft: true read by an anonymous user still returns the old published version.

The collection uses the documented access rule (public users only read _status: published). With the script in the reproduction, 11 of 12 documents end up in this state. Running only the publish job, or only the unpublish job, gives 0 of 12.

Cause, from reading createVersion in @payloadcms/drizzle: the new version is inserted with latest: true, then UPDATE ... SET latest = false WHERE id != <new> AND parent = <parent> AND updatedAt < <new updatedAt> demotes the older versions. In every run the two versions that stay latest have exactly the same updatedAt, to the millisecond (the reproduction prints it), so the strict < demotes neither of them. I did not look into why the two jobs get the same timestamp.

Effect: the draft read picks one of the two latest versions, here the published one. Reads with draft: false are correct (empty). I did not check whether other reads (GraphQL, versions endpoints) are affected.

Where this can happen: the schedule drawer lets an editor schedule both a publish and an unpublish for a document. If the job runner was down, both are overdue at restart and run together.

I tested SQLite only. The createVersion code is shared by the SQL adapters, so I expect the same on Postgres if two versions get the same timestamp there, but I have not run it. The Mongo adapter has its own implementation, which I did not read.

Suggested fix: demote the other versions of the parent by id != <new> without comparing updatedAt, or at least use <= so that an equal timestamp is demoted. I have not checked what else relies on the strict comparison (autosave, for example), so I would like your view before I send a PR with a test.

Link to the code that reproduces this issue

https://github.com/volodymyrshk/payload-draft-repros (npm install && npm run race)

Reproduction Steps

  1. git clone https://github.com/volodymyrshk/payload-draft-repros && cd payload-draft-repros
  2. npm install
  3. npm run race

The first document's versions are printed with their updatedAt and the [latest] flags, then the summary.

Which area(s) are affected?

area: core, db: sqlite

Environment Info

Payload: 3.93.0
@payloadcms/db-sqlite: 3.93.0
Node.js: v25.6.1
OS: macOS (Darwin)
Next.js: not used (Local API script)
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

    BugPayload OSSarea: coredb: sqlite

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions