RequestError: Error: Client network socket disconnected before secure TLS connection was established when Node version set to 10x - node.js

We are using AWS Lambda to call our API's. We recently upgraded our Lambda Node version from 8.10 to Node 10.x version. We have added process event for unhandled rejection and also written code in try-catch block but none of the code resolves our issue. Please find the error block that we are getting when we hit the API through Lambda.
Please note that we are using promise-request NPM package for request.
Tried code with Try catch and handling unhandled event using Process.event
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "RequestError: Error: Client network socket disconnected before secure TLS connection was established",
"stack": [
"Runtime.UnhandledPromiseRejection: RequestError: Error: Client network socket disconnected before secure TLS connection was established",
" at process.on (/var/runtime/index.js:37:15)",
" at process.emit (events.js:203:15)",
" at process.EventEmitter.emit (domain.js:448:20)",
" at /var/task/node_modules/bluebird/js/release/debuggability.js:199:33",
" at activeFireEvent (/var/task/node_modules/bluebird/js/release/debuggability.js:242:44)",
" at fireRejectionEvent (/var/task/node_modules/bluebird/js/release/debuggability.js:632:14)",
" at Promise._notifyUnhandledRejection (/var/task/node_modules/bluebird/js/release/debuggability.js:65:9)",
" at Timeout._onTimeout (/var/task/node_modules/bluebird/js/release/debuggability.js:44:14)",
" at ontimeout (timers.js:436:11)",
" at tryOnTimeout (timers.js:300:5)",
" at listOnTimeout (timers.js:263:5)",
" at Timer.processTimers (timers.js:223:10)"
],
"reason": {
"errorType": "RequestError",
"errorMessage": "Error: Client network socket disconnected before secure TLS connection was established",
"stack": [
"RequestError: Error: Client network socket disconnected before secure TLS connection was established",
" at new RequestError (/var/task/node_modules/request-promise-core/lib/errors.js:14:15)",
" at Request.plumbing.callback (/var/task/node_modules/request-promise-core/lib/plumbing.js:87:29)",
" at Request.RP$callback [as _callback] (/var/task/node_modules/request-promise-core/lib/plumbing.js:46:31)",
" at self.callback (/var/task/node_modules/request/request.js:185:22)",
" at Request.emit (events.js:198:13)",
" at Request.EventEmitter.emit (domain.js:448:20)",
" at Request.onRequestError (/var/task/node_modules/request/request.js:881:8)",
" at ClientRequest.emit (events.js:198:13)",
" at ClientRequest.EventEmitter.emit (domain.js:448:20)",
" at TLSSocket.socketErrorListener (_http_client.js:392:9)",
" at TLSSocket.emit (events.js:198:13)",
" at TLSSocket.EventEmitter.emit (domain.js:448:20)",
" at emitErrorNT (internal/streams/destroy.js:91:8)",
" at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)",
" at process._tickCallback (internal/process/next_tick.js:63:19)"
],
}
}

Upgrading to Node.js 12.x, which is possible now, avoids the issue.

Related

How to send record to Kinesis in fire-and-forget mode with AWS SDK v3 for NodeJS

