axios: ECONNRESET at Function.AxiosError.from

Hi there,

I was using axios v0.21.4 and constantly receiving ECONNRESET errors throughout the day:

Error: read ECONNRESET
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)

After updating to the latest version (v1.1.3) and some rewrites to use the AbortController instead of CancelToken and an NodeJS-Upgrade (v18) I still receive the EONNRESET error but with some more information:

Error: read ECONNRESET
    at Function.AxiosError.from (/home/app/appDir/node_modules/axios/lib/core/AxiosError.js:89:14)
    at RedirectableRequest.handleRequestError (/home/app/appDir/node_modules/axios/lib/adapters/http.js:516:25)
    at RedirectableRequest.emit (node:events:525:35)
    at ClientRequest.eventHandlers.<computed> (/home/app/appDir/node_modules/follow-redirects/index.js:14:24)
    at ClientRequest.emit (node:events:513:28)
    at TLSSocket.socketErrorListener (node:_http_client:494:9)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

Further information

I send similar requests over and over throughout the day. Some of the requests (axios.get) are handled correctly, some return with

AxiosError: timeout of 2000ms exceeded
    at RedirectableRequest.handleRequestTimeout (/home/app/appDir/node_modules/axios/lib/adapters/http.js:553:16)
    at RedirectableRequest.emit (node:events:513:28)
    at Timeout.<anonymous> (/home/app/appDir/node_modules/follow-redirects/index.js:169:12)
    at listOnTimeout (node:internal/timers:564:17)
    at processTimers (node:internal/timers:507:7)

and some lead to the ECONNRESET error above.

Is this a problem within my code or a general problem within axios?

Axios Version

1.1.3

Node.js Version

18

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 22
  • Comments: 19

Most upvoted comments

I am Also facing this issue. I am using axios 1.2.2 version. Out of 200 request 4 to 5 requests are getting failed with this error.

Error: read ECONNRESET at Function.AxiosError.from (/var/task/node_modules/rest-adapter/node_modules/axios/dist/node/axios.cjs:789:14) at RedirectableRequest.handleRequestError (/var/task/node_modules/rest-adapter/node_modules/axios/dist/node/axios.cjs:2744:25) at RedirectableRequest.emit (events.js:412:35) at RedirectableRequest.emit (domain.js:475:12) at ClientRequest.eventHandlers.<computed> (/var/task/node_modules/rest-adapter/node_modules/follow-redirects/index.js:14:24) at ClientRequest.emit (events.js:400:28) at ClientRequest.emit (domain.js:475:12) at TLSSocket.socketErrorListener (_http_client.js:475:9) at TLSSocket.emit (events.js:400:28) at TLSSocket.emit (domain.js:475:12)

Not sure what is the exact issue.

close the proxy servers, it works for me

“axios”: “^1.6.2”, also has this issues image

getting the same error with axios "axios": "^1.4.0"

