Skip to content

Derby: deleting a user while an authorization plugin updates a user during login deadlocks the user controller #583

Open

Description

@pacmano1

On Derby, DefaultUserController.authorizeUser takes the PERSON read lock (StatementLock, a fair ReentrantReadWriteLock) and calls the authorization plugin while holding it. If the plugin calls updateUser in there (an LDAP plugin creating the account on first login, for example) and an admin deletes any user at the same moment, the two threads wait on each other for ever. After that every login and every authenticated API request hangs until the engine is restarted.

Only Derby takes this lock (vacuumPersonTable is only in derby-user.xml).

Lock order, engine v4.7.0 (main at dcd48b9, read but not run, has the same order at lines 292/300, 134 and 222/233):

  1. Login thread: authorizeUser takes the PERSON read lock (DefaultUserController.java:281), calls the plugin (:289), plugin calls updateUser, which is synchronized (:124) and waits for the controller's monitor.
  2. Delete thread: removeUser is synchronized (holds the monitor) and then asks for the PERSON write lock (:222), which waits for the login thread's read lock.
  3. Every later login asks for the read lock (:281) and queues behind the waiting writer, because the lock is fair.

Measured on 4.7.0 with Derby, RBAC 1.3.0 and an LDAP authorization plugin that creates the account on first login. The directory URL list started with an address that never answers, so the login stayed inside authorizeUser for the 8 s connect timeout:

  1. Start a first login for a directory user (plugin creates the account).
  2. Two seconds later, as admin, delete any other user.
  3. Two seconds later, log in as admin.

All three requests hung (curl gave up after 90, 60 and 40 s), and so did a fresh admin REST request. Thread dump:

"UserServlet Thread (Login)" BLOCKED (on object monitor)
    at ...DefaultUserController.updateUser(DefaultUserController.java:124)
    - waiting to lock <0x00000000f1b00098> (a ...DefaultUserController)
    at <plugin>.create(...)
    at ...DefaultUserController.authorizeUser(DefaultUserController.java:289)

"UserServlet Thread (Remove user)" WAITING (parking)
    - parking to wait for <0x00000000f2558fc8> (a ReentrantReadWriteLock$FairSync)
    at ...DefaultUserController.removeUser(DefaultUserController.java:222)
    - locked <0x00000000f1b00098> (a ...DefaultUserController)

"UserServlet Thread (Login)" WAITING (parking)
    - parking to wait for <0x00000000f2558fc8> (a ReentrantReadWriteLock$FairSync)
    at ...DefaultUserController.authorizeUser(DefaultUserController.java:281)

Only a restart cleared it. Neither the new account nor the delete had been written.

A plugin can't work around this: the lock is taken before the plugin is called. Possible fixes:

  1. Release the read lock before calling the authorization plugin, and take it only around the engine's own password check.
  2. Or take the write lock in removeUser before entering the monitor (same order as login).

Co-authored with Claude Code.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions