avoid zombie connections #491

Massimo Melina committed Feb 24, 2024 at 13:41 UTC a70121f57f34940763d7856aa7e3558e06605e19
1 file changed +7 -5
src/listen.ts
+7 -5
@@ -8,7 +8,7 @@ import { watchLoad } from './watchLoad'
8 import { networkInterfaces } from 'os';
9 import { newConnection } from './connections'
10 import open from 'open'
11 -import { debounceAsync, ipForUrl, makeNetMatcher, objSameKeys, onlyTruthy, runAt, wait, waitFor } from './misc'
11 +import { debounceAsync, ipForUrl, makeNetMatcher, MINUTE, objSameKeys, onlyTruthy, runAt, wait, waitFor } from './misc'
12 import { PORT_DISABLED, ADMIN_URI, argv, DEV, IS_WINDOWS } from './const'
13 import findProcess from 'find-process'
14 import { anyAccountCanLoginAdmin } from './adminApis'
@@ -35,12 +35,13 @@ export function getHttpsWorkingPort() {
35 return httpsSrv?.listening && (httpsSrv.address() as any)?.port
36 }
37
38 -const commonOptions = { requestTimeout: 0 }
38 +const commonServerOptions: http.ServerOptions = { requestTimeout: 0 }
39 +const commonServerAssign = { headersTimeout: 30_000, timeout: MINUTE } // 'headersTimeout' is not recognized by type lib, and 'timeout' is not effective when passed in parameters
40
41 const considerHttp = debounceAsync(async () => {
42 await waitFor(() => app)
43 stopServer(httpSrv).then()
43 - httpSrv = Object.assign(http.createServer(commonOptions as any, app.callback()), { name: 'http' })
44 + httpSrv = Object.assign(http.createServer(commonServerOptions, app.callback()), { name: 'http' }, commonServerAssign)
45 const port = await startServer(httpSrv, { port: portCfg.get(), host: listenInterface.get() })
46 if (!port) return
47 httpSrv.on('connection', newConnection)
@@ -87,8 +88,9 @@ const considerHttps = debounceAsync(async () => {
88 try {
89 await waitFor(() => app)
90 httpsSrv = Object.assign(
90 - https.createServer(port === PORT_DISABLED ? {} : { ...commonOptions, key: httpsOptions.private_key, cert: httpsOptions.cert }, app.callback()),
91 - { name: 'https' }
91 + https.createServer(port === PORT_DISABLED ? {} : { ...commonServerOptions, key: httpsOptions.private_key, cert: httpsOptions.cert }, app.callback()),
92 + { name: 'https' },
93 + commonServerAssign
94 )
95 if (port >= 0) {
96 const cert = getCertObject()