Skip to content

fix(api): log and metrics reads hold no account lock or transaction while the store answers (closes #488) - #509

Merged
samuelmbabhazi merged 3 commits into
mainfrom
fix/488-observability-outside-lock
Nov 26, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
fix/488-observability-outside-lock

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

What changed

LogsQueries.search and MetricsQueries.application, node and organization now split each read into three steps:

  1. Authorize, in the usual AccountMutationAuthority.run: account, API token scope, permission, and the resource's existence. This is a short transaction under the account's lock.
  2. Read the store with no transaction and no lock held.
  3. Authorize again before anything is returned.

For the organization chart, the second check also requires that every environment the read covered is still readable. If one is not, the read is refused with the usual 404, rather than returning series the person may no longer read.

SPEC 12 (logs and metrics) states the rule.

Why

Closes #488. Each read ran inside the authority's withUserLock, which holds the account's row with SELECT … FOR UPDATE and a database transaction for the whole callback. A slow VictoriaLogs or VictoriaMetrics, up to its 10 s timeout, held that lock and a pooled connection. It delayed every other operation of that account, password resets and permission changes included. It also broke the unit of work's rule that network I/O happens outside a transaction.

How it was tested

  • logs-lock.integration.spec.ts (new): real HTTP pipeline and PostgreSQL, with the log store replaced by a fake that waits.
    • While it waits, pg_stat_activity shows no idle in transaction connection, and a project creation by the same owner, which takes the account's lock, answers 201.
    • A viewer whose role binding is deleted while the store answers gets 404, without the line.
    • On the old code, both fail: one connection is idle in transaction, and the viewer gets 200 with the line.
  • logs-queries.spec.ts and metrics-queries.spec.ts:
    • Every store call, for logs and for application, server and organization metrics, happens outside the authority's callback, and each answer is authorized twice.
    • Access withdrawn during the read rejects the answer.
    • An environment that becomes unreadable during an organization read refuses it.
    • The three metrics cases and the logs case failed on the old code.
  • The API unit suite passes (105 files, 907 tests), as do api:lint and api:typecheck.

Security considerations

The second check is the same one as the first, so an API token revoked, an account disabled or a role removed during the read returns nothing. Tenant filtering, response bounds and the store's sanitized 503 errors are unchanged. Upstream timeouts and oversized answers are still handled by the stores, whose code is unchanged.

…hile the store answers (closes #488)

Each read ran inside the account mutation authority, so a slow VictoriaLogs or VictoriaMetrics held the account's row lock and a database transaction for up to its timeout. Access is now checked in a short transaction, the store is read outside any, and access is checked again before anything is returned: access lost during the read returns nothing.

Signed-off-by: samuel mbabhazi <[email protected]>
@samuelmbabhazi samuelmbabhazi added bug Something isn't working track:control-plane labels Nov 26, 2026
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 14 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3c9f1b3 and 4a295d8.

Files selected for processing: 6

  • apps/api/src/modules/observability/application/logs-queries.spec.ts
  • apps/api/src/modules/observability/application/logs-queries.ts
  • apps/api/src/modules/observability/application/metrics-queries.spec.ts
  • apps/api/src/modules/observability/application/metrics-queries.ts
  • apps/api/src/modules/observability/logs-lock.integration.spec.ts
  • docs/SPEC.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@samuelmbabhazi
samuelmbabhazi merged commit d1a08b3 into main Nov 26, 2026
27 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the fix/488-observability-outside-lock branch November 26, 2026 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working track:control-plane

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Release account locks before querying external observability stores

1 participant