redis: Reconnect stops working if server is down

My environment is Boost 1.85, GCC 14.2.1 (C++23) and Redis 7.2.5.

Observing a small quirk and not sure if its an issue with my setup or something else. If I leave the Redis server shutdown and initiate a redis::connection with async_run, then it will only attempt to reconnect two times.

After the second ’ Connection refused’, nothing else on the connection instance works (i.e. async_exec no longer runs or cancels).

Expected behavior when Redis is down: Perpetual reconnect loop and requests should be failing since cancel_if_not_connected is explicitly set to true.

Boost/Redis logs with added timestamps

2024-09-19 13:20:33.714400268 (Boost.Redis) check-health-op: timeout disabled.
2024-09-19 13:20:33.714497273 (Boost.Redis) run-all-op: resolve addresses 127.0.0.1:6379
2024-09-19 13:20:33.714646366 (Boost.Redis) run-all-op: connected to endpoint Connection refused

2024-09-19 13:20:33.714686392 (Boost.Redis) hello-op: Operation canceled
2024-09-19 13:20:33.714699958 (Boost.Redis) hello-op: error/canceled. Exiting ...
2024-09-19 13:20:33.714713033 (Boost.Redis) runner-op: Connection refused (async_run_all), Success (async_health_check) Operation canceled (async_hello).
2024-09-19 13:20:33.714730756 (Boost.Redis) Connection lost: Connection refused
2024-09-19 13:20:36.715120375 (Boost.Redis) check-health-op: timeout disabled.
2024-09-19 13:20:36.715291159 (Boost.Redis) run-all-op: resolve addresses 127.0.0.1:6379
2024-09-19 13:20:36.715513632 (Boost.Redis) run-all-op: connected to endpoint Connection refused

//NO MORE OUTPUT FROM HERE ON OUT

Reproduction source code

#include <chrono>
#include <memory>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/redis.hpp>
#include <boost/redis/src.hpp>

namespace asio = boost::asio;
using namespace boost::redis;
using asio::awaitable;
using asio::detached;
using asio::deferred;

awaitable<void> co_main(config cfg) try
{
    auto conn = std::make_shared<connection>(co_await asio::this_coro::executor);
    conn->async_run(cfg, logger(logger::level::debug), detached);

    asio::steady_timer timer{co_await asio::this_coro::executor};

    //Perpetual 30 second PING-PONG (PONG response is ignored, not relevant to issue)
    for(;;)  {
        timer.expires_after(std::chrono::seconds(30));
        co_await timer.async_wait(deferred);

        try {
            request req;
            req.push("PING");
            //Request is never cancelled, even though we are not connected since Redis server is down
            req.get_config().cancel_if_not_connected = true; 
            co_await conn->async_exec(req, ignore, deferred);
        } catch(std::exception& e){
            std::cerr << "(co_main loop) " << e.what() << std::endl;
        }

    }
}catch(std::exception& ex){
    std::cerr << "(co_main) " << ex.what() << std::endl;
}

auto main(int argc, char * argv[]) -> int
{
    try {
        config cfg;
        cfg.health_check_interval = {}; //Disable PING
        cfg.reconnect_wait_interval = std::chrono::seconds(3); //Increase retry time

        asio::io_context ioc;
        asio::co_spawn(ioc, co_main(cfg), [](std::exception_ptr p) {
            if (p) std::rethrow_exception(p);
        });
        ioc.run();
    } catch (std::exception const& e) {
        std::cerr << "(main) " << e.what() << std::endl;
        return 1;
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 22

Most upvoted comments

Hi @zlausd, this PR has some improvements that I think will fix this problem. I couldn’t test it in the container because it uses vcpkg to pull boost so I will think another way. If you have the time to test that branch please let me know if it fixes the problem for you.

I just managed to reproduce this problem on your container and will investigate it further.

Hm, if I remove the following from boost/redis/detail/connection_base.hpp, then things work as I would expect. That is, the connection instance will try to perpetually reconnect and any issued async_exec requests fail as cancelled.

Once Redis server is back up and running, everything works as expected.

            // Protects the code below from being called more than
            // once, see https://github.com/boostorg/redis/issues/181
            if (std::exchange(cancel_run_called_, true)) {
               return;
            }

EDIT:

Another solution that seems to work is calling cancel_on_conn_lost(); in the above if statement? Don’t fully understand the context for which the above if statement was added or if what I am suggesting is a proper fix.

Related question: I have set request->get_config().cancel_if_not_connected = true;. Shouldn’t my post 10 have been cancelled?

I am not exactly sure what the code is doing. cancel_if_not_connected is checked only once at the time async_exec is called. that means, if a disconnect occurs after that it won’t cause the request to be canceled. If you have more questions about that I suggest opening another issue.

Thanks for responding, I think I got the gist of it. To clarify: if the connection to the server goes down, all enqueued async_exec will get cancelled if cancel_on_connection_lost is set to true (and cancel_if_unresponded to true if sent already), right?

Related question: I have set request->get_config().cancel_if_not_connected = true;. Shouldn’t my post 10 have been cancelled?

I am not exactly sure what the code is doing. cancel_if_not_connected is checked only once at the time async_exec is called. that means, if a disconnect occurs after that it won’t cause the request to be canceled. If you have more questions about that I suggest opening another issue.