@devries-cameron20 / t3code-1 / commits / 29724995e2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(relay): mark a tunnel released only after deletion succeeds (#22337)

Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Victor Vogel committed Oct 31, 2026 at 03:40 UTC 29724995e2adb1bf9f3d2c341d32b55c44aea189
2 files changed +49 -11
infra/relay/src/environments/ManagedEndpointProvider.test.ts
+41 -6
@@ -237,6 +237,7 @@ function makeAllocations(calls: AllocationCall[] = []) {
237 mutate(allocationKey(input), (allocation) => ({
238 ...allocation,
239 tunnelId: input.tunnelId,
240 + tunnelReleasedAt: null,
241 readyAt: allocation.tunnelId === input.tunnelId ? allocation.readyAt : null,
242 }));
243 return allocations.get(allocationKey(input))?.generation ?? null;
@@ -297,14 +298,24 @@ function makeAllocations(calls: AllocationCall[] = []) {
298 ) {
299 return null;
300 }
300 - mutate(allocationKey(input), (current) => current);
301 + mutate(allocationKey(input), (current) => ({
302 + ...current,
303 + ...(input.markReleased === true ? { tunnelReleasedAt: "2026-06-02T00:00:00.000Z" } : {}),
304 + }));
305 return allocations.get(allocationKey(input))?.generation ?? null;
306 }),
307 withClaimedTunnel: (input, effect) =>
308 Effect.suspend(() => {
309 const current = allocations.get(allocationKey(input));
310 return current?.tunnelId === input.tunnelId && current.generation === input.generation
307 - ? Effect.asSome(effect)
311 + ? effect.pipe(
312 + Effect.asSome,
313 + Effect.onError(() =>
314 + Effect.sync(() => {
315 + allocations.set(allocationKey(input), current);
316 + }),
317 + ),
318 + )
319 : Effect.succeedNone;
320 }),
321 claimDeprovision: (input) =>
@@ -961,6 +972,22 @@ describe("ManagedEndpointProvider", () => {
972 }).pipe(Effect.provide(layer));
973 });
974
975 + it.effect("marks the release on the claim that deletes the tunnel when asked", () => {
976 + const allocations = makeAllocations();
977 + const layer = layerProvider(makePersistentTunnelClient(), makeDnsClient(), allocations);
978 +
979 + return Effect.gen(function* () {
980 + const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
981 + const key = { userId: "user_ABC", environmentId: "env_ABC" } as const;
982 + yield* provider.provision({
983 + ...key,
984 + origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
985 + });
986 + expect(yield* provider.release({ ...key, markReleased: true })).toBe(true);
987 + expect((yield* allocations.get(key))?.tunnelReleasedAt).toBe("2026-06-02T00:00:00.000Z");
988 + }).pipe(Effect.provide(layer));
989 + });
990 +
991 it.effect("keeps the tunnel alive when a concurrent provision outdates the release claim", () => {
992 const tunnelCalls: TunnelCall[] = [];
993 const allocations = makeAllocations();
@@ -1296,7 +1323,8 @@ describe("ManagedEndpointProvider", () => {
1323 ),
1324 });
1325 const allocationCalls: AllocationCall[] = [];
1299 - const layer = layerProvider(tunnelClient, makeDnsClient(), makeAllocations(allocationCalls));
1326 + const allocations = makeAllocations(allocationCalls);
1327 + const layer = layerProvider(tunnelClient, makeDnsClient(), allocations);
1328
1329 return Effect.gen(function* () {
1330 const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
@@ -1306,6 +1334,9 @@ describe("ManagedEndpointProvider", () => {
1334 origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
1335 });
1336 const result = yield* Effect.result(provider.release({ ...key, markReleased: true }));
1337 + expect((yield* allocations.get(key))?.tunnelReleasedAt).toBe(
1338 + released ? "2026-06-02T00:00:00.000Z" : null,
1339 + );
1340
1341 if (released) {
1342 expect(result).toMatchObject({ _tag: "Success", success: true });
@@ -1320,8 +1351,9 @@ describe("ManagedEndpointProvider", () => {
1351 );
1352
1353 it.effect("reports a tunnel with an attached connector as not released", () => {
1354 + const persistent = makePersistentTunnelClient();
1355 const tunnelClient = ManagedEndpointProvider.ManagedEndpointTunnelClient.of({
1324 - ...makeTunnelClient(),
1356 + ...persistent,
1357 delete: (tunnelId) =>
1358 Effect.fail(
1359 new ManagedEndpointProvider.ManagedEndpointTunnelClientError({
@@ -1336,7 +1368,8 @@ describe("ManagedEndpointProvider", () => {
1368 }),
1369 ),
1370 });
1339 - const layer = layerProvider(tunnelClient, makeDnsClient(), makeAllocations());
1371 + const allocations = makeAllocations();
1372 + const layer = layerProvider(tunnelClient, makeDnsClient(), allocations);
1373
1374 return Effect.gen(function* () {
1375 const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
@@ -1345,7 +1378,9 @@ describe("ManagedEndpointProvider", () => {
1378 ...key,
1379 origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
1380 });
1348 - expect(yield* provider.release(key)).toBe(false);
1381 + expect(yield* provider.release({ ...key, markReleased: true })).toBe(false);
1382 + expect((yield* allocations.get(key))?.tunnelReleasedAt).toBeNull();
1383 + expect((yield* persistent.get("tunnel-id")).id).toBe("tunnel-id");
1384 }).pipe(Effect.provide(layer));
1385 });
1386
infra/relay/src/environments/ManagedEndpointProvider.ts
+8 -5
@@ -1127,19 +1127,22 @@ const make = Effect.gen(function* () {
1127 // A timed-out deletion may have succeeded; confirm Cloudflare state before rolling back the claim.
1128 return yield* deleteTunnel.pipe(
1129 Effect.as(true),
1130 - Effect.catchIf(
1131 - (error) => isManagedEndpointTunnelInUse(error.cause),
1132 - () => Effect.succeed(false),
1133 - ),
1130 Effect.catchTags({
1131 ManagedEndpointDeprovisioningFailed: (failure) =>
1136 - confirmTunnelGone(failure).pipe(Effect.as(true)),
1132 + isManagedEndpointTunnelInUse(failure.cause)
1133 + ? Effect.fail(failure)
1134 + : confirmTunnelGone(failure).pipe(Effect.as(true)),
1135 }),
1136 );
1137 }),
1138 )
1139 .pipe(
1140 Effect.catchTags({
1141 + // Return false only after the refused deletion rolls back the release marker.
1142 + ManagedEndpointDeprovisioningFailed: (failure) =>
1143 + isManagedEndpointTunnelInUse(failure.cause)
1144 + ? Effect.succeedSome(false)
1145 + : Effect.fail(failure),
1146 ManagedEndpointAllocationPersistenceError: (cause) =>
1147 Effect.fail(
1148 new ManagedEndpointDeprovisioningFailed({