Skip to content

fix(connections): keep channel state consistent across reconnect, disconnect and token refresh - #1815

Merged
realcodesiman merged 3 commits into
mainfrom
realcodesiman/connection-bug
Nov 28, 2026
Merged

realcodesiman merged 3 commits into
mainfrom
realcodesiman/connection-bug

Conversation

@realcodesiman

Copy link
Copy Markdown
Contributor

Summary

  • A reconnect landing between a refresh cron's read and write no longer gets its new token overwritten by the stale one (Zalo's single-use refresh token would otherwise be burned too).
  • Orphan channels (satellite row + inbox, no Connection) now get Inbox.status restored on reconnect and set to needs_reauth when unhealthy, instead of staying out of sync.
  • The channels quota unit is released only after the caller's transaction commits, and refresh crons with no owner resolve it from the workspace, so a later reconnect cannot consume a second unit.

Changes

  • packages/business/src/connection/auth-cas.ts: compare-and-swap WHERE fragment on the stored access token, used by the per-channel services and refresh handlers in apps/worker/src/schedule/handlers/.
  • packages/business/src/connection/state-service.ts: pendingRelease / runDisconnectInTransaction, orphan-inbox status mirroring, owner resolution in unhealthy marking.
  • packages/connections: connect-session flow and Messenger teardown updates.
  • apps/builder: connect flow, webhook routing, Instagram/WhatsApp disconnect actions.
  • packages/database: index on connect_session target connection (new migration, not applied automatically) and WhatsApp repository change.

Test plan

  • pnpm lint
  • pnpm --filter builder check-types
  • pnpm --filter worker check-types
  • pnpm test (builder, worker, business, connections)
  • review the new migration SQL before running db:migrate
  • manual: reconnect a channel while a token refresh is in flight; disconnect inside a transaction that rolls back

…connect and token refresh

- guard token refresh writes with a compare-and-swap on the stored access
  token so a reconnect in between is not overwritten (Zalo's single-use
  refresh token would otherwise be burned)
- restore or flag Inbox.status for orphan channels with no Connection row
- release the channels quota unit only after the caller's transaction
  commits, and resolve the owner when refresh crons carry none
- index connect_session target connection
@github-actions github-actions Bot added the bug Something isn't working as expected label Nov 28, 2026
…ota release

- move accessTokenMatches from business/connection to packages/database
  so repositories and refresh handlers can use it without a cycle
- add createPendingRelease/PendingQuotaReleaseRef and extract
  reconnectOrphanInbox, which re-consumes channel quota when reviving an
  orphan inbox whose unit was already released
- update refresh handlers, disconnect actions and tests accordingly
… back usage with the tx

An orphan inbox disconnected before disconnectReason existed had its quota
unit released, so reviving it now consumes one again instead of bypassing the
channel limit. resumeTenantSuspended also writes the workspace usage row on the
caller's transaction so a reconnect rollback undoes it.
@realcodesiman
realcodesiman merged commit ca41d79 into main Nov 28, 2026
14 checks passed
@realcodesiman
realcodesiman deleted the realcodesiman/connection-bug branch November 28, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant