refactor: Migrate Tenant API handler to WithTx#559
Conversation
Continues the `WithTx` migration, this time covering the `GetCurrentTenantHandler` flow — the only `BeginTx` site in `tenant.go`. Some callouts are: - Uses `WithTxResult` since each of the three branches (create-new, update-display-name, return-as-is) naturally returns the resulting tenant. - Pulled the `tnDAO.GetAllByOrg` pre-check inside the tx with an advisory lock on the org to close a TOCTOU race where concurrent requests could both pass the "no tenant exists" check and double-create. - The `taDAO.GetAll` inside `updateTenantAccounts` stays inside since its result drives in-tx writes. - Errors switched to `NewAPIError`. Signed-off-by: Chet Nichols III <chetn@nvidia.com>
Summary by CodeRabbit
Walkthrough
ChangesTenant Handler Transaction Refactoring
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
🔐 TruffleHog Secret Scan✅ No secrets or credentials found! Your code has been scanned for 700+ types of secrets and credentials. All clear! 🎉 🕐 Last updated: 2026-05-21 19:51:58 UTC | Commit: c4fea2b |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
api/pkg/api/handler/tenant.go (1)
164-209: 🏗️ Heavy liftAdd a concurrent same-org regression test for this flow.
This refactor changes the transaction boundary and the get-or-create concurrency behavior. A test that drives two same-org
GetCurrentTenantHandler.Handlerequests concurrently and asserts a single tenant is created while both requests succeed would lock in the TOCTOU fix and catch lock-handling regressions.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/pkg/api/handler/tenant.go` around lines 164 - 209, Add a concurrent same-org regression test that exercises the new transactional get-or-create path in GetCurrentTenantHandler.Handle: spawn two concurrent requests targeting the same org and assert both return success while only one Tenant DB row is created; the test should exercise the advisory lock path via cdb.WithTxResult/TryAcquireAdvisoryLock and verify tnDAO.GetAllByOrg/CreateFromParams/UpdateFromParams are exercised only once (or result in a single created tenant), and also ensure updateTenantAccounts is invoked exactly once when a create occurs to catch TOCTOU or lock-handling regressions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@api/pkg/api/handler/tenant.go`:
- Around line 164-209: Add a concurrent same-org regression test that exercises
the new transactional get-or-create path in GetCurrentTenantHandler.Handle:
spawn two concurrent requests targeting the same org and assert both return
success while only one Tenant DB row is created; the test should exercise the
advisory lock path via cdb.WithTxResult/TryAcquireAdvisoryLock and verify
tnDAO.GetAllByOrg/CreateFromParams/UpdateFromParams are exercised only once (or
result in a single created tenant), and also ensure updateTenantAccounts is
invoked exactly once when a create occurs to catch TOCTOU or lock-handling
regressions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: f953ff03-69be-4d2d-a91a-b84c64fae0f2
📒 Files selected for processing (1)
api/pkg/api/handler/tenant.go
🔍 Container Scan Summary
Per-CVE detail lives in the per-service |
thossain-nv
left a comment
There was a problem hiding this comment.
Looks good, thank you @chet
Description
Continues the
WithTxmigration, this time covering theGetCurrentTenantHandlerflow — the onlyBeginTxsite intenant.go.Some callouts are:
WithTxResultsince each of the three branches (create-new, update-display-name, return-as-is) naturally returns the resulting tenant.tnDAO.GetAllByOrgpre-check inside the tx with an advisory lock on the org to close a TOCTOU race where concurrent requests could both pass the "no tenant exists" check and double-create.taDAO.GetAllinsideupdateTenantAccountsstays inside since its result drives in-tx writes.NewAPIError.Signed-off-by: Chet Nichols III chetn@nvidia.com
Type of Change
Services Affected
Related Issues (Optional)
Breaking Changes
Testing
Additional Notes