today I'm able to put a record in Kinesis, but this operation takes 30 ms (which is too much for my use case), and I would like to not wait for the kinesis response (= fire-and-forget)
Today I use the PutRecordCommand solution:
const putRecordCommand = new PutRecordCommand({
Data: Buffer.from(JSON.stringify(report)),
PartitionKey: requestId,
StreamName: STREAM_NAME,
});
await kinesisClient.send(putRecordCommand);
If I remove the await key word, and the lambda ends before the kinesis client retrieves a response, I get the following error:
{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "Error [ERR_HTTP2_STREAM_CANCEL]: The pending stream has been canceled (caused by: Client network socket disconnected before secure TLS connection was established)",
"reason": {
"errorType": "Error",
"errorMessage": "The pending stream has been canceled (caused by: Client network socket disconnected before secure TLS connection was established)",
"code": "ERR_HTTP2_STREAM_CANCEL",
"cause": {
"errorType": "Error",
"errorMessage": "Client network socket disconnected before secure TLS connection was established",
"code": "ECONNRESET",
"host": "kinesis.eu-west-1.amazonaws.com",
"port": "443",
"stack": [
"Error: Client network socket disconnected before secure TLS connection was established",
" at connResetException (node:internal/errors:692:14)",
" at TLSSocket.onConnectEnd (node:_tls_wrap:1587:19)",
" at TLSSocket.emit (node:events:539:35)",
" at endReadableNT (node:internal/streams/readable:1345:12)",
" at processTicksAndRejections (node:internal/process/task_queues:83:21)"
]
},
"$metadata": {
"attempts": 1,
"totalRetryDelay": 0
},
"stack": [
"Error [ERR_HTTP2_STREAM_CANCEL]: The pending stream has been canceled (caused by: Client network socket disconnected before secure TLS connection was established)",
" at new NodeError (node:internal/errors:372:5)",
" at closeSession (node:internal/http2/core:1136:20)",
" at ClientHttp2Session.destroy (node:internal/http2/core:1530:5)",
" at TLSSocket.socketOnError (node:internal/http2/core:2981:13)",
" at TLSSocket.emit (node:events:527:28)",
" at emitErrorNT (node:internal/streams/destroy:157:8)",
" at emitErrorCloseNT (node:internal/streams/destroy:122:3)",
" at processTicksAndRejections (node:internal/process/task_queues:83:21)"
]
},
"promise": {},
"stack": [
"Runtime.UnhandledPromiseRejection: Error [ERR_HTTP2_STREAM_CANCEL]: The pending stream has been canceled (caused by: Client network socket disconnected before secure TLS connection was established)",
" at process.<anonymous> (file:///var/runtime/index.mjs:775:15)",
" at process.emit (node:events:527:28)",
" at emit (node:internal/process/promises:140:20)",
" at processPromiseRejections (node:internal/process/promises:274:27)",
" at processTicksAndRejections (node:internal/process/task_queues:97:32)"
]
}
Questions:
Is there another Kinesis "command" from AWS SDK v3 that allows me to fire-and-forget?
Or an option to use on the kinesis client in order to make it work without await?
Notes:
Env: AWS Lambda with NodeJS v16
Dependency: AWS SDK v3 v3.154
async/await programming style

My lambda function randomly crashes and throws a socket hang up error

