Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(relay): account deletion drops a live host's tunnel connectors #42430

Mergedjuliusmarmingemerged 1 commit intomain fromrefs/pull/42430/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

The new account-deletion monitor (#42285) paged in prod: 4 Clerk deletions failed on every sweep. Cloudflare refuses to delete a tunnel with live connectors (400 This tunnel has active connections). These users deleted their accounts while their T3 Code host was running, and a host whose link is revoked keeps its connector up until it restarts (CloudLink.markLinkRevoked deliberately keeps going, because a 401 can be clock skew). So the retries could never succeed on their own.

Fix

Account deletion now force-disconnects:

  • When the tunnel delete fails with active connections, the relay rotates the tunnel secret, which kills the host's connector token so it can't reconnect, then clears the tunnel's connections and deletes it. This is Cloudflare's documented token-rotation procedure for remotely managed tunnels.
  • deprovision and unlinkEnvironmentRecord take a forceDisconnect option, and only AccountDeletion sets it. Unlink, release and the reaper keep their current behaviour of waiting for hosts to stop.
  • Alchemy's tunnel binding doesn't expose connection cleanup. The relay now has its own binding (ManagedTunnelBinding) built with Alchemy's exported makeTunnelClient, using the same token policy id and permissions (Cloudflare Tunnel Read and Write). The Worker's scoped token doesn't change.
  • The delete is now wrapped in Effect.suspend, so the retry after disconnecting is always a fresh request.

Evidence

  • Prod trace for the failing passes: DNS deleted, then DeleteTunnelCloudflared → 400 "has active connections". Passes for users whose host was offline succeed.
  • A new provider test: an unforced deprovision of a tunnel with a live connector still fails at delete-tunnel without disconnecting. A forced one disconnects, deletes and removes the allocation. Disabling the forced branch makes the test fail.
  • The relay suite passes (324 tests) and check:changed passes.

After deploy, the 4 stuck deletions should finish on the next sweep and the monitor should resolve.

🤖 Generated with Claude Code (Claude Opus 5.5)

    1. tanstack-source added labelsbug

    2. Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      Managed endpoint deprovisioning now supports an optional forced disconnect when active connections prevent tunnel deletion. The account deletion flow enables this option during environment unlinking. The worker uses a managed tunnel binding that provides the disconnect operation.

      Changes: Managed endpoint disconnect flow

      Layer / File(s)Summary
      Managed tunnel binding and worker wiring
      infra/relay/src/environments/ManagedTunnelBinding.ts, infra/relay/src/worker.ts
      The managed tunnel binding adds disconnect, which updates the tunnel secret and deletes its connection. The worker uses this binding for managed endpoint tunnel access.
      Conditional disconnect and tunnel deletion
      infra/relay/src/environments/ManagedEndpointProvider.ts, infra/relay/src/environments/ManagedEndpointProvider.test.ts, infra/relay/src/environments/ManagedEndpointReaper.test.ts
      When forceDisconnect is true and tunnel deletion fails because connections are active, the provider disconnects the tunnel and retries deletion. Tests cover forced and ordinary deprovisioning.
      Force-disconnect option propagation
      infra/relay/src/http/Api.ts, infra/relay/src/accounts/AccountDeletion.ts
      Environment unlinking accepts and forwards forceDisconnect. Account deletion sets the option when unlinking environments.

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

      Change: Bug fix

      Sequence Diagram(s)

      sequenceDiagram
        participant AccountDeletion
        participant unlinkEnvironmentRecord
        participant ManagedEndpointProvider
        participant ManagedTunnelClient
        participant CloudflareTunnelAPI
        AccountDeletion->>unlinkEnvironmentRecord: unlink environment with forceDisconnect
        unlinkEnvironmentRecord->>ManagedEndpointProvider: deprovision with forceDisconnect
        ManagedEndpointProvider->>ManagedTunnelClient: disconnect tunnel after active-connection deletion failure
        ManagedTunnelClient->>CloudflareTunnelAPI: update tunnel secret and delete connection
        ManagedEndpointProvider->>ManagedTunnelClient: retry tunnel deletion
      Merge Risk: Minimal · up to 6a3b2

      Account deletion now force-disconnects a host's tunnel connectors when Cloudflare refuses to delete a tunnel with active connections, then retries the deletion. No merge-blocking risk was found. The fix should be confirmed after deployment by checking that the four stuck deletions complete.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title names the main change: account deletion now force-disconnects a live host's tunnel connectors, which the raw summary and objectives both confirm. It also follows the conventional commit form…
      Description checkPassedThe description covers the template sections. Problem: Clerk deletions fail with 'This tunnel has active connections' while a revoked host keeps its connector running. Change: forceDisconnect is added…
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Title check

      Explanation

      The title names the main change: account deletion now force-disconnects a live host's tunnel connectors, which the raw summary and objectives both confirm. It also follows the conventional commit form with a fix(relay) scope. It describes the bug fix in plain language and does not mention files or vague terms.

      Full details: Description check

      Explanation

      The description covers the template sections. Problem: Clerk deletions fail with 'This tunnel has active connections' while a revoked host keeps its connector running. Change: forceDisconnect is added for AccountDeletion, with token rotation and a new ManagedTunnelBinding. Scope: it describes a bug fix tied to the account-deletion monitor (#42285), but it does not link a bug issue or explain why no issue exists. Verification: it lists a new provider test, a mutation check, 324 passing relay tests, and a passing check:changed, and it notes that the production outcome is expected but not yet observed. The description also says the relay suite passes, but the template asks for focused checks, which the author gives. The main gap is the missing issue link for the Scope section.

      Resolution

      Optionally link the issue or monitor alert that shows the bug, or state why no issue is needed. The template asks for this in the Scope section. Also consider noting the production result once it is verified.

      Finishing Touches

      Generate unit tests (beta)

      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between 55085df2a6c384f34b37901d5dcbc5925ada1600 and 6a3b2810508e9852e1d877290259f5aa8eb3ba49.

      Files selected for processing: 7

      • infra/relay/src/accounts/AccountDeletion.ts
      • infra/relay/src/environments/ManagedEndpointProvider.test.ts
      • infra/relay/src/environments/ManagedEndpointProvider.ts
      • infra/relay/src/environments/ManagedEndpointReaper.test.ts
      • infra/relay/src/environments/ManagedTunnelBinding.ts
      • infra/relay/src/http/Api.ts
      • infra/relay/src/worker.ts

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

    3. juliusmarminge merged thisc9ba6b4

    4. juliusmarminge closed this

Sign in to join this conversation.