Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Node.js connections to or from Azure fail with read ECONNRESET after a quiet spell

Your error

Error: read ECONNRESET3 more lines it can show

Fix it fast

Most common cause, 1 of 2

A pooled connection sat idle past Azure's 4-minute timeout

Close idle connections before 4 minutes or keep them busy with TCP keep-alive probes sent well inside that window.

Fix
import pg from 'pg';
const pool = new pg.Pool({ idleTimeoutMillis: 60_000 });
pool.on('error', (err) => console.error('idle client error', err.code));

// a socket you manage yourself
socket.setKeepAlive(true, 60_000);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

read ECONNRESET at TCP.onStreamRead on Azure means a connection stayed quiet past the 4-minute idle timeout of Azure Load Balancer or the outbound public IP and was reset. On Node.js 22 to 26, keep pool idle time under 4 minutes or send TCP keep-alive probes; if you own the load balancer rule, raise its idle timeout.

  • Applies to Node.js 22, Node.js 24, Node.js 26, node-postgres 8.23, Azure CLI 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Log the time and the socket state of every failure. A reused connection that fails on the first call after four or more quiet minutes is the idle timeout; failures in the middle of steady traffic have another cause.

Run this first
req.on('error', (err) => {
  console.error(new Date().toISOString(), err.code, 'reused socket:', req.reusedSocket);
});

Then find the line you see in this list and open its fix:

  1. 1A pooled connection sat idle past Azure's 4-minute timeoutErrors appear only after idle periods, on database pools, Redis clients, WebSockets or a keep-alive agent without a timeout
  2. 2Your own load balancer rule resets idle flowsYou run the server behind Azure Standard Load Balancer, and long-lived clients drop at the same idle interval every time

What does the error look like?

  • Error: read ECONNRESET
  • read ECONNRESET at TCP.onStreamRead
  • code: 'ECONNRESET', syscall: 'read'
  • The first query or request after a quiet spell fails, and an immediate retry works

Cause 1 of 2

A pooled connection sat idle past Azure's 4-minute timeout

You seeErrors appear only after idle periods, on database pools, Redis clients, WebSockets or a keep-alive agent without a timeout

Check

read ECONNRESET after a quiet spell comes from Azure dropping a connection that stayed idle past its 4-minute timeout. Azure Load Balancer forgets such a flow, the outbound idle timeout of a public IP is fixed at 4 minutes, and a rule with TCP reset turned on sends RST packets to both ends. Look for clients that hold connections longer than that: pools with idle eviction off, a minimum pool size, or a socket you keep yourself.

Check
grep -rnE "idleTimeoutMillis|keepAlive|min:" src/

Fix

Close idle connections before 4 minutes or keep them busy with TCP keep-alive probes sent well inside that window. For node-postgres, keep idleTimeoutMillis low and listen for pool errors; for a raw socket, turn on keep-alive with a short initial delay.

Fix
import pg from 'pg';
const pool = new pg.Pool({ idleTimeoutMillis: 60_000 });
pool.on('error', (err) => console.error('idle client error', err.code));

// a socket you manage yourself
socket.setKeepAlive(true, 60_000);

Verify

Leave the app idle for ten minutes, then send one request. It succeeds on the first try.

Verify
sleep 600 && curl -sS -o /dev/null -w '%{http_code}\n' https://example.com/health

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 2

Your own load balancer rule resets idle flows

You seeYou run the server behind Azure Standard Load Balancer, and long-lived clients drop at the same idle interval every time

Check

read ECONNRESET at a fixed idle interval, on a server you run behind Azure Standard Load Balancer, comes from a rule with enableTcpReset on: at idle timeout the load balancer sends RST to client and server, and Node.js reports ECONNRESET on the next read. Read both settings on the rule.

Check
az network lb rule show -g my-rg --lb-name my-lb -n my-rule --query "{idle:idleTimeoutInMinutes, reset:enableTcpReset}"

Fix

Raise the idle timeout on the rule for clients that stay connected without sending data, or keep the reset and make the clients send keep-alive probes and reconnect cleanly.

Fix
az network lb rule update -g my-rg --lb-name my-lb -n my-rule --idle-timeout 15

Verify

Show the rule again and confirm the new idle timeout.

Verify
az network lb rule show -g my-rg --lb-name my-lb -n my-rule --query idleTimeoutInMinutes

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Write the pool idle timeout (under 240 seconds) into the config of every service that talks to Azure-hosted databases or caches.
  • Register an error listener on every pool and long-lived socket, so a reset idle connection is logged and replaced instead of crashing the process.
  • Retry an idempotent request once when the error code is ECONNRESET and request.reusedSocket is true, the case the Node.js http docs describe.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce, Raheeb.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.