Repository navigation
fix(api): count Telegram connection slots under the company lock - #523
Merged
Merged
Conversation
POST /channel-accounts/telegram-bot checked the paid connection quota before its insert transaction and took no lock, so two concurrent connects (or a connect racing a WhatsApp spawn) could both see the same count and both insert, leaving the workspace over its plan limit. Do the count inside the insert transaction behind the company-scoped pg_advisory_xact_lock that spawnConnection already takes, and keep the route's 402 MAX_CONNECTIONS_EXCEEDED response. Reconnecting an existing account still does not consume a slot. The integration test drives the real route with the Telegram Bot API stubbed at fetch, and holds the company lock open to prove the Telegram path waits on it and then sees the slot as used. Fixes #379
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #379.
Problem
POST /channel-accounts/telegram-botchecked the paid connection quota before its insert transaction and took no lock. Two concurrent Telegram connects, or a Telegram connect racing a WhatsApp spawn, could both see the same slot count, both pass the check and both insert. The workspace then ends up over its plan's connection limit.Fix
pg_advisory_xact_lock(hashtextextended(companyId, 0))thatspawnConnectionalready takes. Telegram and WhatsApp connects now take turns.402 MAX_CONNECTIONS_EXCEEDEDresponse.Tests
channel-accounts.quota.integration.test.tsdrives the real route with the Telegram Bot API stubbed atfetch. Cases: two concurrent connects for the last slot admit exactly one (the other gets 402); a held WhatsApp-spawn lock makes the Telegram path wait and then see the slot as used; an existing account can still reconnect when the plan is full.main. Typecheck, biome and the new integration test pass on a fresh Postgres 18.Release
No migration.
worker: skip.