Node.js redis#4 Upgrade: SocketClosedUnexpectedlyError: Socket closed unexpectedly - node.js

I've got some legacy code that I'm upgrading from version 3 of the Node.js redis library to version 4 of the Node.js redis library. The basic shape of the code looks like this
var redis = require('redis')
var client = redis.createClient({
port: '6379',
host: process.env.REDIS_HOST,
legacyMode: true
})
client.connect()
client.flushall(function (err, reply) {
client.hkeys('hash key', function (err, replies) {
console.log("key set done")
client.quit()
})
})
console.log("main done")
When I run this code with redis#4.3.1, I get the following error, and node.js exits with a non-zero status code
main done
key set done
events.js:292
throw er; // Unhandled 'error' event
^
SocketClosedUnexpectedlyError: Socket closed unexpectedly
at Socket.<anonymous> (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/socket.js:182:118)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at TCP.<anonymous> (net.js:673:12)
Emitted 'error' event on Commander instance at:
at RedisSocket.<anonymous> (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/index.js:350:14)
at RedisSocket.emit (events.js:315:20)
at RedisSocket._RedisSocket_onSocketError (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/socket.js:205:10)
at Socket.<anonymous> (/Users/astorm/Documents/redis4/node_modules/#redis/client/dist/lib/client/socket.js:182:107)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at TCP.<anonymous> (net.js:673:12)
While in redis#3.1.2 it runs (minus the client.connect()) without issue.
I've been able to work around this by replacing client.quit() with client.disconnect(), but the actual code is a little more complex than the above example and I'd rather use the graceful shutdown of client.quit than the harsher "SHUT IT DOWN NOW" of client.disconnect().
Does anyone know what the issue here might be? Why is redis#4 failing with a SocketClosedUnexpectedlyError: Socket closed unexpectedly error.

What I found so far is that after a while (keepAlive default is 5 minutes) without any requests the Redis client closes and throws an error event, but if you don't handle this event it will crash your application.
My solution for that was:
/* eslint-disable no-inline-comments */
import type { RedisClientType } from 'redis'
import { createClient } from 'redis'
import { config } from '#app/config'
import { logger } from '#app/utils/logger'
let redisClient: RedisClientType
let isReady: boolean
const cacheOptions = {
url: config.redis.tlsFlag ? config.redis.urlTls : config.redis.url,
}
if (config.redis.tlsFlag) {
Object.assign(cacheOptions, {
socket: {
// keepAlive: 300, // 5 minutes DEFAULT
tls: false,
},
})
}
async function getCache(): Promise<RedisClientType> {
if (!isReady) {
redisClient = createClient({
...cacheOptions,
})
redisClient.on('error', err => logger.error(`Redis Error: ${err}`))
redisClient.on('connect', () => logger.info('Redis connected'))
redisClient.on('reconnecting', () => logger.info('Redis reconnecting'))
redisClient.on('ready', () => {
isReady = true
logger.info('Redis ready!')
})
await redisClient.connect()
}
return redisClient
}
getCache().then(connection => {
redisClient = connection
}).catch(err => {
// eslint-disable-next-line #typescript-eslint/no-unsafe-assignment
logger.error({ err }, 'Failed to connect to Redis')
})
export {
getCache,
}
anyway... in your situation try to handle the error event
client.on('error', err => logger.error(`Redis Error: ${err}`))

Related

Socket closed abruptly during opening handshake while trying to connect to RabbitMq

I have a simple NodeJs application that should connect to RabbitMq.
The code:
const amqp = require('amqplib/callback_api');
const amqpUri = "amqp://user:password#localhost:5672"
if (amqpUri == null)
throw Error('Missing AMQP_URI environment variable.');
amqp.connect(amqpUri, function(error0, connection) {
if (error0)
throw error0;
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
const exchangeName = 'product.event';
const queueName1 = 'create';
const routingKey1 = 'create';
const queueName2 = 'delete';
const routingKey2 = 'delete';
channel.assertExchange(exchangeName, 'topic', {
durable: false,
});
// create
channel.assertQueue(queueName1, {
durable: false,
});
channel.bindQueue(queueName1, exchangeName, routingKey1);
channel.consume(queueName1, (msg) => consumeCreated(channel, msg));
// delete
channel.assertQueue(queueName2, {
durable: false,
});
channel.bindQueue(queueName2, exchangeName, routingKey2);
channel.consume(queueName2, (msg) => consumeDeleted(channel, msg));
});
});
Then run a RabbitMq image with:
docker run -d --hostname my-rabbit --name some-rabbit -p 5672:15672 -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password rabbitmq:3-management
I can access the rabbitmq and connect with the credentials user/password at http://localhost:5672.
For some reason, I have the error:
/home/hamuto/CLO902-Group35/indexer/app.js:12
throw error0;
^
Error: Socket closed abruptly during opening handshake
at Socket.endWhileOpening (/home/hamuto/CLO902-Group35/indexer/node_modules/amqplib/lib/connection.js:260:17)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1241:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
i see this is not likely the case as your using the correct port, however I was getting the exact same error messages when I tried using HTTP port instead of tpc port.
Again I know this isnt your issue but if guess it could help you pinpoint potential failpoints.

Problem connecting postgreSQL with Knex - assert.fail(`unknown message code: ${code.toString(16)}`)

I'm totally new to relational databases and I'm trying to build a node and express project with postgres using knex.
I'm getting the following error when trying to connect to postgres:
/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:202
assert.fail(`unknown message code: ${code.toString(16)}`)
^
AssertionError [ERR_ASSERTION]: unknown message code: 5b
at Parser.handlePacket (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:202:16)
at Parser.parse (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:394:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: undefined,
operator: 'fail'
}
I understand it's a connection problem, but I'm not sure why I'm getting this.
This is my connection code:
export const knex = require('knex')({
client: 'pg',
connection: {
host : 'localhost',
port : 3306,
user : 'notTheRealUser',
password : 'notTheRealPassword',
database : 'pgdb'
}
})
knex.raw("SELECT 1").then(() => {
console.log("PostgreSQL connected")
})
.catch((e: Error) => {
console.log("PostgreSQL not connected")
console.error(e)
})
And then I'm importing the Knex object on the different routes to make queries, like so:
import { knex } from '../db/db'
router.post('/register', async (req: Request, res: Response) => {
// Check if the email isn't already taken
try {
const emailIsTaken = await knex('users').where({ email: req.body.email })
if (emailIsTaken) return res.status(500).json('Email already used')
} catch (err) {
res.status(500).send(err)
console.error(err)
}
})
Full code can be found here: https://github.com/coccagerman/mixr-back
you are using MySQL port 3306,
PostgresQL uses port 5432

Using setInterval with socket.write

