Skip to content

fix(feature): reuse caller transactions to avoid subscription pool exhaustion - #5255

Closed
turip wants to merge 3 commits into
openmeterio:mainfrom
turip:feat/feature-transactions
Closed

turip wants to merge 3 commits into
openmeterio:mainfrom
turip:feat/feature-transactions

Conversation

@turip

@turip turip commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Overview

Connection-pool exhaustion occurred during the subscription E2E stress test. Concurrent subscription creation held database connections while waiting for the customer lock, while feature queries in the transaction holding that lock attempted to acquire another pooled connection. With the pool exhausted, the lock holder could not proceed and the stress test timed out.

Make every feature repository operation join the Ent transaction in the caller's context, including eager meter loading, pagination counts, archive guards, and follow-up reads. Remove the metered grant-owner workaround now that the repository handles transaction binding itself.

Wrap feature create, update, and archive commands in transaction.Run so standalone commands own a transaction and nested commands reuse their caller's transaction. This also makes feature changes atomic with system-event outbox enqueueing.

Notes for reviewer

Coverage stays with the feature packages: one adapter regression holds the sole available connection and exercises repository operations against uncommitted data; connector tests verify publication-failure rollback and reuse of an outer transaction. Existing CRUD, workflow, and outbox tests retain their respective responsibilities. The feature package README documents the transaction contract.

Set E2E's existing structured postgres.options.poolMaxConns to 64 so test capacity is independent of runner CPU count. Fork Base E2E otherwise defaults to four connections, while non-fork Base E2E defaults to eight. Strip the pgx-only pool_max_conns option from migration connections, matching the existing Cloud behavior, so explicit pool configuration works with automatic migrations.

The fork Container Tests run passed: Quickstart, Base E2E, and credits-disabled E2E all succeeded. The subscription stress test completed in 0.12 seconds; its five requests took 61–118 milliseconds, compared with the previous 30-second timeouts on the same runner class and unchanged test deadline. This supports pool exhaustion as the cause of that timeout. Billing's default-profile lookup still requests a connection outside the caller's transaction; making that adapter transaction-aware remains separate work.

Local validation passed:

  • Affected product catalog, subscription workflow, metered entitlement, and outbox package tests.
  • Consolidated feature adapter and connector tests.
  • Consumer compilation and vet checks, plus scoped fast lint.
  • Migration up/down cycle, pool-option startup regression, and structured E2E pool configuration validation.

CI's unit tests, lint, migration checks, and generators also passed. Secret Scanning currently reports an unverified PostgreSQL finding in the disposable E2E configuration. The HTTP E2E suites were validated in fork CI rather than rerun locally.

Summary by CodeRabbit

  • Reliability
    • Feature creation, updates, and archival now run transactionally, keeping feature changes and related events consistent if an operation fails.
    • Feature operations can participate in an existing transaction, so changes made within it are visible to relevant reads before they are committed.
    • When no transaction is provided, feature operations continue to use the normal database connection.

RetriggerConfidence Score: 5/5

No outstanding review finding blocks merging, though the E2E rerun described in the PR remains pending.

Summary

The PR makes feature repository operations use a caller’s Ent transaction and wraps feature commands so persistence and event publication share a transaction. Since the previous review, it has changed the E2E PostgreSQL configuration to structured fields and removed the pool setting from migration connection strings.

  • Added regression coverage for transaction reuse, command rollback, and migration connection-string preparation.
  • The E2E pool limit remains 64; the PR description notes that an E2E rerun with the migration fix is still pending.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Caller[Caller transaction context] --> Connector[Feature connector]
  Connector --> Repository[Feature repository]
  Connector --> Publisher[Event publisher]
  Repository --> DB[(Ent transaction)]
  Publisher --> DB
Loading

Reviews (3) · Last reviewed commit: "fix(migrate): exclude pool settings from..."

@turip
turip requested a review from a team as a code owner October 6, 2026 08:45
@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: openmeterio/openmeter/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 67eb3564-05ac-4841-8abf-1d9d4b58c718
📥 Commits

Reviewing files that changed from the base of the PR and between 91c742c and 571d572.

📒 Files selected for processing (8)
  • openmeter/entitlement/metered/grant_owner_adapter.go
  • openmeter/productcatalog/adapter/feature.go
  • openmeter/productcatalog/adapter/feature_transaction_test.go
  • openmeter/productcatalog/adapter/transaction.go
  • openmeter/productcatalog/feature/README.md
  • openmeter/productcatalog/feature/connector.go
  • openmeter/productcatalog/feature/connector_transaction_test.go
  • openmeter/productcatalog/feature/repository.go
💤 Files with no reviewable changes (1)
  • openmeter/productcatalog/feature/repository.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

Feature repository operations now use transactions supplied through context. Feature connector create, update, and archive flows run repository work and event publication within transactions. Tests cover transaction reuse, publisher failures, and rollbacks.

Changes

Feature transaction participation

Layer / File(s) Summary
Repository transaction support
openmeter/productcatalog/feature/repository.go, openmeter/productcatalog/adapter/transaction.go, openmeter/productcatalog/adapter/feature.go, openmeter/entitlement/metered/grant_owner_adapter.go, openmeter/productcatalog/adapter/feature_transaction_test.go
The feature adapter binds transaction-scoped clients and routes repository operations through them. FeatureRepo no longer embeds TxUser, and DescribeOwner calls the feature repository directly. An integration test checks transaction reuse and visibility of uncommitted data.
Transactional connector writes
openmeter/productcatalog/feature/connector.go, openmeter/productcatalog/feature/connector_transaction_test.go, openmeter/productcatalog/feature/README.md
Create, update, and archive flows run within transactions that include repository work and event publication. Tests cover publisher failures and outer transaction rollbacks. The README documents transaction behavior.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant featureConnector
  participant transactionRun
  participant featureRepo
  participant eventPublisher
  featureConnector->>transactionRun: Run create, update, or archive flow
  transactionRun->>featureRepo: Validate and persist using transaction context
  transactionRun->>eventPublisher: Publish event using transaction context
Loading

Suggested reviewers: galexihu

Merge Risk: ⚪ Minimal · up to 571d5

Feature changes and their queued events appear to share a transaction. No actionable merge-blocking issue was identified; complete the planned CI and HTTP E2E checks.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: reusing caller transactions to prevent subscription pool exhaustion.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · 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

Comment @coderabbitai help to get the list of available commands.

@turip turip added the release-note/bug-fix Release note: Bug Fixes label Oct 6, 2026
@turip turip changed the title fix(feature): reuse caller transactions to avoid subscription pool deadlocks fix(feature): reuse caller transactions to avoid subscription pool exhaustion Oct 6, 2026
@turip
turip marked this pull request as draft October 6, 2026 09:03
Comment thread e2e/config.yaml Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant