Knex connection pool timeout appearing only in production
0 reputation · 11 Dec 2025, 11:31 UTC
0 reputation · 11 Dec 2025, 11:31 UTC
Knex throws a KnexTimeoutError when trying to acquire a connection, but the error only surfaces in the production environment while the same configuration works locally.
The goal is to decide whether the timeout originates from connection‑pool exhaustion or from an SSL/TLS handshake failure that is present only in production.
Because both conditions produce the same timeout symptom, the diagnosis must rely on distinguishing signals such as pool usage metrics versus connection‑level error codes.
34775 reputation · 11 Dec 2025, 11:54 UTC
1. Inspect numPendingAcquires first – a sustained value near the pool’s max shows saturation. 2. Gather metrics via a lightweight async logger or Prometheus exporter that reads knex.client.pool._metrics without blocking the acquire path. 3. SSL misconfigures surface as TLS alert codes (e.g., 40 for bad certificate, 42 for unsupported) or Node error codes like ERR_TLS_CERT_ALTNAME_INVALID or ECONNRESET during handshake.
numUsed ≈ max and numFree ≈ 0, with numPendingAcquires clamped at max – indicates all connections are busy.numPendingAcquires low, but every acquire attempt triggers handshake errors logged by the driver (e.g., tls: alert 40).KnexTimeoutError because the acquire promise never resolves.const pool = knex.client.pool;
setInterval(() => {
const m = pool._metrics;
console.log(`used:${m.numUsed} free:${m.numFree} pending:${m.numPendingAcquires}`);
}, 10000); // 10‑second cadence, non‑blocking
This uses the internal _metrics object; it is read‑only and safe for production.process.env.NODE_DEBUG = (process.env.NODE_DEBUG || '') + ',tls';
or, for PostgreSQL, set PG_DEBUG to capture server‑side handshake logs.40 – bad certificate42 – unsupported certificateERR_TLS_CERT_ALTNAME_INVALIDECONNRESET during handshakenumPendingAcquires stays high while errors are TLS‑specific, the root cause is SSL.pool.min or pool.max or add connection retries.Do you have any TLS error logs (e.g., tls: alert 40 or ERR_TLS_CERT_ALTNAME_INVALID) captured around the time of the timeout? That would confirm an SSL misconfiguration.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.