Repository navigation
fix(functions): end a response the handler leaves open, as deployed functions do - #314
chhavi-mandowara-cstk wants to merge 21 commits into
Conversation
…unctions do A handler that returned without ending its response hung under launch:functions:serve (Azure and GCP answer 504 CF005 after the timeout). Deployed AWS functions (lambda-node-bridge's finalizeResponse) end it as soon as the handler returns or its promise settles, with the status and headers it set, and leave a response whose headers were already sent for the handler to end. Do the same. Anything the handler writes after that (end, write, writeHead, setHeader, and so send, json and redirect through them) is ignored with one warn: line, so a late answer cannot crash serve with ERR_HTTP_HEADERS_SENT.
…AGENTS.md Test code and prose; no credentials.
Review follow-ups for ending an open response like deployed functions: - A late res.sendStatus(204), res.status(205).send() or a direct removeHeader/appendHeader/setHeaders call still reached Node's header methods after the forced end and threw ERR_HTTP_HEADERS_SENT. Ignore those too. - res.sendFile and res.download finish asynchronously, so the forced end cut them to an empty 200 with no warning. A response they are sending is now left for them to end. - The warning names the requested URL instead of the route pattern. - Tests poll instead of sleeping and clean their globals in afterEach.
…AGENTS.md Test code and prose; no credentials.
Second review follow-ups for ending an open response like deployed functions: - A file send is marked only once res.sendFile/download/sendfile returned without throwing, and a callback-style send clears its mark when it settles; if the response is still open then, it is ended as AWS would. Before, a send that threw, or whose callback only set a status, left the request hanging, and the lowercase res.sendfile was cut short. - Once a response has ended for any reason (forced end, the 500 path, or the handler itself), later end/write/header/file calls are ignored with one warning instead of crashing serve. A callback passed to them gets ERR_STREAM_ALREADY_FINISHED on the next tick, and write returns false. - The warning now says the response was changed, not answered, so it also fits header-only calls. - AGENTS.md: the deployment agent's 2.x upgrade drops late answers on Azure and GCP; tests clean LAUNCH_TEST_PAGE in afterEach.
…AGENTS.md Test code and prose; no credentials.
Final review follow-ups for ending an open response like deployed AWS functions: - Install the late-change guard when the response finishes, whatever ended it, so a late header change after a callback-less file send can no longer crash serve (and the guard's repeat branch is covered). - Destroy a stream piped into a response that serve ends for the handler, so serve no longer keeps its file descriptor open. - A late write's callback gets ERR_STREAM_WRITE_AFTER_END, as Node does; end keeps ERR_STREAM_ALREADY_FINISHED. - The warning says deployed AWS functions, since Azure and GCP (V1) still deliver late answers.
…AGENTS.md Test code and prose; no credentials.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Reviewed the whole PR (launch-cli#314, CL-8970) at 5b11fbe.
Off-diff:
- Low
src/functions/cloud-functions.ts:290(answerRequestError, pre-existing): a callback-lessres.sendFile/res.downloadthat fails (missing file) logs "Request rejected before the function ran: 404 Error" although the function ran. Fix: in the no-callback branch oftrackAnswering, pass a callback that ends witherr.status, or haveanswerRequestErrortell route-raised errors apart.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): the contracts match (status kept on a never-ended response, a started stream left to finish, a late answer dropped). e2e#487's Azure/GCP gaps cover clouds still on 1.6.0, so it can merge before or after this PR. Note that serve's late-change guard swallows the late res.end('late') that e2e#487's send-after-return fixture makes, so serve can't show how the AWS bridge treats it (see the Medium there).
3 medium, 7 low: not approved.
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
| ignoreLateChanges(response, answering); | ||
| return; | ||
| } | ||
| if (response.headersSent) { |
There was a problem hiding this comment.
Medium A handler that calls res.writeHead(...) (or flushHeaders()) and returns without end() still hangs forever.
headersSent is true as soon as writeHead stores the header, so this treats the response as a started stream and never ends it, though no body was written. A redirect written as res.writeHead(302, {Location}) that forgets .end() is finalized by AWS but hangs under serve (no timeout in 2.x).
Fix: decide "streaming" from an actual body write (count calls to write), not headersSent, and end responses that only have headers.
Generated by Claude Code
There was a problem hiding this comment.
Keeping this as is, because it matches AWS. The lambda bridge's finalizeResponse (cf-sv-with-helpers.ts) does:
if (res.writableEnded) { resolve(); return; }
if (res.headersSent) { // writeHead / flushHeaders set this
res.once("finish", onComplete);
res.once("close", onComplete);
return; // waits for the handler to end it
}
res.end();So a deployed res.writeHead(302, {Location}); return; is not finalized by AWS either. It waits on finish/close until the Lambda timeout. If serve ended it, the function would work locally and hang when deployed, which is the kind of gap this ticket closes. Deciding from body writes would diverge from the bridge. AGENTS.md documents this: "a response whose headers were already sent … is left for the handler to end".
Generated by Claude Code
…lid status Review follow-ups on #314: - Wrap the late-change methods when the request starts, passing calls through until the response has ended, so a write or end between end() and 'finish' (for example res.end('a'); res.write('b')) is ignored with the warning instead of crashing serve. - If ending the response for the handler throws (an invalid status or status message), log the error and answer 500 instead of an unhandled rejection that stopped serve. - Serve's own 500 for a handler that throws is sent only if the response has not ended, so it no longer triggers a false late-change warning. - A stream piped into a response that serve ended, or that had already ended, is unpiped, and only a file stream is destroyed; a stream the function owns, such as a shared PassThrough, is left open. - Only methods that exist are wrapped (Express 5 has no res.sendfile). - The late-change test after a file send waits for 'finish' instead of a fixed timer.
…AGENTS.md Test code and prose; no credentials.
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Re-review since 5b11fbe (9b95f1f): 0 high, 0 medium, 7 low. Not approved: 7 lows are open, and the limit is 3.
My earlier mediums:
- Invalid status crashing serve: fixed. The narrower case that is left is the low on line 294.
- Late changes between
end()and'finish': fixed, because the guard is now installed before the handler runs. writeHead-only responses hanging: settled. The citedfinalizeResponsewaits on'finish'the same way whenheadersSentis set, so serve matches AWS.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): the contracts still match. The writableEnded guard that #487 now adds to send-after-return makes the late res.end a no-op under this serve as well, so either PR can merge first.
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
…ing 500 Review follow-ups on #314: - An end() with no body on an ended response passes through to Node's own no-op, so res.json(data).end() no longer logs a late-change warning. - Streams piped into a response are released when it closes, whoever ended it, and a stream piped into an already-ended one is released at once. Each is unpiped and destroyed unless it still feeds another destination: an upstream response, web stream or child stdout no longer leaks, a file stream also piped to a cache file is not cut short, and a stream piped late is not left flowing with no reader. - Serve's 500 (a handler that throws, or a failed auto-end) resets the status message and drops the connection if even that end throws, so an invalid status message or a content-length mismatch no longer crashes serve. - .talismanrc: refreshed the checksum for cloud-functions.test.ts (inline test functions, no credentials).
Prose; no credentials.
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Re-review of #314 since 9b95f1f (d516665, 9d63bfa).
The new close-time release crashes serve for streams that aren't core Node Readables (the two Highs), and destroys streams the function owns or still pipes after a disconnect (the two Mediums).
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): no new contract issue; #487's route helper still differs from serve's validator (the Medium I posted there). Either PR can merge first.
Not approved: 2 high and 2 medium open.
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
| } catch (error) { | ||
| console.error(error); | ||
| answerServerError(response); | ||
| } |
There was a problem hiding this comment.
Low Moving the release from here to close can print a false late-change warning.
Between serve's end() and 'close' (delayed e.g. by a pipelined request on the same socket), a still-piped source's data goes through the guarded write, so serve warns that the function changed its response although it didn't.
Fix: keep the immediate release after serve ends the response, in addition to the close listener.
Generated by Claude Code
There was a problem hiding this comment.
Not changed. I can't reproduce the window this needs deterministically. 'close' follows 'finish' within the same tick unless the socket carries a pipelined request, and serve's own end() happens after the handler has returned. A source that is still writing at that moment is the function's late write, and the warning names it accurately. Adding a second release path that no test can exercise would be untested code. If you have a reproducer, I'll add it with a test.
Generated by Claude Code
There was a problem hiding this comment.
Correction to my reply above: the response emits 'close' on the next tick after 'finish' (resOnFinish → process.nextTick(emitCloseNT)), not in the same tick. The rest stands: I have no deterministic reproducer for a source still writing between serve's end() and that 'close'.
Generated by Claude Code
| } catch (error) { | ||
| console.error(error); | ||
| response.status(500).send(); | ||
| if (!response.writableEnded) { |
There was a problem hiding this comment.
Low When a handler throws after the body started, the client gets a clean 200 with a cut-off body.
answerServerError sets statusCode = 500 after headers were sent (no effect) and ends normally, so res.write('partial'); throw looks like success, not the documented 500. Pre-existing.
Fix: when response.headersSent, response.destroy(error) as Express's finalhandler does.
Generated by Claude Code
There was a problem hiding this comment.
Not changed in this PR. It is pre-existing v2-dev behaviour, and the test "ends a response the handler started writing and then threw on, logs the error, and keeps serving" pins it. What AWS does here isn't verified: the bridge rethrows non-ApiErrors out of the server's async request listener. I'd rather not switch serve to destroy() on a guess. AGENTS.md now limits the documented 500 to a handler that throws before its headers were sent.
Generated by Claude Code
…reign stream Review follow-ups on #314 (Dhruv, 9d63bfa): - A piped source is unpiped only if it has unpipe(), so a legacy Stream.pipe source (request, through, event-stream) no longer crashes serve when its response closes. - Only a file stream (fs.ReadStream) is destroyed, and only on the next turn when nothing reads it any more (no 'data' or 'readable' listener, which every pipe() adds). This drops the internal _readableState read that crashed on readable-stream v2/v3 sources, keeps a function-owned stream (an SSE PassThrough, a child's stdout) alive across requests, and lets a late tee or async iterator keep reading the file. - A stream piped after the client disconnected is released too. - Serve's 500 drops the headers the failed handler had set (a stale content-length or set-cookie) when they were not sent yet. - A late end() with a body gives its callback ERR_STREAM_WRITE_AFTER_END, as Node does. - .talismanrc: refreshed the checksum for AGENTS.md (prose, no credentials).
Test code; no credentials.
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Re-review of #314 since 9d63bfa (27a2f35, 2c08689).
The 2 highs and 2 mediums from my review at 9d63bfa are fixed: no _readableState.pipes read, unpipe is guarded, only fs.ReadStreams are destroyed, and a pipe after a disconnect is released. This pass: 7 lows.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): no issues; either PR can merge first.
Not approved: 7 lows open (more than 3).
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
| checksum: 038f5cd182e1fd0105e6e10dde7f5068a0c15883222450f72ea3904890a56c0c | ||
| - filename: src/functions/cloud-functions.test.ts | ||
| checksum: ca4e03b4ae6d4e39fe5ad654f781b002deb37f2f30ebf58c27feeb162696c562 | ||
| checksum: 4ecfc8deb42597cda166c7156720901c12665c1439495b4f3b35b942db4fda49 |
There was a problem hiding this comment.
Low The commits in this range (27a2f35, 2c08689) are authored by Claude but end without the #claude_code# footer and the Co-Authored-By: Claude trailer.
The workspace CLAUDE.md (Git conventions) says these are never optional for internal repos; launch-cli's exception covers only its PR title format.
Fix: end future commits with the footer and trailer (the Conventional Commits subjects can stay).
Generated by Claude Code
There was a problem hiding this comment.
Not changed, on purpose. The ticket owner set explicit rules for this public repo: semantic commit and PR titles, with no +Claude and no Claude Code footer in commits or PRs. That instruction takes precedence over the workspace's internal-repo template, so these commits follow it.
Generated by Claude Code
Review follow-ups on #314 (Dhruv, 2c08689): - After unpiping a source, serve settles it on the next turn: a file stream nothing else reads is destroyed as before; any other stream not read through 'readable' is resumed, so it drains and finishes. A file stream with a side 'data' reader, a gzip transform feeding the response (and the file behind it), a child's stdout or an upstream body no longer stay paused holding a descriptor, socket or child, and the close path now leaves a stream in the same flowing state as the late-pipe path. Serve still never destroys a stream the function owns. - Serve's 500 restores its cache-control: no-store default after dropping the failed handler's headers. - The readable-stream v2/v3 test builds that shape inline instead of relying on a hoisted, undeclared package.
…AGENTS.md Test code and prose; no credentials.
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Re-review of #314 since 2c08689 (f6f8ddd, ed948ba).
The fixes for my seven earlier lows hold. The resume() I suggested on line 233 goes too far, though: resuming a released stream that has no other consumer can spin serve on an endless stream and drops data from a stream the function keeps (inline). That's my suggestion being incomplete, not a misreading of it. This pass: 5 lows.
No high or medium findings from earlier reviews are still open.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): no contract issue; #487's guard is offline and doesn't depend on the stream handling here. Either PR can merge first.
Not approved: 5 lows open (more than 3).
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
Review follow-ups on #314 (Dhruv, ed948ba): - A released stream is resumed only when it still has a 'data' reader, so that reader gets the rest; a stream nothing reads is left paused, as Node leaves it after an unpipe. Resuming every released stream could spin serve on an endless synchronous producer and drained a queue the function reuses across requests. - A released source without listenerCount (a stream-like object) is left alone instead of throwing from setImmediate, and one without resume is never resumed. - Tests: the released-PassThrough test now expects it paused and buffering; an endless producer no longer blocks the next request; a non-core stream with a reader is resumed through its own resume; and two stream-like objects without listenerCount or resume keep serve up. The gzip drain test is dropped with the behaviour it pinned. - AGENTS.md states the rule and its limit: only a file stream nothing reads is closed for the function; anything else it leaves paused stays open until the function closes it.
…AGENTS.md Test code and prose; no credentials.
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Re-review of #314 since ed948ba (4b128ee, c30e51b).
The fixes for my lows at ed948ba hold (the listenerCount guard, the AGENTS.md wording, no resume of a stream nothing reads). Leaving every unread stream paused trades the spin for two other problems, one of them new (inline):
- Medium (inline, line 233): a request body piped into the response is now left paused, which stalls the upload and the keep-alive connection.
- Low
src/functions/cloud-functions.ts:233: a transform, child stdout or upstream body is left paused when the handler returns before its pipe sends headers or the client disconnects, so the file, child or socket behind it stays open for the life offunctions:serve(e.g.createReadStream(f).pipe(createGzip()).pipe(res)holds one fd per request). The deleted test ('drains a transform it unpiped…') covered this. This is my earlier low at 233 (2c08689), back in another form; draining in bounded,setImmediate-yielding batches would cover it and the endless producer alike. - Low
src/functions/cloud-functions.ts:341: the late-pipe path still drains. Serve's unpipe runs insidepipe(), andpipe()then callssrc.resume()itself, so a stream piped into an already-ended response flows with no reader: an endless synchronous producer still blocks serve there, and a reused PassThrough loses its data. Deferring the late-pathreleasewithprocess.nextTickavoids it. (Same spot as my earlier low at 341.) - Low
AGENTS.md:564: "Any other stream is left paused, as Node leaves it after an unpipe" is not true on the late-pipe path (above), and the request body, which serve owns, isn't mentioned.
No high or medium findings from earlier reviews are still open.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): no contract issue; #487 doesn't depend on this stream handling. Either PR can merge first.
Not approved: 1 medium open.
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
| return; | ||
| } | ||
| if (read && typeof source.resume === 'function') { | ||
| source.resume(); |
There was a problem hiding this comment.
Low An endless synchronous producer that still has a side 'data' reader (a byte counter, a logging tap) still blocks serve.
s = new Readable({ read() { this.push(Buffer.alloc(1024)); } }); s.on('data', c => n += c.length); s.pipe(res); res.end('done'): after the response closes, settleReleased sees a 'data' listener and calls resume(); that listener applies no backpressure, so flow() never yields and the next request is never served. While piped, the response's backpressure held it back; serve's resume() is what starts the loop.
Fix: resume through a bounded drain that yields between batches (or pause again after a bounded number of chunks) instead of a bare resume().
Generated by Claude Code
There was a problem hiding this comment.
Not changed in this PR. This is the same trade-off as your earlier finding on line 233, from the other side. Serve can't tell from a 'data' listener whether it is a byte counter on an endless producer, or a reader that needs the rest of a finite stream (the side-reader file case, which otherwise holds its descriptor).
A bounded, yielding drain would cover both, but it also drops data from a queue the function reuses. That was your other finding on the earlier resume-everything version. Choosing between those needs a design decision, not another tweak inside gap 4. I'm proposing to the ticket owner that the remaining third-party stream policy move to a follow-up ticket. Today's rule covers serve's own streams (the request, file descriptors) and is documented in AGENTS.md.
Generated by Claude Code
| return; | ||
| } | ||
| if (read && typeof source.resume === 'function') { | ||
| source.resume(); |
There was a problem hiding this comment.
Low source.resume() overrides a pause the function's own 'data' reader is holding.
A reader that does src.pause(); await save(); src.resume() gets the next chunk while save() is still running if the response closes during that window, so saves overlap and can finish out of order.
Fix: record whether the stream was flowing (readableFlowing === true) before the unpipe, and resume only when serve's unpipe is what paused it.
Generated by Claude Code
There was a problem hiding this comment.
Not changed in this PR, for the same reason as the endless-producer finding. Resuming only streams that were flowing before the unpipe would also skip a file stream that the response's backpressure had paused. That is exactly the side-reader case you asked to close in the round before. Serve can't tell a backpressure pause from the function's own pause. I'm proposing the third-party stream policy as a follow-up ticket rather than another rule swap inside gap 4.
Generated by Claude Code
…e after pipe() returns Review follow-ups on #314 (Dhruv, c30e51b): - A request or upstream body (http.IncomingMessage) that serve releases is drained. A handler that pipes its request into its response and returns before a large body arrives no longer stalls the upload and the keep-alive connection: Node does not dump a request pipe() already consumed, so leaving it paused hung the next request on that socket. - A stream piped into an already-ended response is released on the next tick, after pipe() has called resume() itself, so it ends up paused like one released on close instead of flowing with no reader. - The endless-producer test caps its producer and asserts it stayed paused, so a regression fails the test instead of hanging the run. - AGENTS.md describes the drained bodies and that both paths leave any other stream paused. - .talismanrc: refreshed the checksums for cloud-functions.test.ts and AGENTS.md (test code and prose, no credentials).
|
Re-review at c30e51b, with the off-diff lows addressed in c225c9d:
Generated by Claude Code |
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Re-review of #314 since c30e51b (c225c9d).
The medium from my review at c30e51b is fixed: a request body piped straight into the response is drained on release, and the late-pipe release now runs after pipe() returns, so the late path no longer drains (that low and the AGENTS.md low are fixed too). This pass: 4 lows (inline). The first is my own suggestion taken at its broader form: instanceof IncomingMessage also matches upstream bodies, where comparing against this exchange's request was the narrower option.
No high or medium findings from earlier reviews are still open.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): no contract issue; either PR can merge first.
Not approved: 4 lows open (more than 3).
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
|
|
||
| function settleReleased(source: Readable): void { | ||
| if (source instanceof IncomingMessage) { | ||
| source.resume(); |
There was a problem hiding this comment.
Low (pre-existing) A request piped into the response through a transform still stalls the keep-alive connection.
request.pipe(new PassThrough()).pipe(response) (or a gzip), ended before a large body arrives: on close only the transform is released and it stays paused; once its buffer fills it backpressures the request, which Node won't dump because pipe() already consumed it, so the next request on that socket is never answered. The direct request.pipe(response) case is fixed by this commit; this one isn't.
Fix: on response close, if this exchange's request is still incomplete and nothing outside serve still reads it, unpipe and drain it; or at least name the case in AGENTS.md next to the gzip example.
Generated by Claude Code
There was a problem hiding this comment.
Documented in 294fd07, the second option you gave. AGENTS.md now names "a file stream or the request behind a transform (for example gzip)" among the streams left to the function, noting that this "stays open, or stalls that keep-alive connection, until the function closes it".
Unpiping the request from the function's own transform would cut into a pipeline the function built, and serve can't tell when that's safe. That belongs with the follow-up on third-party stream policy.
Generated by Claude Code
…pipe at once Review follow-ups on #314 (Dhruv, c225c9d): - Only this exchange's own request body is drained on release, as Node would dump it. An upstream body (a live feed, an SSE relay) piped into the response is left paused like any other stream instead of being read at full speed after the client has gone. - A stream piped into an already-ended response is unpiped inside the 'pipe' event again, so a shared hub's next write does not reach the ended response and log a false late-change warning, and a throwing unpipe surfaces in the caller's pipe() as on v2-dev. To undo pipe()'s own resume, the source is paused from a one-shot 'resume' listener (removed on the next turn); a reader it still has is resumed by the settle step, as on close. An endless producer piped late no longer spins. - AGENTS.md names only the request as drained and lists a request piped through a transform among the streams left to the function. - .talismanrc: refreshed the checksum for cloud-functions.test.ts (test code, no credentials).
Prose; no credentials.
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Re-review since c225c9d (294fd07, 738037a): 0 high, 0 medium, 5 low (inline). The two around line 226 are the third-party stream trade-offs AGENTS.md already documents; they fit the follow-up ticket you proposed.
No high or medium findings from earlier reviews are still open.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#487): no new contract issue; these commits only change serve's stream release, which #487 doesn't exercise.
Not approved: 5 lows open in this pass, and the limit is 3.
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
| } | ||
|
|
||
| function settleReleased(source: Readable, request: Readable): void { | ||
| if (source === request) { |
There was a problem hiding this comment.
Low Narrowing the drain to source === request leaves a released upstream IncomingMessage paused and never destroyed, so its socket stays held.
A proxy handler http.get(url, up => up.pipe(res)) whose client aborts (or which pipes after the handler returned) has up unpiped and left paused for good; each such request keeps one upstream connection, and with a bounded agent (new Agent({ keepAlive: true, maxSockets: 10 })) the 11th request queues forever. AGENTS.md documents this ("an upstream body then stays open"), and the default agent is unbounded, so it is low for local serve.
Fix: destroy a released IncomingMessage that isn't this exchange's request and has no 'data' reader, the same way fs.ReadStream is handled — or cover it in the proposed follow-up ticket on third-party stream policy.
Generated by Claude Code
| } | ||
|
|
||
| function settleReleased(source: Readable, request: Readable): void { | ||
| if (source === request) { |
There was a problem hiding this comment.
Low (pre-existing, documented in AGENTS.md) The request body behind a transform still stalls the keep-alive connection.
request.pipe(zlib.createGunzip()).pipe(response) with the handler returning before a large upload arrives: serve ends the response, the gunzip is unpiped and paused, the request stops being read, and the next request on that socket (agent with maxSockets: 1) never gets an answer.
Fix: on the response's 'close', if !request.complete, unpipe the request from everything and resume() it (AWS buffers the whole body anyway) — fine to leave to the follow-up ticket.
Generated by Claude Code
| const pause = (): void => { | ||
| source.pause(); | ||
| }; | ||
| source.once('resume', pause); |
There was a problem hiding this comment.
Low The one-shot 'resume' listener can't tell pipe()'s own resume from the function's.
If the source is already flowing into another destination, pipe() doesn't resume it, so the listener stays armed until the next turn and pauses the function's own resume() in that window: hub.pipe(other); hub.pipe(endedRes); Promise.resolve().then(() => { hub.unpipe(other); hub.resume(); }) leaves hub paused with data buffered, and settleReleased won't resume it (no 'data' reader).
Fix: arm the listener only when pipe() will resume the source, i.e. when source.readableFlowing !== true at that point.
Generated by Claude Code
| source.pause(); | ||
| }; | ||
| source.once('resume', pause); | ||
| setImmediate(() => source.removeListener('resume', pause)); |
There was a problem hiding this comment.
Low pauseOnResume guards once and pause but calls source.removeListener unchecked inside setImmediate.
A stream-like source that has once/pause but no removeListener, piped into an ended response, throws TypeError: source.removeListener is not a function on the next turn, outside any try, and takes serve down. Unlikely with real Node streams, but it's the same duck-typed class of source the unpipe guard was added for.
Fix: add typeof source.removeListener === 'function' to the guard (or check it in the callback).
Generated by Claude Code
| ] as const; | ||
|
|
||
| interface Answering { | ||
| request: Readable; |
There was a problem hiding this comment.
Low Answering.request is set in trackAnswering but never read; the release paths use the closure's request.
Dead state that a later change could read and drift from the closure value.
Fix: drop the field, or pass answering.request so there is one source of truth.
Generated by Claude Code
What was wrong
A handler that returns without ending its response hung under
launch:functions:serveuntil the client gave up. Azure and GCP (CLI V1) answer 504 CF005 after the timeout.Deployed AWS functions (
lambda-node-bridge/cf-sv-with-helpers.ts,finalizeResponse) end it as soon as the handler returns or its promise settles, with the status and headers it set:res.end().This is CL-8970 gap 4. The ticket owner chose to match AWS exactly (option A, agreed with Dhruv), because any other choice would change behaviour for existing AWS sites. One consequence: a callback that answers after the handler returned gets an empty response, as it does on AWS today.
The change (
src/functions/cloud-functions.ts)Ending the response. After the handler returns, or throws,
endLikeDeployedFunctionsends an open response with the status and headers it set. It leaves the response alone in two cases: headers were already sent, orres.sendFile/download/sendfileis still sending.Serve's own 500 never crashes serve. This covers a handler that throws before its headers were sent, and an auto-end that fails (an invalid status or status message, a content-length mismatch). The error is logged, and serve answers a
cache-control: no-store500 without the headers the handler had set, with the status message reset. If even thatend()throws, serve destroys the response.Late changes no longer crash serve. The guards are installed before the handler runs and pass calls straight through until the response has ended. From then on, a later
endwith a body,write,writeHead, header call or file send is ignored with onewarn:line naming the requested URL.send,jsonandredirectare covered through those calls.end()with no body is Node's own no-op and is not warned about.ERR_STREAM_WRITE_AFTER_END(write, orendwith a body) orERR_STREAM_ALREADY_FINISHED, as Node does.writereturnsfalse.res.sendfile).Piped streams. When a response closes, or a stream is piped into one that has already ended or whose client has gone, each source is unpiped, if it has
unpipe, inside the'pipe'event on the late path. On the next turn:fs.ReadStream) that nothing else reads is destroyed;'data'reader is resumed for that reader;'resume'listener afterpipe()resumes it.Sources without
listenerCount,resume,onceorpauseare left alone.AGENTS.md.res.sendfiledeprecation warning names serve's wrapper..talismanrc. Refreshed checksums forcloud-functions.test.tsandAGENTS.md. Both are test code and prose, with no credentials.Tests
src/functions/cloud-functions.test.ts(118 tests):send,json,end,write,writeHead,setHeader,redirect,sendStatus(204),status(205).send(),removeHeader,appendHeader,setHeaders,sendFile,downloadandsendfileis ignored, warns once, and serve keeps answering.end("a"); write("b"); end("c")warns once;json(...).end(); end(); end(cb)does not warn.writereturningfalse.content-length/set-cookiedropped andno-storekept;'finish'.'data'reader or afor awaitreader keeps delivering;PassThroughstays paused and buffering;listenerCount,resume,once,pause) don't crash serve.res.sendfileremoved, requests still answer.Mutation checks: I removed or inverted each guard and each release rule, one at a time, and each change turned a test red. Coverage of
cloud-functions.tsis unchanged from v2-dev apart from the new code, which is fully covered.Full suite in the pre-push hook: 201 suites, 4163 tests passed. Every hook ran, with no
--no-verify.E2E (http-sanity cloud-functions checks, AWS expectations)
I ran all 22 checks against
launch:functions:servebuilt from each side, using e2e PR #487:v2-dev 8899a3e: 13/22.
This branch: 16/22, last measured at 2c08689. Later commits change only stream release and serve's 500. Now passing:
a handler that never ends the response still answersa handler that never ends the response keeps the status it seta response ended after the handler returned is not delivereda response the handler started streaming is delivered wholepasses on both sides, and no check got worse. The 6 that still fail are later gaps (5–11).e2e PR #487 adds those checks and Dhruv's
res.send('gone', 410)check, all behindLAUNCH_E2E_CLOUD_FUNCTION_PARITY. The owner's dev11 run on AWS passed the first 20 checks. A rerun with all 22 is pending.Review
Eleven review passes. Accepted findings are fixed with tests, and each review thread has a reply. Not changed:
res.writeHead()with noend()is left open. AWS'sfinalizeResponsedoes the same, becauseheadersSentis true.end()and'close'. I found no deterministic reproducer.QA: the Launch QA agent tests CL-8970 in the repos once it is in QA; GoCD sanity covers dev11.
Jira: https://contentstack.atlassian.net/browse/CL-8970