Skip to content

fix(license): hold the licence lock only for the count and insert (#396) - #499

Open
mblaauw wants to merge 1 commit into
mainfrom
fix/396-licence-lock-scope
Open

mblaauw wants to merge 1 commit into
mainfrom
fix/396-licence-lock-scope

Conversation

@mblaauw

@mblaauw mblaauw commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Closes #396.

Summary

Every worker launch took the installation-wide workers licence lock and kept it
until the handler returned, across the launcher HTTP call (up to 20 s), even when
the licence sets no worker limit. Launches were serialised behind one slow launcher
and other API replicas answered 409 license limit is busy.

  • worker_limit_guard returns None without touching the database when the
    licence sets no worker limit, and the launch handler drops the guard once the
    pending launch row exists (it already counts) and before calling the launcher.
  • Store::license_limit_guard bounds the in-process mutex wait by the same 10 s
    deadline as the advisory lock.
  • New AppState::license_limit_lock takes the lock without counting, so callers
    decide under the lock whether a slot is needed:
    • activating an agent reads its status under the lock (an agent archived after
      the access check is still counted);
    • create_execution_with_limit looks the idempotency key up again under the
      lock, so a concurrent duplicate at the exact limit gets the first execution
      instead of 402;
    • a first OIDC login checks for a pre-provisioned user with that email under
      the seat lock and only counts seats when it would insert a new user.
  • seat_limit_guard is removed (its only caller now uses the lock directly).

Tests: launch with a slow launcher stub (the limit check does not wait for it),
no lock without a limit, two concurrent launches at limit+1, idempotent duplicate
at a zero execution limit, activate racing an archive. The launch and activate
tests fail with the fix reverted. Count-sensitive licence tests now serialise
with the tests that lower those counts. The OIDC seat change has no test: the
API tests have no OIDC login harness.

Testing

  • cargo fmt --check, clippy -D warnings: clean
  • cargo test --workspace on its own database: 766 passed, 0 failed
  • New tests: slow launcher stub (second lock within 2 s while the first launch waits; fails with the fix reverted), no lock without a limit, two concurrent launches at limit + 1, idempotent duplicate at a zero execution limit, activate racing an archive (fails with the fix reverted)
  • Count-sensitive licence tests now serialise with the tests that lower those counts
  • pnpm docs:check: passed

The OIDC first-login seat change has no test (no OIDC login harness in the API tests).

🤖 Generated with Claude Code

Every worker launch took the installation-wide `workers` licence lock and kept it
until the handler returned, across the launcher HTTP call (up to 20 s), even when
the licence sets no worker limit. Launches were serialised behind one slow launcher
and other API replicas answered `409 license limit is busy`.

- `worker_limit_guard` returns `None` without touching the database when the
  licence sets no worker limit, and the launch handler drops the guard once the
  pending launch row exists (it already counts) and before calling the launcher.
- `Store::license_limit_guard` bounds the in-process mutex wait by the same 10 s
  deadline as the advisory lock.
- New `AppState::license_limit_lock` takes the lock without counting, so callers
  decide under the lock whether a slot is needed:
  - activating an agent reads its status under the lock (an agent archived after
    the access check is still counted);
  - `create_execution_with_limit` looks the idempotency key up again under the
    lock, so a concurrent duplicate at the exact limit gets the first execution
    instead of 402;
  - a first OIDC login checks for a pre-provisioned user with that email under
    the seat lock and only counts seats when it would insert a new user.
- `seat_limit_guard` is removed (its only caller now uses the lock directly).

Tests: launch with a slow launcher stub (the limit check does not wait for it),
no lock without a limit, two concurrent launches at limit+1, idempotent duplicate
at a zero execution limit, activate racing an archive. The launch and activate
tests fail with the fix reverted. Count-sensitive licence tests now serialise
with the tests that lower those counts. The OIDC seat change has no test: the
API tests have no OIDC login harness.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Worker launches hold the installation-wide licence lock across the launcher HTTP call, even without a worker limit

1 participant