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.
```
This commit is contained in:
Binbin 2022-01-04 20:45:10 +08:00 committed by GitHub
parent 747b08bee0
commit c57e41c029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);