moleculerjs/moleculer

moleculer crash if ioredis can not change state to "ready"

0x0a0d opened this issue · 1 comments

0x0a0d commented

Today, I accidentally entered the wrong Redis server address, which was actually another TCP service address, resulting in the application crashing
After hours of debugging, I see that It was happen because on the Redis transporter class

clientSub.on("connect", () => {

and
clientPub.on("connect", () => {

"connect" event not tell the redis client is "ready", just emits when a connection is established to the Redis server.
@icebob you can try with this code

const net = require('net')
const { ServiceBroker } = require('moleculer')

const localhost = '127.0.0.1'
const server = net.createServer()
server.on('connection', socket => {
  socket.end()
})
server.listen(0, localhost, () => {
  const broker = new ServiceBroker({
    transporter: `redis://${localhost}:${server.address().port}}`
  })
  broker.start().then(() => {
    console.log('Broker started.')
  }).catch(err => {
    console.error(err)
  })
})

After a moment, it will crash with error
image

I found just 1 way to keep moleculer does not crash that is changing line 45,51 of redis transporter class to

// line 45
clientSub.on("ready", () => { // was: clientSub.on("connect", () => {
// line 51
clientPub.on("ready", () => { // was: clientPub.on("connect", () => {
icebob commented

Nice catch