GeezerVison is showing github.com — fix(connect): reclaim idle managed tunnels and recover them when the host returns by rynfar · Pull Request #937 · pylon-code/pylon
Conversation
rynfar
commented
Sep 30, 2026
•
edited by blacksmith-sh
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Problem
Cloudflare bills a managed tunnel whether or not a connector is attached, so a linked host that sleeps or is switched off leaves a paid tunnel behind indefinitely. Pylon Connect had no way to reclaim one without the user deregistering the environment, and no way for a host to get a replacement if its tunnel disappeared.
Fix
Ports the upstream tunnel reclaim-and-recover design onto Pylon's relay and server (#865).
- Relay reaper. The relay's maintenance job can delete a same-stage tunnel that Cloudflare reports
down for at least five minutes, or one that never connected and is at least an hour old. It is controlled by RELAY_TUNNEL_CLEANUP_MODE (off | dry-run | enabled) and defaults to off.
- Recovery. A host registers recovery at startup with a short-lived proof signed by its environment key. If its connector exits or
cloudflared reports repeated rejections, it asks the relay for a replacement, at most once every two minutes. The replacement is provisioned under the same allocation, so the hostname and DNS record survive and clients keep their bindings. No re-pairing.
- Sweep counters. The sweep span carries the reaper's counters and cron spans now reach the relay's existing trace dataset, so a dry-run can be read without tailing Worker logs.
Why a reconnecting host is not torn down
- Only allocations whose host registered recovery are candidates. Older server builds never register, are counted as
skippedLegacy, and are never deleted.
- Deletion is bound to the owning allocation row (
userId + environmentId + recorded tunnel ID). A tunnel with no allocation row is counted as skippedOrphan and left alone; an allocation with a missing or different tunnel ID is skipped.
- Immediately before the delete, the relay re-reads the tunnel from Cloudflare and aborts unless it is still in the expected status and still inactive since before the cutoff. A host that reconnected between the list and the delete keeps its tunnel.
- Every mutation bumps the allocation's
generation; the delete claims the row at the generation it loaded and re-claims inside the row lock, so a provision racing the sweep wins.
- Recovery endpoints require the environment credential, a matching environment ID, the linked environment public key, and a proof bound to the cloud user, action, tunnel ID and loopback origin.
- Each sweep is bounded: ten list requests, 100 deletions, a two-minute deadline, early stop on a Cloudflare rate limit.
Sources
| Source |
Upstream PR |
Disposition |
8d7b5e998c72b4674326cbac7d23bafe9b222668 fix(connect): remove tunnels after hosts go offline |
pingdotgg/t3code#9386 |
Adapted (cherry-pick, 7 conflicts resolved by hand) |
d23eab13d9d91697c5d4e7a458213b3f6473bc96 fix(relay): export tunnel cleanup counters to Axiom |
pingdotgg/t3code#13528 |
Adopted (clean cherry-pick) |
a727d1d97690c9bb12cee5760e91cfd1aa7c017d test(relay): remove constant-restating database mode test |
pingdotgg/t3code#13932 |
Adopted; relayDatabaseMode and relay stage behavior unchanged |
Pylon adaptations
- Hourly cron kept. Upstream moved the reaper onto a five-minute cron. Pylon's relay cron is hourly so Neon's compute can suspend; that schedule is unchanged. The reaper touches Cloudflare only, and the database only when a sweep finds candidates. A down tunnel is therefore reclaimed within about an hour rather than five to ten minutes; docs say so.
- Rotation slot follows the sweep interval. Upstream derives its page/attempt rotation slot from the five-minute grace period. On an hourly schedule consecutive sweeps would be twelve slots apart, which returns to the same starting offset for common candidate counts (for example 150 or 200) and lets a block of failing deletes starve everything after it. Added
MANAGED_ENDPOINT_SWEEP_INTERVAL_MINUTES = 60 and derive the slot from it; the rotation tests now advance by one sweep interval and the starvation test fails without the change.
- Server. Kept the awareness-relay catch-up after relay config is stored, the desktop update restart-marker cleanup at startup, the exported
isAllowedEndpointOrigin with its interface matching, and the loopback test server for the new tests. Dropped a duplicate httpClient test option that Pylon had since added.
- Branding. New log lines, the unsupported-runtime error, and the docs say Pylon Connect. Compatibility identifiers are untouched.
- Docs. Rollout and canary runbook added to
docs/operations/release.md with Pylon wording and hourly timing; upstream's marketing-site section is not carried. infra/relay/README.md lists the new optional variable.
- Migration name. Kept
20260919015455_managed_endpoint_recovery. Pylon's relay migration directory is identical to upstream's (same eight names and snapshot chain; the new snapshot's prevIds is Pylon's current head snapshot 4b6d0d21…), so this is the next migration in Pylon's own chain.
Exclusions
- No upstream dataset names, tokens or accounts. The counter export uses the
RelayTracesDataset and worker ingest token that Pylon's stack already provisions; infra/relay/src/observability.ts is unchanged.
- Cleanup is not turned on. No repository variable is set by this PR.
What the automatic relay deploy changes
Merging this deploys the prod relay stage.
- Migration (additive):
relay_managed_endpoint_allocations gains nullable recovery_enabled_at, recovery_environment_public_key, origin, and generation integer NOT NULL DEFAULT 0. The default is a constant, so Postgres does not rewrite the table; nothing is dropped or renamed. One new row in relay_migrations.
- Cron: schedule stays
0 * * * *. The handler now also runs the reaper, which returns immediately while the mode is off. Cron spans are exported to the existing trace dataset (roughly one small trace per hour).
- Bindings: none added or removed.
deploy-relay.yml passes one new optional variable, RELAY_TUNNEL_CLEANUP_MODE; unset or blank means off. It is read at deploy time, so changing it needs a relay deploy.
- HTTP: two new environment-credential routes,
POST /v1/environments/:environmentId/tunnel/recovery and POST /v1/environments/:environmentId/tunnel. Existing routes keep their contracts.
Operator setup required before enabling cleanup (not part of this PR): set the RELAY_TUNNEL_CLEANUP_MODE variable in the production GitHub environment to dry-run, deploy, read the counters, run the disposable-host canary in docs/operations/release.md, then set enabled. Server builds with this change must reach users first; hosts on older builds stay skippedLegacy.
Rollback:
- With cleanup
off (the state this PR ships), reverting the PR and redeploying is safe for the database: the new columns are nullable or defaulted and older relay code ignores them. Leave the columns and the migration row in place; do not edit relay_migrations by hand.
- Once server builds with this change are in use, keep the recovery routes deployed. Against a relay without them, a new server still starts its stored tunnel at boot (the 404 is a terminal registration failure and the stored config starts after the jitter of up to 30 seconds), but linking a managed environment from web or mobile returns an error and that tunnel does not start until the next server restart.
- If cleanup has been enabled, set it to
off and deploy the relay before downgrading any host. Deleted tunnels are not restored by a rollback; a current host re-provisions on its next start or wake.
Version skew
- Older server, new relay: unchanged behavior. It never registers recovery, so its tunnel is never a candidate.
- New server, older relay: covered above; boot falls back to the stored tunnel, web/mobile managed linking is unsupported until the relay has the recovery routes. Pylon deploys the relay on merge, before any server build ships.
- Web, desktop renderer and mobile clients: no contract they consume changed.
- Behavior change on the host: relay config is now persisted before the connector starts, so a link whose connector fails to start returns 503 with the config stored rather than leaving nothing stored.
Verification
Run on the rebased head.
vp test run in infra/relay (whole relay package): 31 files, 343 tests passed. Includes ManagedEndpointReaper.test.ts (18), ManagedEndpointProvider.test.ts, ManagedEndpointAllocations.test.ts, Api.test.ts, Config.test.ts.
vp test run src/server.test.ts src/cloud in apps/server: 15 files, 392 tests passed.
- Regression check for the hourly adaptation: with the upstream five-minute slot restored, "rotates the deletion order so persistent failures do not starve later tunnels" fails (1 failed, 17 passed); with the sweep-interval slot it passes.
- Typecheck, each confirmed to have run
tsc --noEmit and exit 0: t3code-relay, t3, @t3tools/contracts, @t3tools/shared.
vp fmt --check on the 34 changed source and doc files: clean. vp lint on the changed TypeScript files: 0 errors, 30 warnings, all in code this PR does not touch (server.ts:6 unused import, server.test.ts lines 11400+).
Limits
- No rendered-client pass; nothing was deployed and no Alchemy or Wrangler command was run against a real account.
- The reaper and recovery paths are covered by unit and route tests with Cloudflare represented at its client boundary. The disposable-host canary has not been run against a real Cloudflare account, which is why cleanup stays
off.
Model: Claude Opus 5.5. Harness: Claude Code in Pylon. Workflow: review-t3-upstream skill.
![Autofix with [code]smith](/api/image?u=https%3A%2F%2Fcamo.githubusercontent.com%2Fef26d88456de9cc93cf03b8611c53675c8af4fc8361b0049e62a133b46e853b6%2F68747470733a2f2f70722d636f6d6d656e74732d6173736574732e626c61636b736d6974682e73682f636f6465736d6974682f6175746f6669782d776974682d636f6465736d6974682d6461726b2e737667)
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Thread transfer impact
✅ Thread transfer remains within every enforced ceiling.
| Provider |
Metric |
Main baseline |
This PR |
Impact |
PR ceiling |
|
| Codex |
Total thread wire |
14.0 KiB |
14.0 KiB |
−1 B (−0.0%) |
15.1 KiB |
✅ |
| Codex |
Thread snapshot wire |
7.3 KiB |
7.3 KiB |
+9 B (+0.1%) |
7.3 KiB |
✅ |
| Codex |
Live turn WebSocket wire |
6.7 KiB |
6.7 KiB |
−10 B (−0.1%) |
7.8 KiB |
✅ |
| Codex |
Live turn WebSocket decoded |
58.0 KiB |
58.0 KiB |
0 B (0.0%) |
66.4 KiB |
✅ |
| Codex |
Live turn messages |
9 |
9 |
0 (0.0%) |
21 |
✅ |
| Claude |
Total thread wire |
14.0 KiB |
14.0 KiB |
−41 B (−0.3%) |
15.1 KiB |
✅ |
| Claude |
Thread snapshot wire |
7.3 KiB |
7.3 KiB |
−6 B (−0.1%) |
7.3 KiB |
✅ |
| Claude |
Live turn WebSocket wire |
6.7 KiB |
6.7 KiB |
−35 B (−0.5%) |
7.8 KiB |
✅ |
| Claude |
Live turn WebSocket decoded |
58.9 KiB |
58.8 KiB |
−44 B (−0.1%) |
66.4 KiB |
✅ |
| Claude |
Live turn messages |
9 |
8 |
−1 (−11.1%) |
21 |
✅ |
Baseline: 29895ce · PR result: 4411ceb · Source CI: success
Scenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 115.8 KiB
- Claude decoded thread snapshot: 116.5 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The relay can now reclaim a managed tunnel whose host stayed offline, and the
host asks for a replacement under the same hostname when it returns. Cleanup
ships off (RELAY_TUNNEL_CLEANUP_MODE) and only ever deletes tunnels whose host
registered recovery.
Pylon adaptations: the reaper rides Pylon's hourly relay cron instead of a
five-minute one, so its rotation slot follows the sweep interval; the server
keeps the awareness catch-up on relay config, the desktop restart-marker
cleanup, the loopback test server, and Pylon Connect wording.
Upstream: pingdotgg/t3code#9386
(cherry picked from commit 8d7b5e998c72b4674326cbac7d23bafe9b222668)
The sweep span now carries the reaper's counters, and cron spans reach the
relay's existing trace dataset like HTTP spans do, so a dry-run rollout can be
read without tailing Worker logs. No new binding or configuration.
Upstream: pingdotgg/t3code#13528
(cherry picked from commit d23eab13d9d91697c5d4e7a458213b3f6473bc96)
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|