Skip to content

Fix redis-cli exit after long idle #2066

Closed

Description

@mattsta

redis-cli has a persistent problem where it will just exit on the next command when left idle for too long. It should reconnect instead.

I haven't been able to track down why it happens yet. redis-cli has many exit points (see end) and even with inserting logging before each exit or return, it still just exits without triggering any of the logs/breakpoints.

The failure looks like:

127.0.0.1:26379> sentinel masters
matt@ununoctium:~/repos/redis/src%

It's just an immediate drop out of redis-cli back to a command prompt.

Easy to reproduce: open redis-cli to an active server, leave it idle for a few hours (or over night), then try to issue any command. Instead of sending the command, redis-cli just exits.

(exit points):

% grep "exit(1)" redis-cli.c |wc -l
      49

Plus, many of the return; statements can cause an exit too if we are returning from the event loop.

Activity

  1. antirez commented on Oct 14, 2014

    @antirez
    Contributor

    Hello Matt, it always happens to me, it is really a pain. Following your advices to reproduce, let's check if I'll be lucky enough to find the issue in a reasonable amount of time.

  2. antirez commented on Oct 14, 2014

    @antirez
    Contributor

    A suspicious code path could be: cliPrintContextError() + exit().
    If the context is NULL, this is where you can get a silent exit...

    Btw I tried to reproduce the issue without waiting 3 hours, without success so far ;-)

  3. mattsta commented on Oct 14, 2014

    @mattsta
    ContributorAuthor

    Yeah, it's very tricky to reproduce without waiting 5+ hours. Plus, when you test it and it does work, any timers are probably reset!

    Maybe open 10 different redis-cli instances and test them incrementally? :) Then at least one failed test won't reset time back to zero for the rest.

  4. antirez commented on Oct 14, 2014

    @antirez
    Contributor

    Ok, this is where I think the issue is:

            if (config.interactive) {
                /* Filter cases where we should reconnect */
                if (context->err == REDIS_ERR_IO && errno == ECONNRESET)
                    return REDIS_ERR;
                if (context->err == REDIS_ERR_EOF)
                    return REDIS_ERR;
            }
            cliPrintContextError();
            exit(1);
    

    The cliReadReply() call gets a different error from some reason after a long wait, so exits... and context is NULL so no error is printed.

  5. antirez commented on Oct 14, 2014

    @antirez
    Contributor

    No sorry, it's not NULL, so the exit should still not be totally silent. Argh! :-)

  6. antirez commented on Oct 15, 2014

    @antirez
    Contributor

    @mattsta yes opening 10 should definitely work! And maybe set a break point to the exit syscall in the debugger. I'll try today, thanks.

  7. mattsta commented on Oct 15, 2014

    @mattsta
    ContributorAuthor

    More data: Just had a failure (no lldb hooked up to this one though, doh), but the return value indiciates a failure due to SIGPIPE:

    127.0.0.1:6379> set a b
    matt@ununoctium:~/repos/redis/src% echo $?
    141

    (See things like http://stackoverflow.com/questions/18880606/socket-connection-getting-closed-abruptly-with-code-141)

    So, we can track down where the SIGPIPE happens or just SIG_IGN SIGPIPE :)

  8. antirez commented on Oct 15, 2014

    @antirez
    Contributor

    Bingo! You nailed it. Redis itself does signal(SIGPIPE, SIG_IGN);.

    You tracked it, your is the honor of committing the fix :-)

  9. Plasma commented on Oct 15, 2014

    @Plasma

    This happens to me, I always assumed its because we have idle timeout set on all redis connections and so unless I'm sending comments often in the CLI it will eventually time out.

  10. antirez commented on Oct 16, 2014

    @antirez
    Contributor

    @Plasma redis-cli is designed to try to reconnect forever :-) Should never exit. But finally we know what it is. The problem remained there forever since is timeout-ish alike as you already told, and because it only happens after hours. So it needed some focus to track it down.

  11. mattsta commented on Oct 16, 2014

    @mattsta
    ContributorAuthor

    Just caught it in the debugger too:

    * thread #1: tid = 0x2aed6f, 0x0000000108a4c01e redis-cli`redisBufferWrite(c=0x00007f8842e00130, done=0x00007fff571b838c) + 46 at hiredis.c:1179, queue = 'com.apple.main-thread', stop reason = step in
        frame #0: 0x0000000108a4c01e redis-cli`redisBufferWrite(c=0x00007f8842e00130, done=0x00007fff571b838c) + 46 at hiredis.c:1179
       1176         return REDIS_ERR;
       1177 
       1178     if (sdslen(c->obuf) > 0) {
    -> 1179         nwritten = write(c->fd,c->obuf,sdslen(c->obuf));
       1180         if (nwritten == -1) {
       1181             if ((errno == EAGAIN && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {
       1182                 /* Try again later */
    (lldb)  
    Process 62104 stopped
    * thread #1: tid = 0x2aed6f, 0x00007fff93189f92 libsystem_kernel.dylib`write + 10, queue = 'com.apple.main-thread', stop reason = signal SIGPIPE
        frame #0: 0x00007fff93189f92 libsystem_kernel.dylib`write + 10
    libsystem_kernel.dylib`write + 10:
    -> 0x7fff93189f92:  jae    0x7fff93189f9c            ; write + 20
       0x7fff93189f94:  movq   %rax, %rdi
       0x7fff93189f97:  jmp    0x7fff9318519a            ; cerror
       0x7fff93189f9c:  retq   
  12. mattsta commented on Oct 16, 2014

    @mattsta
    ContributorAuthor

    Oh, and with signal(SIGPIPE, SIG_IGN);, now we get:

    127.0.0.1:6379> get a
    Error: Broken pipe
    matt@ununoctium:~/repos/redis/src% 

    But, redis-cli.c does have a section to check for connection problems then just re-try:

                if (context->err == REDIS_ERR_IO && errno == ECONNRESET)

    We can just throw in a check for errno == EPIPE too and redis-cli should re-connect just like a regular connection loss.

    So, I've added an EPIPE check there and I'm testing that fix today (third attempt!). Hopefully now we've found everything needed to stop automatic exits.

  13. mattsta commented on Oct 18, 2014

    @mattsta
    ContributorAuthor

    Finally found proof the fix works.

    Here is the error being caught (so we can continue instead of exiting):

    Process 78300 stopped
    * thread #1: tid = 0x36df39, 0x000000010065fe37 redis-cli`cliReadReply(output_raw_strings=<unavailable>) + 199 at redis-cli.c:527, queue = 'com.apple.main-thread', stop reason = step in
        frame #0: 0x000000010065fe37 redis-cli`cliReadReply(output_raw_strings=<unavailable>) + 199 at redis-cli.c:527
       524          }
       525          if (config.interactive) {
       526              /* Filter cases where we should reconnect */
    -> 527              if (context->err == REDIS_ERR_IO &&
       528                  (errno == ECONNRESET || errno == EPIPE))
       529                  return REDIS_ERR;
       530              if (context->err == REDIS_ERR_EOF)
    (lldb)  

    And here's the reconnect happening after the failure above:

    Process 78300 stopped
    * thread #1: tid = 0x36df39, 0x000000010065f301 redis-cli`main [inlined] repl + 369 at redis-cli.c:941, queue = 'com.apple.main-thread', stop reason = step in
        frame #0: 0x000000010065f301 redis-cli`main [inlined] repl + 369 at redis-cli.c:941
       938                          if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
       939                              != REDIS_OK)
       940                          {
    -> 941                              cliConnect(1);
       942  
       943                              /* If we still cannot send the command print error.
       944                               * We'll try to reconnect the next time. */
    (lldb)  

    No more spontaneous exits due to uncaught signals or bad return values!

  14. added a commit that references this issue on Oct 29, 2014
    e10c544
  15. antirez commented on Oct 30, 2014

    @antirez
    Contributor

    Thanks Matt! Merging.

  16. added 2 commits that reference this issue on Oct 30, 2014
    eb67ca0
    e8f7899
  17. Fry-kun commented on Dec 8, 2014

    @Fry-kun

    I'm often using actual shell pipe, e.g. "redis-cli -p ... monitor | head -n20" to see limited number of live requests on the redis server.
    Pretty sure this fix of yours broke that functionality -- now instead of quitting after 20 lines, redis-cli just sits there until killed with another signal.
    Should I create a new bug or can this one be reopened?

  18. badboy commented on Dec 8, 2014

    @badboy
    Contributor

    Yes, the patch now completely ignores the SIGPIPE. Maybe we should only ignore it if we're interactive.

  19. Fry-kun commented on Dec 8, 2014

    @Fry-kun

    yes, sounds like reasonable solution

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions