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

Cannot connect after quit command #928

Closed
manast opened this issue Jul 14, 2019 · 2 comments · Fixed by #929
Closed

Cannot connect after quit command #928

manast opened this issue Jul 14, 2019 · 2 comments · Fixed by #929

Comments

@manast
Copy link

manast commented Jul 14, 2019

Trying to connect after calling quit results in the following exception:

Error: Connection is closed.
    at Redis.connectionCloseHandler (/Users/manuelastudillo/Dev/taskforce/bull/node_modules/ioredis/built/redis/index.js:295:24)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Redis.emit (events.js:211:7)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

How to reproduce:

async function test() {
  const IORedis = require('ioredis');

  const client = new IORedis();

  client.on('end', async () => {
    console.log('end');
    try {
      await client.connect();
    } catch (err) {
      console.error(err);
    }
  });

  client.on('error', async err => {
    console.error(err);
  });

  await client.quit();
}

test();

From what I can understand from the code it seems that the status property is not properly updated in the case of quit (or disconnect btw).

@luin luin added the bug label Jul 14, 2019
luin added a commit that referenced this issue Jul 14, 2019
@luin
Copy link
Collaborator

luin commented Jul 14, 2019

Thank you for pointing this out. This is a bug happening when connecting immediately after "end", thus changing:

    try {
      await client.connect();
    } catch (err) {
      console.error(err);
    }

to

    setTimeout(() => {
      client.connect();
    }, 0)

Will avoid the reconnecting and will be able to connect successfully.

This issue is caused by the handlers for the events emitted from the previous connection being executed during the next connection being made.

I created a pull request to fix this issue by introducing a connection epoch to distinguish different connections.

@luin luin closed this as completed in #929 Jul 15, 2019
luin added a commit that referenced this issue Jul 15, 2019
Introduce a connection epoch to distinguish different connections.

Close #928
ioredis-robot pushed a commit that referenced this issue Jul 15, 2019
## [4.12.1](v4.12.0...v4.12.1) (2019-07-15)

### Bug Fixes

* handle connecting immediately after "end" event ([#929](#929)) ([7bcd8a8](7bcd8a8)), closes [#928](#928)
@ioredis-robot
Copy link
Collaborator

🎉 This issue has been resolved in version 4.12.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment