diff options
author | Binbin <binloveplay1314@qq.com> | 2022-01-04 20:45:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-04 14:45:10 +0200 |
commit | c57e41c029ba45b0ebb3fdd9474e202cdef92767 (patch) | |
tree | 1627043175c273d294cf51d915e784d084b5f893 /src/redis-cli.c | |
parent | 747b08bee0cc1e481b9bd08acb881bc5b9ab18d9 (diff) | |
download | redis-c57e41c029ba45b0ebb3fdd9474e202cdef92767.tar.gz |
Print error messages in monitor/pubsub when errors occurs (#10050)
In monitor/pubsub mode, if the server closes the connection,
for example, use `CLIENT KILL`, redis-cli will exit directly
without printing any error messages.
This commit ensures that redis-cli will try to print the
error messages before exiting. Also there is a minor cleanup
for restart, see the example below.
before:
```
127.0.0.1:6379> monitor
OK
[root@ redis]#
127.0.0.1:6379> subscribe channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel"
3) (integer) 1
[root@ redis]#
127.0.0.1:6379> restart
127.0.0.1:6379> get keyUse 'restart' only in Lua debugging mode.
(nil)
```
after:
```
127.0.0.1:6379> monitor
OK
Error: Server closed the connection
[root@ redis]#
127.0.0.1:6379> subscribe channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel"
3) (integer) 1
Error: Server closed the connection
[root@ redis]#
127.0.0.1:6379> restart
Use 'restart' only in Lua debugging mode.
```
Diffstat (limited to 'src/redis-cli.c')
-rw-r--r-- | src/redis-cli.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/redis-cli.c b/src/redis-cli.c index 94697f99f..0a19c025d 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -1415,7 +1415,10 @@ static int cliSendCommand(int argc, char **argv, long repeat) { redisAppendCommandArgv(context,argc,(const char**)argv,argvlen); if (config.monitor_mode) { do { - if (cliReadReply(output_raw) != REDIS_OK) exit(1); + if (cliReadReply(output_raw) != REDIS_OK) { + cliPrintContextError(); + exit(1); + } fflush(stdout); /* This happens when the MONITOR command returns an error. */ @@ -1434,7 +1437,10 @@ static int cliSendCommand(int argc, char **argv, long repeat) { redisSetPushCallback(context, NULL); while (config.pubsub_mode) { - if (cliReadReply(output_raw) != REDIS_OK) exit(1); + if (cliReadReply(output_raw) != REDIS_OK) { + cliPrintContextError(); + exit(1); + } fflush(stdout); /* Make it grep friendly */ if (!config.pubsub_mode || config.last_cmd_type == REDIS_REPLY_ERROR) { if (config.push_output) { @@ -2271,7 +2277,8 @@ static void repl(void) { linenoiseFree(line); return; /* Return to evalMode to restart the session. */ } else { - printf("Use 'restart' only in Lua debugging mode."); + printf("Use 'restart' only in Lua debugging mode.\n"); + fflush(stdout); } } else if (argc == 3 && !strcasecmp(argv[0],"connect")) { sdsfree(config.conn_info.hostip); |