I have serverless API which saves and fetch data in Redis mostly it runs fine but randomly throws 502 error (socket hang up) which I can't even handle inside the lambda.
"errorType": "Error",
"errorMessage": "socket hang up",
"code": "ECONNRESET",
"stack": [
"Error: socket hang up",
" at createHangUpError (_http_client.js:323:15)",
" at TLSSocket.socketOnEnd (_http_client.js:426:23)",
" at TLSSocket.emit (events.js:203:15)",
" at endReadableNT (_stream_readable.js:1143:12)",
" at process._tickCallback (internal/process/next_tick.js:63:19)"
]
}```
I am using:
Nodejs 12.x
Redis: 2.8.0
AWS Lambda functions have a timeout of 3-900 seconds, with 3 seconds being the default. If your Lambda does not have the timeout configured, and it takes more than 3 seconds to execute, you will get these errors.
https://docs.aws.amazon.com/lambda/latest/dg/resource-model.html

Intermittent redis connection error: 'UNCERTAIN STATE'

Time and time again, we get this error:
{ Error: Redis connection to some-redis-server:6379 failed - read ETIMEDOUT
at TCP.onStreamRead (internal/stream_base_commons.js:171:27) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'read' }
{ AbortError: Redis connection lost and command aborted. It might have been processed.
at RedisClient.flush_and_error (/usr/src/app/node_modules/redis/index.js:362:23)
at RedisClient.connection_gone (/usr/src/app/node_modules/redis/index.js:664:14)
at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:289:14)
at Object.onceWrapper (events.js:281:20)
at Socket.emit (events.js:193:13)
at TCP._handle.close (net.js:614:12)
code: 'UNCERTAIN_STATE',
command: 'GET'
args: [ 'some-key' ] }
We are using https://www.npmjs.com/package/redis to access redis.
Some details of the setup:
Currently 25GB of data stored in Redis
Writes = a rate of approximately 2MB/sec.
Max number of concurrent clients connected: 400
I don't know where to really start looking, so any help would be appreciated.

Node.js, ws-tcp-relay with nats: connection error

Please help solve the problem:
When I start the nats server listening 4222 port:
./gnatsd --addr localhost --port 4222, then
./ws-tcp-relay localhost:4222 -p 4223 and try to connect through websockets using websocket-nats like this (in service.js for example):
'use strict';
const nats = require('websocket-nats').connect('ws://localhost:4223');
I get this error:
node server.js
events.js:188
throw err;
^
Error: Unhandled "error" event. (Could not connect to server: Error: getaddrinfo ENOTFOUND localhost:4223 localhost:4223:4223)
at Client.emit (events.js:186:19)
at Socket.<anonymous> (/home/dev/projects/node-pub/node_modules/websocket-nats/lib/nats.js:404:14)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
But, if I try to connect to nats directly using tcp client port 4222, the connection establishes succesfull:
const nats = require('nats').connect('nats://localhost:4222');

Bad Request at respond node_modules/elasticsearch/src/lib/transport.js:264:15

I am very dummy on ElasticSearch and I have little experience with NodeJs. I have created few cruds using NodeJs + MongoDb. I have to change from MongoDb to ElasticSearch (this is mandatory). Please, I don't want to compare MOngoDb with ElasticSearch so don't try that.
I am following the example found here
https://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/
I have started succesfully ElasticSearch. I can see:
nodejs express.js-elasticsearch-autocomplete-demo-master # curl --noproxy -XGET 'http://127.0.0.1:9200/?pretty'
{
"name" : "-em7X-s",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "cqJUaPcFREe4wX_YyYWoFg",
"version" : {
"number" : "5.2.1",
"build_hash" : "db0d481",
"build_date" : "2017-02-09T22:05:32.386Z",
"build_snapshot" : false,
"lucene_version" : "6.4.1"
},
"tagline" : "You Know, for Search"
}
However, when I try to start the sample above or access I get the error bellow respectvely:
/dev/samples/express.js-elasticsearch-autocomplete-demo-master $ npm start
> autocompleter#0.0.0 start /home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master
> node ./bin/www
Elasticsearch INFO: 2017-02-16T20:00:48Z
Adding connection to http://localhost:9200/
Unhandled rejection Error: Bad Request
at respond (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/transport.js:264:15)
at checkRespForFailure (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/transport.js:223:7)
at HttpConnector.<anonymous> (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/connectors/http.js:155:7)
at IncomingMessage.wrapper (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/lodash/index.js:3095:19)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at nextTickCallbackWith2Args (node.js:441:9)
at process._tickCallback (node.js:355:17)
Unhandled rejection Error: [illegal_argument_exception] request [/randomindex/_suggest] contains unrecognized parameter: [type]
at respond (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/transport.js:264:15)
at checkRespForFailure (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/transport.js:223:7)
at HttpConnector.<anonymous> (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/connectors/http.js:155:7)
at IncomingMessage.wrapper (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/lodash/index.js:3095:19)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at nextTickCallbackWith2Args (node.js:441:9)
at process._tickCallback (node.js:355:17)
Unhandled rejection Error: [illegal_argument_exception] request [/randomindex/_suggest] contains unrecognized parameter: [type]
at respond (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/transport.js:264:15)
at checkRespForFailure (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/transport.js:223:7)
at HttpConnector.<anonymous> (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/elasticsearch/src/lib/connectors/http.js:155:7)
at IncomingMessage.wrapper (/home/demetrio/dev/samples/express.js-elasticsearch-autocomplete-demo-master/node_modules/lodash/index.js:3095:19)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at nextTickCallbackWith2Args (node.js:441:9)
at process._tickCallback (node.js:355:17)
I am very confused what to check. Any suggestion will be appreciated. My final target is to understand how to connect to ElasticSearch from NodeJs application

Resources