Skip to content

fix: use stackless singleton error for test permission - #1250

Merged
ferhatelmas merged 1 commit into
masterfrom
ferhat/stackless-singleton-test-permission
Jul 21, 2026
Merged

fix: use stackless singleton error for test permission#1250
ferhatelmas merged 1 commit into
masterfrom
ferhat/stackless-singleton-test-permission

Conversation

@ferhatelmas

Copy link
Copy Markdown
Member

What kind of change does this PR introduce?

refactor for performance

What is the current behavior?

On hot path (RLS check), an error is thrown and caught immediately but it does stack allocation and walk the prototype chain.

What is the new behavior?

It doesn't carry information but a signal so a module singleton without stack and identity check is enough.

Additional context

It's 4-5x faster but latency win (0.1%) is small compared to other work so don't expect much there. However, it's hot path allocation, and it becomes zero now.

Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
Copilot AI review requested due to automatic review settings July 21, 2026 09:18
@ferhatelmas
ferhatelmas requested a review from a team as a code owner July 21, 2026 09:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Small, well-contained performance refactor that replaces a per-call new TestPermissionRollbackError() (stack allocation + prototype walk) with a frozen, stackless module-level singleton compared by reference (===) instead of instanceof.

What was reviewed: traced the sentinel error through withTransaction/runQuery's catch paths — mapPgError only transforms DatabaseError instances, so the singleton's identity survives unchanged through nested/savepoint rollback paths. Checked for other instanceof TestPermissionRollbackError" or mutation of the frozen instance elsewhere in the codebase — none found. New tests cover single, throwing, concurrent, and nested-savepoint testPermission` flows, including a concurrency test confirming the shared singleton is safe when multiple in-flight calls throw the same object.

Extended reasoning...

Overview

This PR touches only src/storage/database/pg.ts and its test file. It replaces the per-call allocation of TestPermissionRollbackError in StoragePgDB.testPermission with a single frozen, stack-less instance created once at module load, and switches the catch-side check from instanceof to reference equality (===). This is a hot-path micro-optimization for the RLS permission-check flow, which throws-and-immediately-catches a sentinel error to force a transaction rollback while keeping the callback's result.

Security risks

None identified. The change does not affect authorization logic, SQL construction, or transaction semantics — it only changes how the internal control-flow sentinel is represented and detected. The actual RLS/permission check behavior (running the callback inside a transaction, then always rolling back) is unchanged.

Level of scrutiny

This warrants light-to-moderate scrutiny: it's a pure internal refactor of an error-signaling mechanism confined to one class, with no behavior change from the caller's perspective (uploader.ts, storage.ts, object.ts all just call testPermission and get back the callback result or a rethrown error). I verified the singleton's identity is preserved as it passes through mapPgError/mapPgErrorWithQueryName (which only rewrite DatabaseError instances) in both the plain-transaction and nested-savepoint rollback paths, and confirmed there are no other instanceof checks against TestPermissionRollbackError elsewhere in the codebase that the switch to reference equality could break.

Other factors

The PR adds solid test coverage for this exact change: a basic success case, an error-rethrow case, a concurrency case explicitly designed to catch bugs from sharing one frozen error object across in-flight `testPermission" calls, and two nested-savepoint cases. This gives good confidence the shared-singleton approach is safe under concurrent use, which was the main risk I'd otherwise worry about.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 29817741293

Coverage increased (+0.03%) to 79.579%

Details

  • Coverage increased (+0.03%) from the base build.
  • Patch coverage: 5 of 5 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 12757
Covered Lines: 10600
Line Coverage: 83.09%
Relevant Branches: 7448
Covered Branches: 5479
Branch Coverage: 73.56%
Branches in Coverage %: Yes
Coverage Strength: 426.57 hits per line

💛 - Coveralls

@ferhatelmas
ferhatelmas merged commit 4128b9e into master Jul 21, 2026
27 checks passed
@ferhatelmas
ferhatelmas deleted the ferhat/stackless-singleton-test-permission branch July 21, 2026 09:26
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.

4 participants