From 82e1678deab95231fa8567d2ad4c9d96e3521020 Mon Sep 17 00:00:00 2001 From: hgshreyas Date: Thu, 13 Aug 2026 11:16:27 +0530 Subject: [PATCH] fix: warn when connection limit drops a socket Signed-off-by: hgshreyas --- src/app/index.ts | 2 ++ src/http/server.ts | 16 +++++++++++++- src/http/types.ts | 3 +++ test/http.test.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/app/index.ts b/src/app/index.ts index dac515e..3a9e3e4 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -274,6 +274,7 @@ export function createApp(opts: { const fetchOpts: HttpOptions = { bus, + logger, limits: opts.limits, tls: opts.tls, trustProxy: opts.trustProxy, @@ -305,6 +306,7 @@ export function createApp(opts: { server = createHttpServer(httpRoutes, wsRoutes, bus, meshControl, { bus, + logger, limits: opts.limits, streams, tls: opts.tls, diff --git a/src/http/server.ts b/src/http/server.ts index 37a65c5..555ccd0 100644 --- a/src/http/server.ts +++ b/src/http/server.ts @@ -53,7 +53,21 @@ export function createHttpServer( ? nodeHttps.createServer(opts.tls as https.ServerOptions, handler) : nodeHttp.createServer(handler)) as unknown as http.Server; const maxConnections = opts?.limits?.maxConnections ?? 1000; - if (maxConnections > 0) server.maxConnections = maxConnections; + + if (maxConnections > 0) { + server.maxConnections = maxConnections; + let lastDropWarn = 0; + server.on('drop', ({ remoteAddress, remotePort }) => { + const now = Date.now(); + if (now - lastDropWarn < 60_000) return; + lastDropWarn = now; + opts?.logger?.warn( + `maxConnections (${maxConnections}) reached \u2014 dropped connection from ${remoteAddress ?? 'unknown'}:${remotePort ?? 'unknown'}`, + { maxConnections, remoteAddress, remotePort }, + ); + }); + } + server.requestTimeout = opts?.limits?.requestTimeoutMs ?? 30_000; server.headersTimeout = opts?.limits?.headersTimeoutMs ?? 10_000; server.keepAliveTimeout = opts?.limits?.keepAliveTimeoutMs ?? 5_000; diff --git a/src/http/types.ts b/src/http/types.ts index 4e9d61f..d4be7cf 100644 --- a/src/http/types.ts +++ b/src/http/types.ts @@ -5,6 +5,7 @@ import type { TlsOptions, SecurityOptions, CorsOptions } from '../security'; import type { WsRequest, WsSocket } from './ws-core'; import type { StaticResolver } from '../views'; import type { Bus } from '../bus'; +import type { Logger } from '../logger'; /** Per-request and per-server resource ceilings applied by the server. */ export interface RequestLimits { @@ -24,6 +25,8 @@ export interface HttpOptions { * a separate channel for it. */ bus?: Bus; + /** Where this server writes diagnostics. Optional because `createHttpServer` takes no options at all in tests. */ + logger?: Logger; limits?: RequestLimits; streams?: Set<() => void>; tls?: TlsOptions; diff --git a/test/http.test.ts b/test/http.test.ts index a358652..2787f87 100644 --- a/test/http.test.ts +++ b/test/http.test.ts @@ -1,4 +1,6 @@ -import { describe, expect, it, test } from 'vitest'; +import { describe, expect, it, test, vi } from 'vitest'; +import * as net from 'node:net'; +import { once } from 'node:events'; import { matchRoute, parseQuery, createHttpServer } from '../src/http'; const handler = async () => ({ status: 200, headers: {}, body: 'ok' }); @@ -143,6 +145,55 @@ describe('request hardening', () => { server.close(); }); + it('warns when maxConnections drops a real connection and throttles repeated warnings', async () => { + const logger = { + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }; + const server = createHttpServer([], [], undefined, undefined, { + logger, + limits: { maxConnections: 1 }, + }); + + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + const address = server.address(); + if (!address || typeof address === 'string') throw new Error('server did not bind to a TCP port'); + + const first = net.createConnection({ host: '127.0.0.1', port: address.port }); + await once(first, 'connect'); + + const waitForClose = (socket: net.Socket): Promise => + new Promise((resolve) => { + socket.on('error', () => undefined); + socket.once('close', () => resolve()); + }); + + const second = net.createConnection({ host: '127.0.0.1', port: address.port }); + const third = net.createConnection({ host: '127.0.0.1', port: address.port }); + const secondClosed = waitForClose(second); + const thirdClosed = waitForClose(third); + + try { + await Promise.all([secondClosed, thirdClosed]); + + expect(logger.warn).toHaveBeenCalledOnce(); + expect(logger.warn).toHaveBeenCalledWith( + expect.stringContaining('maxConnections (1) reached \u2014 dropped connection from'), + expect.objectContaining({ + maxConnections: 1, + remoteAddress: '127.0.0.1', + }), + ); + } finally { + first.destroy(); + second.destroy(); + third.destroy(); + await new Promise((resolve) => server.close(() => resolve())); + } + }); + it('requestTimeout does NOT kill an in-flight SSE stream (streaming regression)', async () => { async function* feed() { for (let n = 1; n <= 3; n++) { yield { n }; await new Promise((r) => setTimeout(r, 80)); } } const server = createHttpServer(