Skip to content

Commit 218ca45

Browse files
authored
Cleanup closed connection when client disconnects. (#5783)
Fixes #5346 (Thank you @beezital)
1 parent 254dc8b commit 218ca45

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
- Fixes bug where the functions emulator would attempt to call to prod for 'demo-' projects (#5170)
1111
- Address issues starting the Firebase Hosting emulator with some versions of Next.js (#5781)
1212
- Fix regex page matcher for Next.js middlewares version 1 (#5496)
13+
- Fixes bug where functions emulator broke when client request disconnects (#5783)

src/emulator/functionsRuntimeWorker.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,20 @@ export class RuntimeWorker {
161161
}
162162
const proxy = http.request(reqOpts, (_resp: http.IncomingMessage) => {
163163
resp.writeHead(_resp.statusCode || 200, _resp.headers);
164+
165+
let finished = false;
166+
const finishReq = (event?: string): void => {
167+
this.logger.log("DEBUG", `Finishing up request with event=${event}`);
168+
if (!finished) {
169+
finished = true;
170+
onFinish();
171+
resolve();
172+
}
173+
};
174+
_resp.on("pause", () => finishReq("pause"));
175+
_resp.on("close", () => finishReq("close"));
164176
const piped = _resp.pipe(resp);
165-
piped.on("finish", () => {
166-
onFinish();
167-
resolve();
168-
});
177+
piped.on("finish", () => finishReq("finish"));
169178
});
170179
proxy.on("timeout", () => {
171180
this.logger.log(

0 commit comments

Comments
 (0)