Uploaded image for project: 'XWiki Platform'
  1. XWiki Platform
  2. XWIKI-25285

Saving several pages in parallel in a new space fails with a duplicate key error on the space table

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • Major
    • None
    • 18.8.0
    • Old Core
    • None
    • Unknown

    Description

      Finding from a Kubernetes test cluster, reproduced on a single XWiki instance with PostgreSQL 17.

      Steps to reproduce

      1. Create 150 pages with the REST API, 10 requests in parallel, in a space that does not exist yet (PUT /rest/wikis/xwiki/spaces/A/spaces/B/pages/P1 ... P150).

      Result

      Some requests (8 of 200 and 9 of 150 in two runs, apparently among the first parallel requests) fail with HTTP 500 and the page is not created:

      XWikiException: Error number 3201 in 3: Exception while saving document xwiki:A.B.P12
      ...
      ERROR: duplicate key value violates unique constraint "xwikispace_pkey"
      

      Cause

      XWikiHibernateStore#maybeCreateSpace reads the space row without any lock and, when it is missing, insertXWikiSpace calls session.save(space) under the space lock added by XWIKI-21385. The XWikiSpace identifier is assigned and the session uses FlushMode.COMMIT, so save only queues the INSERT: it reaches the database when the document transaction commits, after the lock was released. Two transactions saving different pages of the same new space can both see no row and both insert it; the second commit fails and the whole page save is rolled back. The lock of XWIKI-21385 therefore does not prevent the error (the MySQL variant of that issue has the same cause), and a JVM lock cannot protect several cluster members anyway.

      Proposed fix

      Insert the missing space rows in a separate short transaction that treats a unique constraint violation as "the space already exists" (rollback that transaction and continue), and never insert the space row in the document transaction. Update the hidden flag of an existing space with an UPDATE statement.

      Attachments

        Issue Links

          Activity

            People

              ciaens Clément Christiaens
              ciaens Clément Christiaens
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated: