Unverified Commit 8038eb31 authored by Vitah Lin's avatar Vitah Lin Committed by GitHub
Browse files

Fix wrong dbnum showed in redis-cli after client reconnected (#13411)



When the server restarts while the CLI is connecting, the reconnection
does not automatically select the previous db.
This may lead users to believe they are still in the previous db, in
fact, they are in db0.

This PR will automatically reset the current dbnum and `cliSelect()`
again when reconnecting.

---------
Co-authored-by: default avatardebing.sun <debing.sun@redis.com>
parent 89742a95
......@@ -3264,6 +3264,9 @@ static int issueCommandRepeat(int argc, char **argv, long repeat) {
config.cluster_reissue_command = 0;
return REDIS_ERR;
}
/* Reset dbnum after reconnecting so we can re-select the previous db in cliSelect(). */
config.dbnum = 0;
cliSelect();
}
config.cluster_reissue_command = 0;
if (config.cluster_send_asking) {
......
......@@ -809,3 +809,27 @@ if {!$::tls} { ;# fake_redis_node doesn't support TLS
assert_equal "a\n1\nb\n2\nc\n3" [exec {*}$cmdline ZRANGE new_zset 0 -1 WITHSCORES]
}
}
start_server {tags {"cli external:skip"}} {
test_interactive_cli_with_prompt "db_num showed in redis-cli after reconnected" {
run_command $fd "select 0\x0D"
run_command $fd "set a zoo-0\x0D"
run_command $fd "select 6\x0D"
run_command $fd "set a zoo-6\x0D"
r save
# kill server and restart
exec kill [s process_id]
wait_for_log_messages 0 {"*Redis is now ready to exit*"} 0 1000 10
catch {[run_command $fd "ping\x0D"]} err
restart_server 0 true false 0
# redis-cli should show '[6]' after reconnected and return 'zoo-6'
write_cli $fd "GET a\x0D"
after 100
set result [format_output [read_cli $fd]]
set regex {not connected> GET a.*"zoo-6".*127\.0\.0\.1:[0-9]*\[6\]>}
assert_equal 1 [regexp $regex $result]
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment