Public: anyone can read this; the crew can change it.

A replace waits for the old machine to let go of the volume before making the new one #16346

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

sold/replace-waits-for-volume → main 1 commit

  • Merged. sold/replace-waits-for-volume was merged into main as 77382e3.

Review

Sign in to review this change.
7 files changed +75 −5
src/cloud/droplet.ts +4
@@ -46,6 +46,10 @@ export const digitalocean: Driver = {
4646 async deleteVolume(call, volume) {
4747 await call("DELETE", `/volumes/${volume}`);
4848 },
49 async volumeFree(call, volume) {
50 const v = (await call("GET", `/volumes/${volume}`)).volume;
51 return Array.isArray(v?.droplet_ids) ? v.droplet_ids.length === 0 : true;
52 },
4953 async readMachine(call, remote) {
5054 const d = (await call("GET", `/droplets/${remote}`)).droplet;
5155 const v4: unknown[] = Array.isArray(d?.networks?.v4) ? d.networks.v4 : [];
src/cloud/hetzner.ts +4
@@ -49,6 +49,10 @@ export const hetzner: Driver = {
4949 async deleteVolume(call, volume) {
5050 await call("DELETE", `/volumes/${volume}`);
5151 },
52 async volumeFree(call, volume) {
53 const v = (await call("GET", `/volumes/${volume}`)).volume;
54 return v?.server === null || v?.server === undefined;
55 },
5256 async readMachine(call, remote) {
5357 const s = (await call("GET", `/servers/${remote}`)).server;
5458 return { status: String(s?.status ?? ""), address: ipv4(s?.public_net?.ipv4?.ip) };
src/cloud/machine.ts +19
@@ -157,6 +157,8 @@ export interface Driver {
157157 deleteMachine(call: Call, remote: string): Promise<void>;
158158 /** Delete the data volume: nothing of the forge's data is left once this answers. */
159159 deleteVolume(call: Call, volume: string): Promise<void>;
160 /** Whether the volume is attached to no machine: a cloud detaches it after a delete answers, and refuses a new machine with it until then. */
161 volumeFree(call: Call, volume: string): Promise<boolean>;
160162 readMachine(call: Call, remote: string): Promise<{ status: string; address: string }>;
161163 }
162164
@@ -217,10 +219,27 @@ export async function replace(
217219 if (!(e instanceof Error && e.message.endsWith("answered 404"))) throw e;
218220 }
219221 hooks.onDeleted();
222 await untilFree(d, call, p.volume);
220223 }
221224 return { remote: await d.createMachine(call, p, p.volume) };
222225 }
223226
227 /** How long a replace waits for the old machine to let go of the volume, and how often it asks. */
228 export const DETACH = { waitMs: 180_000, everyMs: 5_000 };
229
230 /**
231 * Wait until the cloud says the volume is attached to nothing. DigitalOcean answered 422 to a
232 * droplet made with a volume it was still detaching from the deleted one (test1, 2026-11-24), which
233 * left a sold forge with no machine until someone asked again (#16342).
234 */
235 async function untilFree(d: Driver, call: Call, volume: string): Promise<void> {
236 const until = Date.now() + DETACH.waitMs;
237 while (!(await d.volumeFree(call, volume))) {
238 if (Date.now() >= until) throw new Error("the old machine still holds the data volume; nothing new was made: ask again in a minute");
239 await Bun.sleep(DETACH.everyMs);
240 }
241 }
242
224243 /**
225244 * Ask whether the row's place may be ordered, and make nothing: the driver's `admit` alone, which
226245 * is where a place that cannot be ordered, or a ceiling (`ceiling.ts`), refuses. Asked before a
test/pinned-claim.test.ts +1 −1
@@ -435,9 +435,9 @@ describe("a deployed forge is claimed with the link its deployer was shown, and
435435 expect(calls.slice(0, 5)).toEqual([
436436 "GET /v2/droplets/" + first.value.remote,
437437 "DELETE /v2/droplets/" + first.value.remote,
438 "GET /v2/volumes/vol-1",
438439 "POST /v2/account/keys",
439440 "POST /v2/droplets",
440 "DELETE /v2/account/keys/4242",
441441 ]);
442442 const next = LINK.exec(again.value.claim.link)![2]!;
443443 expect(next).not.toBe(old);
test/pinned-detach.test.ts added +42
@@ -0,0 +1,42 @@
1 import { expect, test } from "bun:test";
2 import { DETACH, replace, type Driver } from "../src/cloud/machine.ts";
3
4 // test1 on DigitalOcean, 2026-11-24: a replace deleted the droplet and made the new one with the
5 // volume while the cloud was still detaching it; the create answered 422 and the forge had no
6 // machine until someone asked again (#16342). A replace now waits for the volume to be free.
7 function cloud(heldFor: number) {
8 const seen: string[] = [];
9 let asks = 0;
10 const d: Driver = {
11 api: "https://api.example.test",
12 async createVolume() { return "vol"; },
13 async createMachine() { seen.push("create"); return "2"; },
14 async deleteMachine() { seen.push("delete"); },
15 async deleteVolume() {},
16 async volumeFree() { asks += 1; seen.push("free?"); return asks > heldFor; },
17 async readMachine() { return { status: "active", address: "" }; },
18 } as unknown as Driver;
19 return { d, seen };
20 }
21 const p = { remote: "1", volume: "vol", name: "acme" } as any;
22
23 test("a replace waits until the old machine lets go of the volume, then makes one machine", async () => {
24 const keep = { ...DETACH };
25 DETACH.everyMs = 1;
26 try {
27 const { d, seen } = cloud(2);
28 const r = await replace(d, fetch, "t", p, { onDeleted() {} });
29 expect(r.remote).toBe("2");
30 expect(seen).toEqual(["delete", "free?", "free?", "free?", "create"]);
31 } finally { Object.assign(DETACH, keep); }
32 });
33
34 test("a volume that is never let go makes nothing and says so", async () => {
35 const keep = { ...DETACH };
36 DETACH.everyMs = 1; DETACH.waitMs = 20;
37 try {
38 const { d, seen } = cloud(Number.POSITIVE_INFINITY);
39 await expect(replace(d, fetch, "t", p, { onDeleted() {} })).rejects.toThrow("still holds the data volume");
40 expect(seen).not.toContain("create");
41 } finally { Object.assign(DETACH, keep); }
42 });
test/pinned-forge.test.ts +3 −3
@@ -138,10 +138,10 @@ describe("pinned forge", () => {
138138 expect(calls.map((c) => `${c.method} ${new URL(c.url).pathname}`)).toEqual([
139139 // The machine's address is read first, to ask the forge there whether it was claimed.
140140 expect.stringMatching(/^GET \/v2\/droplets\/\d+$/),
141 expect.stringMatching(/^DELETE \/v2\/droplets\/\d+$/), "POST /v2/account/keys", "POST /v2/droplets", "DELETE /v2/account/keys/4242",
141 expect.stringMatching(/^DELETE \/v2\/droplets\/\d+$/), "GET /v2/volumes/vol-1", "POST /v2/account/keys", "POST /v2/droplets", "DELETE /v2/account/keys/4242",
142142 ]);
143 expect(calls[3]!.body.volumes).toEqual(["vol-1"]);
144 expect(calls[3]!.body.user_data).toContain(`checkout --detach ${V2}`);
143 expect(calls[4]!.body.volumes).toEqual(["vol-1"]);
144 expect(calls[4]!.body.user_data).toContain(`checkout --detach ${V2}`);
145145 const [latest] = changesOf(app.db, getPinned(app.db, "acme")!.id);
146146 expect(latest).toMatchObject({ kind: "update", fromVersion: V1, toVersion: V2, by: admin().id });
147147 expect(Date.parse(latest!.at)).toBeGreaterThan(0);
test/pinned-hetzner.test.ts +2 −1
@@ -59,6 +59,7 @@ describe("hetzner target", () => {
5959 calls.push({ method, path: u.pathname, auth: new Headers(init?.headers).get("authorization"), body: init?.body ? JSON.parse(String(init.body)) : undefined });
6060 const json = (o: unknown, status = 200) => new Response(JSON.stringify(o), { status });
6161 if (u.pathname === "/v1/volumes") return json({ volume: { id: 77 } }, 201);
62 if (u.pathname === "/v1/volumes/77" && method === "GET") return json({ volume: { id: 77, server: null } });
6263 if (u.pathname === "/v1/ssh_keys" && method === "POST") return json({ ssh_key: { id: 4242 } }, 201);
6364 if (u.pathname === "/v1/servers" && method === "POST") {
6465 if (failServer > 0) { failServer--; return json({}, 500); }
@@ -104,7 +105,7 @@ describe("hetzner target", () => {
104105 expect(r.ok).toBe(true);
105106 // The address is read to ask the forge whether it was claimed; whether the recorded place may
106107 // be ordered is answered from the catalog, with no call; then the server is replaced.
107 expect(seen()).toEqual(["GET /v1/servers/501", "DELETE /v1/servers/501", "POST /v1/ssh_keys", "POST /v1/servers", "DELETE /v1/ssh_keys/4242"]);
108 expect(seen()).toEqual(["GET /v1/servers/501", "DELETE /v1/servers/501", "GET /v1/volumes/77", "POST /v1/ssh_keys", "POST /v1/servers", "DELETE /v1/ssh_keys/4242"]);
108109 const made = calls.find((c) => c.method === "POST" && c.path === "/v1/servers")!;
109110 expect(made.body).toMatchObject({ server_type: "cx23", location: "nbg1" });
110111 expect(made.body.volumes).toEqual([77]);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs