Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to determine if underlying connection has been killed #1645

Open
phawxby opened this issue Sep 2, 2022 · 3 comments
Open

No way to determine if underlying connection has been killed #1645

phawxby opened this issue Sep 2, 2022 · 3 comments

Comments

@phawxby
Copy link

phawxby commented Sep 2, 2022

In the event that client.quit(); is called I would expected client.status to update to something like disconnecting or closing and then to close once complete. That does not happen.

    console.log(this._redisClient?.status);
    await this._redisClient?.quit();
    console.log(this._redisClient?.status);
    await this._redisClient?.quit();
    console.log(this._redisClient?.status);
    
    OUTPUT
    
ready
ready
[ioredis] Unhandled error event: Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:164:15)
    at writeGeneric (node:internal/stream_base_commons:155:3)
    

The same applies when calling await this._redisClient?.quit(); too.

Incidentally it would be good to get some tsdocs on the statuses so it's obvious what they mean, for example what's the difference between end and close
https://github.com/luin/ioredis/blob/main/lib/Redis.ts#L37-L44

@phawxby
Copy link
Author

phawxby commented Sep 28, 2022

After some investigation it seems the issue is that there is no intermediate state before end. Like reconnecting or connecting there should be an ending status which indicates that the connection is in the process of being closed, up until that point it responds as ready even though it's being killed in an async fashion.

Perhaps disconnect should be awaitable until disconnection has occurred.

@martinffx
Copy link

@luin @phawxby this seems related to: #1362

We've run into this in our Jest tests complaining about open handlers... would be nice to get a fix for this?

@martinffx
Copy link

I did this to cleanly close the client with Fastify.

const registerRedis = (server: FastifyInstance) => {
  const redisClient = new Redis(process.env.REDIS_URL || '', { disconnectTimeout: 100 })
  server.decorate('redis', redisClient)

  server.addHook('onClose', async (instance) => {
    await instance.redis.quit()
    await new Promise((resolve) => {
      instance.redis.disconnect()
      setTimeout(() => { resolve(null) }, 100)
    })
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants