Repository navigation
fix: reserve chat admission before body parsing - #7853
diegosouzapw merged 4 commits into
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Code Review
This pull request replaces the heap-pressure-aware admission guard for POST /v1/chat/completions with an atomic, process-local heavyweight admission lease system that limits concurrent heavyweight requests and enforces hard byte bounds before parsing. It also updates the early stream keepalive utility to cancel the upstream response when a client aborts. The review feedback correctly identifies two critical issues: a potential lease leak if handleChat rejects during streaming, and a runtime TypeError in the keepalive utility if result.response.body is null when attempting to cancel the stream.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
Addressed both review findings in |
83cb305 to
82c08bd
Compare
|
Thanks for tackling #7846 — the direction here is exactly right: atomic reserve-before-parse, actual-byte ingestion that doesn't trust I did find one gap that I'd consider blocking before merge, and it's the same one In const handlerResponse = handleChat(request, null, parsedBody, reqId).then(finishAdmission);
const streamedResponse = await withEarlyStreamKeepalive(handlerResponse, {...});
I reproduced this directly against your Suggested fix: const handlerResponse = handleChat(request, null, parsedBody, reqId).then(finishAdmission, (error) => {
admission.lease?.release();
throw error;
});plus a regression test pinning "handler rejects on the slow keepalive path releases the lease" — happy to see that land alongside the existing Everything else here (the atomic reservation itself, the actual-byte hard cap, the (Separately, worth a quick look before/at merge time: the default |
|
Follow-up after reconciling this against #7862 — both PRs restructure the same block of Suggested land order for the cluster: #7852 → #7855 → #7862 → this PR last, rebased on top of #7862's tree. Why this order: #7862 is a tiny, self-contained change whose regression test pins the parse-once contract, and this PR is the functional superset once reconciled. When you rebase, please also drop the To be clear about severity: your current clone operates on an in-memory buffered body, so it does not resurrect the #4380/#7847 heap-amplification issue — it's just redundant once #7862 lands, and resolving it during your rebase avoids a needless conflict. And thanks for the fast turnaround on the release-on-failure path ( |
# Conflicts: # src/app/api/v1/chat/completions/route.ts
Post-#7862 rebase: the admission-rebuilt request is json()-parsed directly over the already-buffered bytes — the clone()+json() pair is gone, and the parse-once regression tests now pin the post-admission contract (original request: zero json() calls, zero clone() calls; the single materialization is admitChatRequest()'s bounded byte reader). Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
8b78bc3
into
cheaper-inference:release/v3.8.49
|
Merged — thanks @RaviTharuma for the whole trilogy (#7852/#7855/#7862/#7853 all landed today)! The atomic admission reservation closes the #7846 TOCTOU for real, your lease-release turnaround was fast, and the final rebase now composes it with the #7862 parse-once discipline (single materialization in the bounded byte reader, zero clones). Validated in the local merge-train on tomni-proxmox-113 @ 9acbe4266 (fast gates green; the 14-PR full-suite train earlier today covered the same route stack) then squash-merged. |
* fix: reserve chat admission before parsing * fix: release admission on handler failure * fix: reconcile chat admission with the cheaper-inference#7862 parse-once route Post-cheaper-inference#7862 rebase: the admission-rebuilt request is json()-parsed directly over the already-buffered bytes — the clone()+json() pair is gone, and the parse-once regression tests now pin the post-admission contract (original request: zero json() calls, zero clone() calls; the single materialization is admitChatRequest()'s bounded byte reader). Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --------- Co-authored-by: Ravi Tharuma <RaviTharuma@users.noreply.github.com> Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com> Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
* fix: reserve chat admission before parsing * fix: release admission on handler failure * fix: reconcile chat admission with the cheaper-inference#7862 parse-once route Post-cheaper-inference#7862 rebase: the admission-rebuilt request is json()-parsed directly over the already-buffered bytes — the clone()+json() pair is gone, and the parse-once regression tests now pin the post-admission contract (original request: zero json() calls, zero clone() calls; the single materialization is admitChatRequest()'s bounded byte reader). Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --------- Co-authored-by: Ravi Tharuma <RaviTharuma@users.noreply.github.com> Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com> Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
Summary
Content-Length413or retryable503instead of risking process terminationThis PR intentionally adds no waiting queue; bounded queueing remains separate under #7849.
Validation
npm run typecheck:coregit diff --checkCloses #7846