rollbridge 0.1.66 → 0.1.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -171,6 +171,68 @@ test("a wedged non-blocking process cannot block release retirement", async () =
171
171
  expect(logs).toMatchObject([{message: "release non-blocking process still draining", data: {pid: 4242, processId: "worker"}}])
172
172
  })
173
173
 
174
+ test("an explicit stop bounds the processes it must not kill and reports what it left alive", async () => {
175
+ /** @type {Array<{message: string, data?: import("../src/json.js").JsonValue}>} */
176
+ const logs = []
177
+ /** @type {Array<{id: string, options: {retirementTimeoutMs?: number, timeoutMs?: number}}>} */
178
+ const stopOptions = []
179
+ const release = buildRelease({command: "run web", id: "web", policy: "proxied", port: {from: 0, to: 0}})
180
+
181
+ release.logger = (message, data) => logs.push({message, data})
182
+ release.nonBlockingDrainIds.add("worker")
183
+
184
+ /**
185
+ * @param {string} id - Process id.
186
+ * @param {boolean} unboundedWindow - Whether its graceful window can ever end.
187
+ * @returns {void} A process recorded in the generation.
188
+ */
189
+ const addProcess = (id, unboundedWindow) => {
190
+ const instance = {
191
+ hasUnboundedGracefulWindow: () => unboundedWindow,
192
+ retirementTimedOut: false,
193
+ status: () => ({pid: 4242, state: "stopped"}),
194
+ stop: async (/** @type {{retirementTimeoutMs?: number, timeoutMs?: number}} */ options) => {
195
+ stopOptions.push({id, options})
196
+ // Only the retirement branch sets this, so a process that was never budgeted never claims it.
197
+ if (options.retirementTimeoutMs !== undefined) instance.retirementTimedOut = true
198
+ }
199
+ }
200
+
201
+ // eslint-disable-next-line local/no-unknown-jsdoc-type
202
+ release.processes.set(id, /** @type {import("../src/managed-process.js").default} */ (/** @type {unknown} */ (instance)))
203
+ }
204
+
205
+ addProcess("worker", false)
206
+ addProcess("jobs-main", true)
207
+ addProcess("web", false)
208
+
209
+ await release.stop({retirementTimeoutMs: 40})
210
+
211
+ expect(stopOptions).toEqual([
212
+ {id: "worker", options: {retirementTimeoutMs: 40}},
213
+ {id: "jobs-main", options: {retirementTimeoutMs: 40}},
214
+ {id: "web", options: {}}
215
+ ])
216
+ expect(release.state).toBe("stopped")
217
+ expect(logs).toMatchObject([
218
+ {message: "release stop gave up waiting for a retiring process", data: {pid: 4242, processId: "worker", releaseId: "v1", timeoutMs: 40}},
219
+ {message: "release stop gave up waiting for a retiring process", data: {pid: 4242, processId: "jobs-main", releaseId: "v1", timeoutMs: 40}}
220
+ ])
221
+
222
+ // Shutdown has no budget to hand out and must still wait for the processes it owns, so an
223
+ // unbudgeted stop changes nothing.
224
+ logs.length = 0
225
+ stopOptions.length = 0
226
+ await release.stop()
227
+
228
+ expect(stopOptions).toEqual([
229
+ {id: "worker", options: {}},
230
+ {id: "jobs-main", options: {}},
231
+ {id: "web", options: {}}
232
+ ])
233
+ expect(logs).toHaveLength(0)
234
+ })
235
+
174
236
  test("restoring a stopped release does not re-adopt a stale connection count", async () => {
175
237
  const release = buildRelease({command: "run web", id: "web", policy: "proxied", port: {from: 0, to: 0}})
176
238
 
@@ -303,4 +365,138 @@ test("a generation still holding a process group after its tracked process exite
303
365
  fs.rmSync(procPath, {force: true, recursive: true})
304
366
  }
305
367
  })
