Skip to content

Commit e8ab0a7

Browse files
fix(relay): a deleted tunnel whose allocation removal failed stops counting
Deprovision deletes the tunnel and then removes the allocation inside one transaction. If the removal or the commit fails, the rollback keeps the deleted tunnel's id, so a later stalled relink makes the tunnel limit count a tunnel that no longer exists. After a confirmed delete, a failed deprovision now marks the row released. That keeps tunnel reuse intact, and a tunnel that still exists keeps counting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3e10e2e commit e8ab0a7

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎infra/relay/src/environments/ManagedEndpointProvider.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,46 @@ describe("ManagedEndpointProvider", () => {
14061406
}).pipe(Effect.provide(layer));
14071407
});
14081408

1409+
it.effect("marks the tunnel released when removing the allocation fails after deletion", () => {
1410+
const allocationCalls: AllocationCall[] = [];
1411+
const allocations = makeAllocations(allocationCalls);
1412+
const layer = layerProvider(makePersistentTunnelClient(), makeDnsClient(), {
1413+
...allocations,
1414+
removeClaimed: (input) =>
1415+
Effect.suspend(() => {
1416+
allocationCalls.push({ operation: "removeClaimed", input });
1417+
return Effect.fail(
1418+
new ManagedEndpointAllocations.ManagedEndpointAllocationPersistenceError({
1419+
operation: "remove-claimed",
1420+
stage: "database-request",
1421+
userId: input.userId,
1422+
environmentId: input.environmentId,
1423+
}),
1424+
);
1425+
}),
1426+
});
1427+
1428+
return Effect.gen(function* () {
1429+
const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
1430+
const key = { userId: "user_ABC", environmentId: "env_ABC" } as const;
1431+
yield* provider.provision({
1432+
...key,
1433+
origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
1434+
});
1435+
const result = yield* Effect.result(provider.deprovision(key));
1436+
1437+
expect(result._tag).toBe("Failure");
1438+
if (result._tag === "Failure") {
1439+
expect(result.failure).toMatchObject({ stage: "remove-allocation" });
1440+
}
1441+
// The tunnel is gone; without the mark the tunnel limit would keep counting it.
1442+
expect(allocationCalls.at(-1)).toMatchObject({
1443+
operation: "claimRelease",
1444+
input: { tunnelId: "tunnel-id", markReleased: true },
1445+
});
1446+
}).pipe(Effect.provide(layer));
1447+
});
1448+
14091449
it.effect("treats already deleted remote resources as successfully deprovisioned", () => {
14101450
const allocationCalls: AllocationCall[] = [];
14111451
const notFound = { _tag: "NotFound" } as const;

‎infra/relay/src/environments/ManagedEndpointProvider.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ const make = Effect.gen(function* () {
882882
return false;
883883
}
884884
const tunnelId = allocation.tunnelId;
885+
let tunnelDeleted = false;
885886
const deprovision = Effect.gen(function* () {
886887
const dnsRecordId = allocation.dnsRecordId;
887888
if (dnsRecordId !== null) {
@@ -909,6 +910,7 @@ const make = Effect.gen(function* () {
909910
}),
910911
),
911912
);
913+
tunnelDeleted = true;
912914
}
913915
return yield* allocations
914916
.removeClaimed({
@@ -956,6 +958,20 @@ const make = Effect.gen(function* () {
956958
}),
957959
),
958960
}),
961+
// The rollback keeps the deleted tunnel's id; mark it released so the tunnel limit stops counting it.
962+
Effect.tapError(() =>
963+
tunnelDeleted
964+
? allocations
965+
.claimRelease({
966+
userId: input.userId,
967+
environmentId: input.environmentId,
968+
tunnelId,
969+
generation: claimedGeneration,
970+
markReleased: true,
971+
})
972+
.pipe(Effect.ignore)
973+
: Effect.void,
974+
),
959975
);
960976
return Option.getOrElse(removed, () => false);
961977
}),

‎infra/relay/src/environments/ManagedTunnelLimits.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ describe("ManagedTunnelLimits", () => {
242242
allocation: { updatedAt: 50 },
243243
link: { revoked: true, updatedAt: 48 },
244244
},
245+
// Tunnel deleted but the row survived; deprovision marked it released, then a relink stalled.
246+
{
247+
environmentId: "released-after-failed-removal-then-reserved",
248+
counted: false,
249+
allocation: { tunnelId: "t", releasedAt: 20, updatedAt: 30 },
250+
link: { revoked: true, updatedAt: 15 },
251+
},
245252
{
246253
environmentId: "withdrawn-at-provisioning-write",
247254
counted: false,

0 commit comments

Comments
 (0)