Skip to content

Commit b591fa0

Browse files
committed
heart.ts: Fix leak when server closes
This had me very confused for quite a while until I did a binary search inspection on route/index.ts. Only with the heart.beat line commented out did my tests pass without leaking. They weren't leaking fds but just this heartbeat timer and node of course prints just fds that are active when it detects some sort of leak I guess and that made the whole thing very confusing. These fds are not leaked and will close when node's event loop detects there are no more callbacks to run. no of handles 3 tcp stream { fd: 20, readable: false, writable: true, address: {}, serverAddr: null } tcp stream { fd: 22, readable: false, writable: true, address: {}, serverAddr: null } tcp stream { fd: 23, readable: true, writable: false, address: {}, serverAddr: null } It kept printing the above text again and again for 60s and then the test binary times out I think. I'm not sure if it was node printing the stuff above or if it was a mocha thing. But it was really confusing... cc @code-asher for thoughts on what was going on.
1 parent bf97d37 commit b591fa0

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/node/heart.ts

+9
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ export class Heart {
4545
})
4646
}, this.heartbeatInterval)
4747
}
48+
49+
/**
50+
* Call to clear any heartbeatTimer for shutdown.
51+
*/
52+
public dispose(): void {
53+
if (typeof this.heartbeatTimer !== "undefined") {
54+
clearTimeout(this.heartbeatTimer)
55+
}
56+
}
4857
}

src/node/routes/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export const register = async (
5555
})
5656
})
5757
})
58+
server.on("close", () => {
59+
heart.dispose()
60+
})
5861

5962
app.disable("x-powered-by")
6063
wsApp.disable("x-powered-by")

test/integration.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export async function setup(
88
argv: string[],
99
configFile?: string,
1010
): Promise<[express.Application, express.Application, httpserver.HttpServer, DefaultedArgs]> {
11+
argv = ["--bind-addr=localhost:0", ...argv]
12+
1113
const cliArgs = parse(argv)
1214
const configArgs = parseConfigFile(configFile || "", "test/integration.ts")
1315
const args = await setDefaults(cliArgs, configArgs)

0 commit comments

Comments
 (0)