368
+
369
+ /**
370
+ * @param {number} pid - Live pid the supervisor can still signal.
371
+ * @param {string[]} escalations - Collects the reason of every escalation delivered to this process.
372
+ * @returns {import("../src/managed-process.js").default} A process whose retirement wait already expired.
373
+ */
374
+ function wedgedProcess(pid, escalations) {
375
+ // eslint-disable-next-line local/no-unknown-jsdoc-type
376
+ return /** @type {import("../src/managed-process.js").default} */ (/** @type {unknown} */ ({
377
+ escalateRetirement: async (/** @type {{reason: string}} */ options) => {
378
+ escalations.push(options.reason)
379
+
380
+ return true
381
+ },
382
+ quiesceStrict: async () => {},
383
+ retirementTimedOut: true,
384
+ status: () => ({pid, retirementTimedOut: true, state: "quiesced"}),
385
+ stop: async () => {}
386
+ }))
387
+ }
388
+
389
+ test("the supervisor escalates a wedged generation whose residue holds no runner child", async () => {
390
+ const procPath = fs.mkdtempSync(path.join(os.tmpdir(), "rollbridge-escalate-wedged-"))
391
+
392
+ try {
393
+ // Group leader only, 5000ms of consumed CPU: nothing left in the group but the wedged worker.
394
+ writeProcMember(procPath, 101, 101, "S", 300, 200)
395
+
396
+ /** @type {Array<{data?: Record<string, import("../src/json.js").JsonValue>, message: string}>} */
397
+ const logs = []
398
+ /** @type {string[]} */
399
+ const escalations = []
400
+ const release = buildRelease({command: "run web", id: "web", policy: "proxied", port: {from: 0, to: 0}}, () => true, procPath)
401
+
402
+ release.logger = (message, data) => logs.push({data, message})
403
+ release.state = "stopped"
404
+ release.processes.set("worker", wedgedProcess(101, escalations))
405
+ // The generation already recorded `release drained`; this is the observation its retirement
406
+ // left behind, a full window behind the read that now acts on it.
407
+ release.drainNewestSample = {
408
+ atMs: Date.now() - 25000,
409
+ processes: [{cpuMs: 5000, heldProcessGroupPgid: undefined, id: "worker", liveChildCount: 0, pid: 101, retirementTimedOut: true}]
410
+ }
411
+
412
+ const escalated = await release.escalateWedgedGeneration({reason: "deploy of v2"})
413
+
414
+ expect(escalated).toEqual(["worker"])
415
+ expect(escalations).toHaveLength(1)
416
+ expect(escalations[0]).toContain("deploy of v2")
417
+ expect(escalations[0]).toContain("no runner child remaining")
418
+ expect(logs).toMatchObject([{data: {escalation: "SIGKILL", processIds: ["worker"], releaseId: "v1", trigger: "deploy of v2"}, message: "release wedged generation escalated"}])
419
+ } finally {
420
+ fs.rmSync(procPath, {force: true, recursive: true})
421
+ }
422
+ })
423
+
424
+ test("a wedged generation that still holds a live runner child is reported, never killed", async () => {
425
+ const procPath = fs.mkdtempSync(path.join(os.tmpdir(), "rollbridge-escalate-refused-"))
426
+
427
+ try {
428
+ writeProcMember(procPath, 101, 101, "S", 300, 200)
429
+ // A live runner child: the wedge is real, and killing it is still the STOE-1077 failure.
430
+ writeProcMember(procPath, 102, 101, "S", 0, 0)
431
+
432
+ /** @type {Array<{data?: Record<string, import("../src/json.js").JsonValue>, message: string}>} */
433
+ const logs = []
434
+ /** @type {string[]} */
435
+ const escalations = []
436
+ const release = buildRelease({command: "run web", id: "web", policy: "proxied", port: {from: 0, to: 0}}, () => true, procPath)
437
+
438
+ release.logger = (message, data) => logs.push({data, message})
439
+ release.state = "stopped"
440
+ release.processes.set("worker", wedgedProcess(101, escalations))
441
+ release.drainNewestSample = {
442
+ atMs: Date.now() - 25000,
443
+ processes: [{cpuMs: 5000, heldProcessGroupPgid: undefined, id: "worker", liveChildCount: 1, pid: 101, retirementTimedOut: true}]
444
+ }
445
+
446
+ const escalated = await release.escalateWedgedGeneration({reason: "deploy of v2"})
447
+
448
+ expect(escalated).toEqual([])
449
+ expect(escalations).toEqual([])
450
+ expect(logs).toMatchObject([{data: {escalation: "refused", pids: [101], processIds: ["worker"], releaseId: "v1"}, message: "release wedged generation left alive"}])
451
+ expect(logs[0].data?.reason).toBe("no escalation: 1 runner child is still live in the generation; SIGKILL would abort in-flight work")
452
+ } finally {
453
+ fs.rmSync(procPath, {force: true, recursive: true})
454
+ }
455
+ })
456
+
457
+ test("the retirement transition brackets its own wait and reports a residue it cannot yet prove wedged", async () => {
458
+ const procPath = fs.mkdtempSync(path.join(os.tmpdir(), "rollbridge-escalate-bracket-"))
459
+
460
+ try {
461
+ writeProcMember(procPath, 4242, 4242, "S", 300, 200)
462
+
463
+ /** @type {Array<{data?: Record<string, import("../src/json.js").JsonValue>, message: string}>} */
464
+ const logs = []
465
+ /** @type {string[]} */
466
+ const escalations = []
467
+ const release = buildRelease({command: "run web", id: "web", policy: "proxied", port: {from: 0, to: 0}}, () => true, procPath)
468
+
469
+ release.logger = (message, data) => logs.push({data, message})
470
+ release.state = "draining"
471
+ release.nonBlockingDrainIds.add("worker")
472
+ release.handoffServiceIds.add("jobs-main")
473
+ release.processes.set("worker", wedgedProcess(4242, escalations))
474
+ // eslint-disable-next-line local/no-unknown-jsdoc-type
475
+ release.processes.set("jobs-main", /** @type {import("../src/managed-process.js").default} */ (/** @type {unknown} */ ({
476
+ status: () => ({pid: undefined, retirementTimedOut: false, state: "stopped"}),
477
+ stop: async () => {}
478
+ })))
479
+
480
+ await release.drainAndStop(0, {
481
+ ...release.config,
482
+ proxy: {...release.config.proxy, nonBlockingDrainTimeoutMs: 20},
483
+ releaseRetention: {...release.config.releaseRetention, escalateWedgedGenerations: true}
484
+ })
485
+
486
+ // The transition still completes: the escalation is its last act, never a wait of its own.
487
+ expect(release.state).toBe("stopped")
488
+ expect(escalations).toEqual([])
489
+ expect(release.drainPriorSample).toBeDefined()
490
+ expect(release.drainNewestSample).toBeDefined()
491
+ expect(logs).toMatchObject([
492
+ {data: {pid: 4242, processId: "worker"}, message: "release non-blocking process still draining"},
493
+ {data: {escalation: "refused", pids: [4242], releaseId: "v1", trigger: "release retirement"}, message: "release wedged generation left alive"}
494
+ ])
495
+ // A bracket shorter than the flatness window cannot justify a kill; the retained pair is
496
+ // what lets the next deploy prove the residue without sampling on a timer.
497
+ expect(logs[1].data?.reason).toContain("is below the 20000ms minimum")
498
+ } finally {
499
+ fs.rmSync(procPath, {force: true, recursive: true})
500
+ }
501
+ })
306
502
  })
