Skip to content

Commit 794d5b8

Browse files
only21milclaude
andcommitted
fix(relay): write the link before the allocation bump
The relink upsert updated the allocation row and then wrote the link row. enableRecovery takes the same two locks the other way round: its EXISTS subquery locks the link row FOR UPDATE before its UPDATE locks the allocation row. A same-key relink concurrent with tunnel registration or recovery could deadlock, and Postgres would abort one request. The upsert now writes the link first and bumps the allocation generation second, in the same transaction. That is the link-then-allocation order enableRecovery uses. Neither write is visible before commit, so the listing, get and claim windows stay closed, and the bump still waits on the allocation row lock a claimed release holds while it deletes the tunnel. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fd1fe90 commit 794d5b8

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ describe("EnvironmentLinks", () => {
5050
// The reaper can list an allocation while its recovery key differs from the
5151
// link and release it later at that generation. A link that restores the key
5252
// must move the generation before it commits, or the release deletes the
53-
// tunnel the relink just provisioned.
54-
it.effect("moves the allocation generation before a link restores its recovery key", () => {
53+
// tunnel the relink just provisioned. The link write comes first, matching
54+
// enableRecovery's link-then-allocation lock order so the two cannot deadlock.
55+
it.effect("moves the allocation generation when a link restores its recovery key", () => {
5556
const events: Array<string> = [];
5657
const allocationUpdates: Array<{
5758
readonly set: Record<string, unknown>;
@@ -104,7 +105,7 @@ describe("EnvironmentLinks", () => {
104105
endpoint: { httpBaseUrl: "https://a.example", wsBaseUrl: "wss://a.example" } as never,
105106
});
106107

107-
expect(events).toEqual(["begin", "allocation", "link", "commit"]);
108+
expect(events).toEqual(["begin", "link", "allocation", "commit"]);
108109
const dialect = new PgDialect();
109110
const where = dialect.sqlToQuery(allocationUpdates[0]?.where as never);
110111
expect(where.sql).toContain('"recovery_environment_public_key" = $3');

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,12 @@ const make = Effect.gen(function* () {
204204
// The reaper may have listed this allocation while its recovery key did
205205
// not match the link, and then releases it without the recent
206206
// registration guard. A link proven by that key makes recovery usable
207-
// again, so it moves the generation the release was pinned to. The
208-
// update runs first to take the allocation row lock, which a claimed
209-
// release holds while it deletes the tunnel.
207+
// again, so it moves the generation the release was pinned to. Both
208+
// writes commit together, and the bump waits on the allocation row lock
209+
// a claimed release holds while it deletes the tunnel. The link is
210+
// written first to keep enableRecovery's order: its EXISTS locks the
211+
// link row before its UPDATE locks the allocation row.
210212
const writeLink = Effect.gen(function* () {
211-
yield* db
212-
.update(relayManagedEndpointAllocations)
213-
.set({ generation: sql`${relayManagedEndpointAllocations.generation} + 1` })
214-
.where(
215-
and(
216-
eq(relayManagedEndpointAllocations.userId, input.userId),
217-
eq(relayManagedEndpointAllocations.environmentId, environmentId),
218-
eq(
219-
relayManagedEndpointAllocations.recoveryEnvironmentPublicKey,
220-
proof.environmentPublicKey,
221-
),
222-
),
223-
);
224213
yield* db
225214
.insert(relayEnvironmentLinks)
226215
.values({
@@ -257,6 +246,19 @@ const make = Effect.gen(function* () {
257246
holdWebhooksWhileOffline: inheritedHoldWebhooks,
258247
},
259248
});
249+
yield* db
250+
.update(relayManagedEndpointAllocations)
251+
.set({ generation: sql`${relayManagedEndpointAllocations.generation} + 1` })
252+
.where(
253+
and(
254+
eq(relayManagedEndpointAllocations.userId, input.userId),
255+
eq(relayManagedEndpointAllocations.environmentId, environmentId),
256+
eq(
257+
relayManagedEndpointAllocations.recoveryEnvironmentPublicKey,
258+
proof.environmentPublicKey,
259+
),
260+
),
261+
);
260262
});
261263
yield* db.$client.withTransaction(writeLink).pipe(Effect.mapError(upsertError));
262264
}),

0 commit comments

Comments
 (0)