fix(resilience): drain heavyweight SSE on SIGTERM (#11020)

Validated on the combined batch board + this branch alone: chat-body-admission + authz/pipeline 65/65, file-size gate green with a dated frozen entry (chatBodyAdmission 1005→1009 — the +4 lease/drain wiring lines, owner-authorized rebaseline). trackRequest was never called, so SIGTERM waitForDrain saw zero in-flight and killed live SSE; leases now hold the drain counter for the stream's lifetime, and the 503 carries Retry-After. Closes #11015. Thank you @RaviTharuma!
This commit is contained in:
Ravi Tharuma
2026-08-23 03:51:02 +02:00
committed by GitHub
parent e73ab0040c
commit 230017196c
7 changed files with 65 additions and 3 deletions

View File

@@ -18,6 +18,7 @@
import { CORS_HEADERS } from "../utils/cors";
import { createHmac } from "crypto";
import v8 from "node:v8";
import { trackRequest } from "../../lib/gracefulShutdown";
function parsePositiveInt(value: string | undefined, fallback: number): number {
const parsed = Number.parseInt(String(value), 10);
@@ -229,6 +230,7 @@ export class ChatAdmissionController {
tryAcquireHealthyHeadroom(): ChatAdmissionLease | null {
if (this.#activeHealthy >= this.healthyHeadroom) return null;
this.#activeHealthy += 1;
const done = trackRequest();
let released = false;
return {
get released() {
@@ -238,6 +240,7 @@ export class ChatAdmissionController {
if (released) return;
released = true;
this.#activeHealthy = Math.max(0, this.#activeHealthy - 1);
done();
},
};
}
@@ -264,6 +267,7 @@ export class ChatAdmissionController {
tryAcquireHeavy(): ChatAdmissionLease | null {
if (this.#activeHeavy >= this.maxHeavyInFlight) return null;
this.#activeHeavy += 1;
const done = trackRequest();
let released = false;
return {
get released() {
@@ -273,6 +277,7 @@ export class ChatAdmissionController {
if (released) return;
released = true;
this.#activeHeavy = Math.max(0, this.#activeHeavy - 1);
done();
this.#dispatchFair();
},
};