From 7de0ad9d8ee1beb07d43777ea20ddb7967957868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Wed, 8 Jul 2026 15:04:36 -0600 Subject: [PATCH] diag: sonda dns.lookup vs dns.resolve4 del host de Postgres (temporal) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Para CONFIRMAR/refutar (con dato, no inferencia) si los cuelgues de ~30s con CPU 0% son getaddrinfo (dns.lookup, threadpool de libuv) colgándose por el DNS embebido de Docker. services/dns-probe.js: cada 3s cronometra dns.lookup (getaddrinfo/threadpool, el sospechoso) vs dns.resolve4 (c-ares, sin threadpool, el control) contra config.db.host, y loguea ms + error de cada uno ('dns-probe'). Bucles separados auto-reprogramados: si lookup se cuelga no se apilan sondas y resolve4 sigue como latido. Cero cambio de comportamiento (solo resuelve y loguea). Se detiene en el shutdown. Confirma: durante un freeze lookup→~30s mientras resolve4 sigue en ms. Refuta: lookup sigue rápido → el DNS no es la causa. Revertir: git revert de este commit. Co-Authored-By: Claude Fable 5 --- backend/src/index.js | 7 ++++ backend/src/services/dns-probe.js | 54 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 backend/src/services/dns-probe.js diff --git a/backend/src/index.js b/backend/src/index.js index ce4cff6..31eed34 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -19,6 +19,7 @@ import kioscoRoutes from './routes/kiosco.js'; import solicitudesRoutes from './routes/solicitudes.js'; import mantenimientoRoutes from './routes/mantenimiento.js'; import { startTareas } from './services/tareas.js'; +import { startDnsProbe } from './services/dns-probe.js'; const fastify = Fastify({ // node vive detrás del nginx del propio container (y de Traefik en prod): @@ -104,11 +105,13 @@ fastify.get('/api/health', async () => ({ })); let tareasInterval = null; +let dnsProbe = null; const shutdown = async (signal) => { fastify.log.info({ signal }, 'Graceful shutdown iniciado'); try { if (tareasInterval) tareasInterval.forEach((i) => clearInterval(i)); + if (dnsProbe) dnsProbe.stop(); await fastify.close(); await db.end(); process.exit(0); @@ -150,6 +153,10 @@ await fastify.register(mantenimientoRoutes, { prefix: '/api' }); // Tareas internas: auto-cierre a revisión + limpieza de tokens (§6, §11.8). tareasInterval = startTareas(db, fastify.log); +// Sonda diagnóstica temporal (dns.lookup vs dns.resolve4 del host de PG) para +// confirmar/refutar que los cuelgues son getaddrinfo colgando el threadpool. +dnsProbe = startDnsProbe(config.db.host, fastify.log); + try { const address = await fastify.listen({ port: config.apiPort, host: config.apiHost }); fastify.log.info(`Taboada API lista en ${address}`); diff --git a/backend/src/services/dns-probe.js b/backend/src/services/dns-probe.js new file mode 100644 index 0000000..684813b --- /dev/null +++ b/backend/src/services/dns-probe.js @@ -0,0 +1,54 @@ +import dns from 'node:dns'; + +// ── SONDA DIAGNÓSTICA (temporal) ───────────────────────────────────────────── +// Cronometra periódicamente dos formas de resolver el host de Postgres: +// · dns.lookup → getaddrinfo, corre en el threadpool de libuv (EL SOSPECHOSO: +// si el DNS embebido de Docker se cuelga, agota el threadpool). +// · dns.resolve4 → c-ares, NO usa el threadpool (EL CONTROL). +// Corre cada pocos segundos para caer dentro de una ventana de cuelgue. +// +// CONFIRMA la hipótesis si durante un freeze `lookup` se dispara (~30s) mientras +// `resolve4` sigue en ms. La REFUTA si `lookup` sigue rápido durante el cuelgue. +// Además valida la cura: si `resolve4` resuelve el nombre `postgres` de forma +// rápida y fiable, moverse a c-ares (cura 1a) es viable. +// +// Cero cambio de comportamiento: solo resuelve un nombre y loguea. No toca el +// pool, ni requests, ni la DB. Quitar (git revert) tras confirmar/refutar. + +function medir(fn, host) { + return new Promise((resolve) => { + const t0 = process.hrtime.bigint(); + fn(host) + .then(() => resolve({ ms: Math.round(Number(process.hrtime.bigint() - t0) / 1e6), err: null })) + .catch((e) => + resolve({ ms: Math.round(Number(process.hrtime.bigint() - t0) / 1e6), err: e.code || e.message }) + ); + }); +} + +export function startDnsProbe(host, log, intervalMs = 3000) { + const state = { stopped: false }; + + // Cada método corre en su propio bucle auto-reprogramado: así, si `lookup` se + // cuelga 30s, NO se apilan sondas (no amplifica el problema) y `resolve4` sigue + // reportando en paralelo cada 3s como latido — se ve directo si uno se dispara + // mientras el otro sigue rápido. + const bucle = (nombre, fn) => { + let timer = null; + const run = async () => { + if (state.stopped) return; + const { ms, err } = await medir(fn, host); + log[ms >= 1000 || err ? 'warn' : 'info']({ dns: nombre, host, ms, err }, 'dns-probe'); + if (!state.stopped) timer = setTimeout(run, intervalMs); + }; + run(); + return () => { if (timer) clearTimeout(timer); }; + }; + + const stops = [ + bucle('lookup', (h) => dns.promises.lookup(h)), + bucle('resolve4', (h) => dns.promises.resolve4(h)), + ]; + + return { stop: () => { state.stopped = true; stops.forEach((s) => s()); } }; +}