AxiosError: read ECONNRESET at AxiosError.from (/home/cares/2wt-mpc-integration-service/node_modules/axios/dist/node/axios.cjs:836:14) at RedirectableRequest.handleRequestError (/home/cares/2wt-mpc-integration-service/node_modules/axios/dist/node/axios.cjs:3010:25) at RedirectableRequest.emit (node:events:513:28) at eventHandlers.<computed> (/home/cares/2wt-mpc-integration-service/node_modules/follow-redirects/index.js:14:24) at ClientRequest.emit (node:events:513:28) at Socket.socketErrorListener (node:_http_client:502:9) at Socket.emit (node:events:513:28) at emitErrorNT (node:internal/streams/destroy:151:8) at emitErrorCloseNT (node:internal/streams/destroy:116:3) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { syscall: ‘read’, code: ‘ECONNRESET’, errno: -104, config: { transitional: { silentJSONParsing: true, forcedJSONParsing: true, clarifyTimeoutError: false }, adapter: [ ‘xhr’, ‘http’ ], transformRequest: [ [Function: transformRequest] ], transformResponse: [ [Function: transformResponse] ], timeout: 0, xsrfCookieName: ‘XSRF-TOKEN’, xsrfHeaderName: ‘X-XSRF-TOKEN’, maxContentLength: -1, maxBodyLength: -1, env: { FormData: [Function], Blob: [class Blob] }, validateStatus: [Function: validateStatus], headers: AxiosHeaders { Accept: ‘application/json, text/plain, /’, Authorization: ‘TurGjZGLlVol’, ‘User-Agent’: ‘axios/1.4.0’, ‘Accept-Encoding’: ‘gzip, compress, deflate, br’ }, method: ‘get’, url: ‘http://<host-ip>/api/v1/programs/1/patients/211898’, data: undefined }, request: <ref *1> Writable { _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: false, ending: false, ended: false, finished: false, destroyed: false, decodeStrings: true, defaultEncoding: ‘utf8’, length: 0, writing: false, corked: 0, sync: true, bufferProcessing: false, onwrite: [Function: bound onwrite], writecb: null, writelen: 0, afterWriteTickInfo: null, buffered: [], bufferedIndex: 0, allBuffers: true, allNoop: true, pendingcb: 0, constructed: true, prefinished: false, errorEmitted: false, emitClose: true, autoDestroy: true, errored: null, closed: false, closeEmitted: false, [Symbol(kOnFinished)]: [] }, _events: [Object: null prototype] { response: [Function: handleResponse], error: [Function: handleRequestError], socket: [Function: handleRequestSocket] }, _eventsCount: 3, _maxListeners: undefined, _options: { maxRedirects: 21, maxBodyLength: Infinity, protocol: ‘http:’, path: ‘/api/v1/programs/1/patients/211898’, method: ‘GET’, headers: [Object: null prototype], agents: [Object], auth: undefined, family: undefined, lookup: undefined, beforeRedirect: [Function: dispatchBeforeRedirect], beforeRedirects: [Object], hostname: ‘192.168.7.203’, port: ‘3005’, agent: undefined, nativeProtocols: [Object], pathname: ‘/api/v1/programs/1/patients/211898’ }, _ended: true, _ending: true, _redirectCount: 0, _redirects: [], _requestBodyLength: 0, _requestBodyBuffers: [], _onNativeResponse: [Function (anonymous)], _currentRequest: ClientRequest { _events: [Object: null prototype], _eventsCount: 7, _maxListeners: undefined, outputData: [], outputSize: 0, writable: true, destroyed: false, _last: true, chunkedEncoding: false, shouldKeepAlive: false, maxRequestsOnConnectionReached: false, _defaultKeepAlive: true, useChunkedEncodingByDefault: false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, strictContentLength: false, _contentLength: 0, _hasBody: true, _trailer: ‘’, finished: true, _headerSent: true, _closed: false, socket: [Socket], _header: ‘GET /api/v1/programs/1/patients/211898 HTTP/1.1\r\n’ + ‘Accept: application/json, text/plain, /\r\n’ + ‘Authorization: TurGjZGLlVol\r\n’ + ‘User-Agent: axios/1.4.0\r\n’ + ‘Accept-Encoding: gzip, compress, deflate, br\r\n’ + ‘Host: <host-ip>\r\n’ + ‘Connection: close\r\n’ + ‘\r\n’, _keepAliveTimeout: 0, _onPendingData: [Function: nop], agent: [Agent], socketPath: undefined, method: ‘GET’, maxHeaderSize: undefined, insecureHTTPParser: undefined, joinDuplicateHeaders: undefined, path: ‘/api/v1/programs/1/patients/211898’, _ended: false, res: null, aborted: false, timeoutCb: null, upgradeOrConnect: false, parser: null, maxHeadersCount: null, reusedSocket: false, host: ‘192.168.7.203’, protocol: ‘http:’, _redirectable: [Circular *1], [Symbol(kCapture)]: false, [Symbol(kBytesWritten)]: 0, [Symbol(kNeedDrain)]: false, [Symbol(corked)]: 0, [Symbol(kOutHeaders)]: [Object: null prototype], [Symbol(errored)]: null, [Symbol(kUniqueHeaders)]: null }, _currentUrl: ‘http://<host-ip>/api/v1/programs/1/patients/211898’, [Symbol(kCapture)]: false }, cause: Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:217:20) { errno: -104, code: ‘ECONNRESET’, syscall: ‘read’ } }

The same feedback was received about a high traffic API. After a week of searching and many failures I managed to identify the reason, in my case the type of communication protocol was generating packet losses, however these losses were not noticeable to the server, there was only one loss and the data arrived compromised.

To resolve this, we adopted the same protocol on the client and server, in my case TLS 1.2, forcing communication with axios, as we can see below:

import https from 'https';

const httpsAgent = new https.Agent({
  maxVersion: 'TLSv1.2',
  minVersion: 'TLSv1.2',
});

export const API = axios.create({
  httpsAgent,
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  baseURL:
  ...
});

By forcing all packets into a single protocol, socket drops and early closures were fixed.

I don’t know if this could help your case.

I think the default on Node JS >= 18 is keepAlive=true. You can disable keepAlive explicitly in Axios to resolve those errors