Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(http): keep a finished chunked body priced until it is released
Yudhi Armyndharisc7de9c35dd30
diff --git a/src/config/inflight-body-budget.spec.ts b/src/config/inflight-body-budget.spec.tsindex 1e5a5d288..0c39a37ef 100644--- a/src/config/inflight-body-budget.spec.ts+++ b/src/config/inflight-body-budget.spec.ts@@ -690,12 +690,30 @@ describe('stall reaper', () => {const req = makeReq({ 'transfer-encoding': 'chunked' });const { res } = run(budget, req);- // A finished chunked body is priced at what arrived, not the opening placeholder.+ // A finished chunked body keeps at least its opening placeholder until the request is released.(req as unknown as { socket: { bytesRead: number } }).socket.bytesRead = 300;req.emit('end');jest.advanceTimersByTime(120_000);expect(destroyMock(req)).not.toHaveBeenCalled();- expect(budget.currentBytes()).toBe(300);+ expect(budget.currentBytes()).toBe(1000);++ res.emit('finish');+ expect(budget.currentBytes()).toBe(0);+ });++ it('keeps pricing a chunked body that arrived with its headers while the handler still holds it', () => {+ const budget = createInflightBodyBudget(1000, { perClientShare: 1 });+ const req = makeReq({ 'transfer-encoding': 'chunked' });+ // The middleware runs inside the parse of the read that carried the headers, so the socket counter+ // already includes a body sent in the same write: nothing more "arrives" after admission.+ (req as unknown as { socket: { bytesRead: number } }).socket.bytesRead = 600;+ const { res } = run(budget, req);++ req.emit('end');+ expect(budget.currentBytes()).toBe(1000);+ (req as unknown as { complete: boolean }).complete = true;+ jest.advanceTimersByTime(10_000);+ expect(budget.currentBytes()).toBe(1000);res.emit('finish');expect(budget.currentBytes()).toBe(0);diff --git a/src/config/inflight-body-budget.ts b/src/config/inflight-body-budget.tsindex 121992238..e7bd175ca 100644--- a/src/config/inflight-body-budget.ts+++ b/src/config/inflight-body-budget.ts@@ -190,9 +190,9 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflight// probes) never touch the map, so a full map cannot refuse them.const clientInFlight = new Map<string, number>();const MAX_TRACKED_CLIENTS = 10_000;- // Opening reservation for a body with no declared length (chunked). It is only a placeholder:- // the poller below reconciles it against the bytes that actually arrive, so a small chunked- // request ends up costing what it really weighs. Reserving a whole per-request cap up front+ // Opening reservation for a body with no declared length (chunked). It is only a floor: the+ // poller below raises it to the bytes that actually arrive, so a small chunked request costs this+ // placeholder rather than a whole per-request slot. Reserving a whole per-request cap up front// instead would make the budget a concurrency limit of DEFAULT_BUDGET_MULTIPLIER for chunked// senders — four 6-byte uploads would refuse every further body-carrying request.const undeclaredReservation = Math.max(1, Math.min(UNDECLARED_OPENING_RESERVATION_BYTES, budgetBytes));@@ -358,16 +358,17 @@ export function createInflightBodyBudget(budgetBytes: number, options?: Inflightlet lastProgress = admittedAt;// A declared body keeps its declared size until it is released. A chunked body is re-priced at- // the bytes that have arrived, never below its opening placeholder until it is complete: budget- // handed back mid-stream would be taken back, unchecked, by a body that then completes between- // polls. Growth that crosses the aggregate, the client share or the anonymous pool aborts the- // request mid-stream, the same bound a declared length gets at admission. A complete body is- // re-priced at its real size but never aborted: it is already buffered. reserved is updated- // BEFORE release() so the exactly-once decrement subtracts the reconciled size.+ // the bytes that have arrived, never below its opening placeholder: budget handed back mid-stream+ // would be taken back, unchecked, by a body that then completes between polls. The floor holds+ // after completion too, because bytes that arrived in the same read as the headers were already+ // counted in startBytes, so a small body sent with its headers measures as 0 while the handler+ // still holds it. Growth that crosses the aggregate, the client share or the anonymous pool+ // aborts the request mid-stream, the same bound a declared length gets at admission. A complete+ // body is never aborted: it is already buffered. reserved is updated BEFORE release() so the+ // exactly-once decrement subtracts the reconciled size.const reconcile = (complete: boolean): void => {if (released || declared !== undefined) return;- const floor = complete ? 0 : undeclaredReservation;- const actual = Math.min(Math.max(floor, socket.bytesRead - startBytes), ceiling);+ const actual = Math.min(Math.max(undeclaredReservation, socket.bytesRead - startBytes), ceiling);const delta = actual - reserved;if (delta === 0) return;inFlightBytes += delta;