@@ -61,6 +61,61 @@ test("a nonBlockingDrain worker stops immediately while its release is still dra
61
61
  }
62
62
  })
63
63
 
64
+ test("an explicit stop returns within its retirement budget instead of waiting on a worker that is still draining", async () => {
65
+ const fixture = await createFixture({nonBlockingDrainWorker: true, workerStopDelayMs: 30000})
66
+ const config = normalizeConfig({
67
+ ...fixture.config,
68
+ processes: fixture.config.processes.map((processConfig) => processConfig.id === "worker"
69
+ ? {...processConfig, gracefulStopMs: "indefinite"}
70
+ : processConfig),
71
+ proxy: {...fixture.config.proxy, nonBlockingDrainTimeoutMs: 100}
72
+ })
73
+ /** @type {Array<{message: string, data?: import("../src/json.js").JsonValue}>} */
74
+ const logs = []
75
+ const daemon = new RollbridgeDaemon({config, logger: (message, data) => logs.push({message, data})})
76
+ /** @type {number | undefined} */
77
+ let workerPid
78
+
79
+ try {
80
+ await daemon.start()
81
+ await daemon.deploy({releaseId: "v1", releasePath: fixture.root, revision: "v1"})
82
+
83
+ workerPid = statusRelease(daemon, "v1").processes.find((processStatus) => processStatus.id === "worker")?.pid
84
+
85
+ if (typeof workerPid !== "number") throw new Error("Expected the worker to be running before the stop")
86
+
87
+ const startedAtMs = Date.now()
88
+
89
+ await daemon.stopRelease("v1")
90
+
91
+ // Before this the command had no deadline for a worker whose graceful window is "indefinite",
92
+ // so it never returned and held the owner-mutation fence for the whole drain, refusing every
93
+ // later deploy for hours (TensorBuzz production, 2026-11-16). The bound is far below the
94
+ // worker's own 30 000 ms stop delay because the stop must still let the generation's other
95
+ // processes exit; what it must not do is wait for the worker at all.
96
+ expect(Date.now() - startedAtMs).toBeLessThan(10000)
97
+
98
+ const stopped = statusRelease(daemon, "v1")
99
+ const stoppedWorker = stopped.processes.find((processStatus) => processStatus.id === "worker")
100
+
101
+ expect(stopped.state).toBe("stopped")
102
+ expect(stoppedWorker?.retirementTimedOut).toBe(true)
103
+ // The tracked record is the shell wrapper, which is already gone; the worker it launched is
104
+ // still draining inside the same process group, and that is the residue the stop must report.
105
+ expect(stoppedWorker?.heldProcessGroup?.liveMemberCount).toBeGreaterThan(0)
106
+ expect(logs.filter((entry) => entry.message === "release stop gave up waiting for a retiring process")).toMatchObject([
107
+ {data: {processId: "worker", releaseId: "v1", timeoutMs: 100}}
108
+ ])
109
+ expect(logs.filter((entry) => entry.message === "release stopped")).toMatchObject([{data: {releaseId: "v1"}}])
110
+ } finally {
111
+ await daemon.shutdown()
112
+ if (workerPid !== undefined) {
113
+ try { process.kill(-workerPid, "SIGKILL") } catch (_error) { /* The group already exited. */ }
114
+ }
115
+ await fs.rm(fixture.root, {force: true, recursive: true})
116
+ }
117
+ })
118
+
64
119
  test("deploy switches new HTTP traffic while old WebSockets drain", async () => {
65
120
  const fixture = await createFixture()
66
121
  const daemon = await startDaemon(fixture.config)
@@ -2064,6 +2119,51 @@ test("a draining-generation bound of 0 leaves concurrent drains unbounded", asyn
2064
2119
  }
2065
2120
  })