This is the code that works but it writes the data just once:
var net = require('net');
var PORT = 3000;
var client = new net.Socket();
client.connect(PORT, function(){
client.write('{printing}');
})
I am looking to write the same thing every few seconds. Wrote the below code but it doesn't seem to work:
client.connect(PORT, function(){
setInterval(function(){
client.write('{ printing }');
},10000);
})
Following is the error that I keep getting:
node:events:355
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at afterWriteDispatched (node:internal/stream_base_commons:160:15)
at writeGeneric (node:internal/stream_base_commons:151:3)
at Socket._writeGeneric (node:net:773:11)
at Socket._write (node:net:785:8)
at writeOrBuffer (node:internal/streams/writable:395:12)
at Socket.Writable.write (node:internal/streams/writable:340:10)
at Timeout._onTimeout (/app/src/index.js:135:14)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:188:8)
at emitErrorCloseNT (node:internal/streams/destroy:153:3)
at processTicksAndRejections (node:internal/process/task_queues:81:21) {
errno: -32,
code: 'EPIPE',
syscall: 'write'
}
[nodemon] app crashed - waiting for file changes before starting..
This is how I fixed it:
client.connect(PORT, function(){
client.write('printing')
})
//adding drain if the buffer gets full
client.on('drain',()=>{
console.log("draining the buffer")
setTimeout(() => {
client.write('printing')
})
//reading the response recieved : ("ok")
client.on('data', (data) => {})
//in case of an error, closing the connection
client.on('error',err => {}).on('close',() => {
setTimeout(() => {
client.connect(PORT, function(){
client.write('printing')
})
},40000)
})
In this context, the EPIPE error probably means that you're trying to write to a socket that has been closed. Since the setInterval() example you show keeps going forever, that probably means that the socket you originally connected gets closed at some point, but your setInterval() is still firing and trying to write to it.
You don't show the overall context of what you're trying to accomplish here to know exactly what to suggest, but at a minimum, you need to call clearInterval() to stop the timer whenever the socket it's trying to write to gets closed, either on purpose or because of error.
Here's an example for how you could debug if this is what is happening to you:
const net = require('net');
const PORT = 3000;
const client = new net.Socket();
let timer;
function disableTimer() {
if (timer) {
clearInterval(timer);
timer = null;
}
}
client.on('error', err => {
console.log("socket error", err);
disableTimer();
}).on('close', () => {
console.log("socket closed");
disableTimer();
});
client.connect(PORT, function(){
timer = setInterval(function(){
client.write('{ printing }');
},10000);
});

Node JS Events Emitter on net.createserver

So i got a problem and stuck around 3 days, i'm using node js as a socket server to receive string and processing some json data to give a string response to socket client, stack used in node js(net, events), my code have no problem for the first request, but after the second request it got an error like this(server):
[2020-12-23T07:45:13.821Z] server listening on port: 4444
[2020-12-23T07:45:15.696Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:15.701Z] message: string
[2020-12-23T07:45:15.702Z] executing call function
[2020-12-23T07:45:15.703Z] oncall function
[2020-12-23T07:45:15.703Z] executing emit
[2020-12-23T07:45:15.704Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:15.707Z] Returning : diterima
[2020-12-23T07:45:42.403Z] New Connection : ::ffff:127.0.0.1
[2020-12-23T07:45:42.407Z] message: string
[2020-12-23T07:45:42.408Z] executing call function
[2020-12-23T07:45:42.408Z] oncall function
[2020-12-23T07:45:42.409Z] executing emit
[2020-12-23T07:45:42.409Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.410Z] Returning : diterima
[2020-12-23T07:45:42.411Z] {
orig: 'orig',
origParam: { number: '123456' },
resp: [
{
content: [Array],
firstPage: true,
lastPage: true,
number: 0,
numberOfElements: 1,
size: 1,
sort: null,
totalElements: 1
},
{ result: 'UNMATCHED' }
]
}
[2020-12-23T07:45:42.412Z] Returning : diterima
events.js:288
throw er; // Unhandled 'error' event
^
Error: This socket has been ended by the other party
at Socket.writeAfterFIN [as write] (net.js:451:14)
at HanaconsResponded.<anonymous> (C:\Users\ralfian\--\--\--.js:47:11)
at HanaconsResponded.emit (events.js:323:22)
at callFunction (C:\Users\ralfian\--\--\--.js:79:23)
at Socket.<anonymous> (C:\Users\ralfian\--\--\--.js:35:17)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:271:13)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on Socket instance at:
at emitErrorNT (net.js:1336:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EPIPE'
}
and bellow is error on the client side at second request, (first request have no problem):
CLIENT: I connected to the server.
{"orig":"orig","origParam":{"number":"123456"},"resp":[{"content":[{"gender":"not match"}],"firstPage":true,"lastPage":true,"number":0,"numb
erOfElements":1,"size":1,"sort":null,"totalElements":1},{"result":"UNMATCHED"}]}
events.js:288
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
Here is my server Code:
var net = require('net');
var server = net.createServer();
require('log-timestamp');
var hostport = 4444;
var EventEmitter = require('events');
class CheckResponded extends EventEmitter {}
var checkResponded = new CheckResponded();
server.listen({
port : hostport,
exclusive: true
},);
console.log('server listening on ' + 'port: ' + hostport);
server.on('connection', (e) => {
console.log( 'New Connection : ' + e.remoteAddress );
e.setEncoding('utf8');
e.setTimeout(60000);
e.on('end', () => {})
e.on( 'timeout', () => {
console.log('Socket Timeout. Reseting.');
e.end();
});
e.on( 'data', (buff) => {
console.log("message: " + typeof buff);
try {
if (buff === "exec"){
console.log("executing call function")
callFunction()
}
} catch(error){
console.log('error on socket -> ' + error)
}
});
checkResponded.on( 'event1', (f) => {
console.log(f)
var rmsg;
rmsg = f;
console.log("Returning : " + 'diterima');
e.write(JSON.stringify(rmsg));
});
});
function callFunction() {
console.log('oncall function')
var imsg = {
"orig": "orig",
"origParam": {
"number": "123456",
},
"resp": [
{
"content": [
{
"gender": "not match",
}
],
"firstPage": true,
"lastPage": true,
"number": 0,
"numberOfElements": 1,
"size": 1,
"sort": null,
"totalElements": 1
},
{
"result": "UNMATCHED"
}
]
}
console.log('executing emit')
checkResponded.emit('event1', imsg);
return
}
and bellow is the client request example:
const net = require('net');
var host = 'localhost';
const client = net.createConnection({ port: 4444, host: host }, () => {
console.log('CLIENT: I connected to the server.');
client.write('exec')
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('CLIENT: I disconnected from the server.');
});
looking for help , Thanks!
One thing I see is that your client code assumes that your entire response arrives in one data event. That is not a safe assumption. It may sometimes happen that way, but it is not guaranteed and if the server is not done sending data when you call client.end(), you will get an error (like you are) on the server such as:
Error: This socket has been ended by the other party
You will need some sort of protocol in the data you send so that you can parse the incoming data and you can then know when you have a complete response and when you need to wait for the rest of the data to arrive.
Similar issue discussed here: Detect complete data received on 'data' Event in Net module of Node.js

Socket Hangup Error In Node JS On Force API Timeout

I am using request module in Node JS (v8.12) to call a third party API. Since the API is not very reliable and due to lack of better option I am timing out the call after 2 seconds in case if there is no response from the API. But in doing so it creates a socket hang up error. Below is the code used and stack trace
const options = {
url: resource_url,
rejectUnauthorized: false,
timeout: 2000,
method: 'GET',
headers: {
'content-Type': 'application/json',
}
};
return new Promise(function (resolve, reject) {
request(options, function (err, res, body) {
if (!err) {
resolve(JSON.parse(body.data));
} else {
if (err.code === 'ETIMEDOUT' || err.code == 'ESOCKETTIMEDOUT') {
resolve(someOldData);
} else {
resolve(someOldData);
}
}
});
});
Error: socket hang up
at createHangUpError (_http_client.js:331:15)
at TLSSocket.socketCloseListener (_http_client.js:363:23)
at scope.activate (<trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/base.js:54:19)
at Scope._activate (<trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/async_hooks.js:51:14)
at Scope.activate (<trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/base.js:12:19)
at TLSSocket.bound (<trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/base.js:53:20)
at emitOne (events.js:121:20)
at TLSSocket.emit (events.js:211:7)
at _handle.close (net.js:554:12)
at TCP.done [as _onclose] (_tls_wrap.js:356:7)
After doing a bit of reading and research I found this article pointing out a similar issue so I switched to http module as mentioned in one of the solution in the article. But switching to http module also did not resolve the issue. Below is code implementation using http and stack trace.
let responseData;
const requestOptions = {
hostname: resource_host,
path: resource_path,
method: 'GET',
timeout: 2000,
};
return new Promise((resolve, reject) => {
const requestObject = http.request(requestOptions, (responseObj) => {
responseObj.setEncoding('utf8');
responseObj.on('data', (body) => {
responseData = body;
});
responseObj.on('end', () => {
resolve(responseData);
});
});
requestObject.on('error', (err) => {
responseData = someOldData;
resolve(responseData);
});
requestObject.on('timeout', () => {
responseData = someOldData;
requestObject.abort();
});
requestObject.end();
});
Error: socket hang up
at connResetException (internal/errors.js:608:14)
at Socket.socketCloseListener (_http_client.js:400:25)
at <trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/base.js:54:19
at Scope._activate (<trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/async_hooks.js:51:14)
at Scope.activate (<trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/base.js:12:19)
at Socket.bound (<trace-log-base-path>/dd-trace/packages/dd-trace/src/scope/base.js:53:20)
at Socket.emit (events.js:322:22)
at Socket.EventEmitter.emit (domain.js:482:12)
at TCP.<anonymous> (net.js:672:12)
I went through multiple SO post and various other resources over the web, but I am unable to resolve this issue.
Could it be because of the third party, because I also tried to reproduce the issue by creating a dummy server which sleeps for some time after the request is fired and timing out that request but was unable to reproduce the issue.
I'll be very grateful for any help in this regards.
Removing requestObject.abort() in timeout event block when using http module resolves this issue.

Resources