2066
2121
 
2122
+ test("a deploy is the event that asks a retiring generation for its escalation decision, and nothing else does", async () => {
2123
+ const config = normalizeConfig({
2124
+ application: "demo",
2125
+ control: {path: path.join(os.tmpdir(), "rollbridge-escalate-gate.sock")},
2126
+ processes: [{command: `${JSON.stringify(process.execPath)} -e "setInterval(() => {}, 1000)"`, id: "web", policy: "proxied", port: {from: 0, to: 0}}],
2127
+ proxy: {host: "127.0.0.1", port: 0}
2128
+ })
2129
+ const daemon = new RollbridgeDaemon({config, logger: () => {}})
2130
+ /** @type {Array<{reason: string, releaseId: string}>} */
2131
+ const decided = []
2132
+
2133
+ /**
2134
+ * @param {string} releaseId - Generation id.
2135
+ * @param {import("../src/release-group.js").ReleaseState} state - Recorded state of the generation.
2136
+ * @returns {import("../src/release-group.js").default} A generation that only records the decision request.
2137
+ */
2138
+ // eslint-disable-next-line local/no-unknown-jsdoc-type
2139
+ const retiringRelease = (releaseId, state) => /** @type {import("../src/release-group.js").default} */ (/** @type {unknown} */ ({
2140
+ escalateWedgedGeneration: async (/** @type {{reason: string}} */ options) => {
2141
+ decided.push({reason: options.reason, releaseId})
2142
+
2143
+ return []
2144
+ },
2145
+ releaseId,
2146
+ state
2147
+ }))
2148
+
2149
+ daemon.releases.set("draining", retiringRelease("draining", "draining"))
2150
+ daemon.releases.set("drained", retiringRelease("drained", "stopped"))
2151
+ daemon.releases.set("active", retiringRelease("active", "active"))
2152
+
2153
+ // Off by default: enabling the escalation is the operator's decision, not the daemon's.
2154
+ await daemon.escalateWedgedGenerations(config, "candidate")
2155
+
2156
+ expect(decided).toEqual([])
2157
+
2158
+ const escalating = normalizeConfig({...config, releaseRetention: {...config.releaseRetention, escalateWedgedGenerations: true}})
2159
+
2160
+ await daemon.escalateWedgedGenerations(escalating, "candidate")
2161
+
2162
+ // Every generation still holding a retirement — draining, drained with residue, or failed —
2163
+ // is asked; the active one and the candidate itself are not.
2164
+ expect(decided).toEqual([{reason: "deploy of candidate", releaseId: "draining"}, {reason: "deploy of candidate", releaseId: "drained"}])
2165
+ })
2166
+
2067
2167
  test("status reports the drain classification of each draining generation", async () => {
2068
2168
  const fixture = await createFixture()
2069
2169
  const daemon = await startDaemon(fixture.config)