redis-plus-plus icon indicating copy to clipboard operation
redis-plus-plus copied to clipboard

[QUESTION] The program will be terminated when exception occurs during the automatic unlocking

Open regenttsui opened this issue 2 months ago • 3 comments

Describe the problem If an exception occurs during the automatic unlocking in the destructor of std::lock_guard,it seems to cause the program to terminate. Here is an example, I manually shutdown the Redis before the deconstruction of std::lock_guard:

#include <memory>
#include <sw/redis++/redis++.h>
#include <sw/redis++/patterns/redlock.h>
#include <iostream>

using namespace sw::redis;

int main() {
        try {
                ConnectionOptions connection_options;
                connection_options.host = "127.0.0.1";
                connection_options.port = 9379;
                auto redis = std::make_shared<Redis>(connection_options);
                RedMutex mtx(redis, "resource");
                std::lock_guard<RedMutex> lock(mtx);
		//Sleep for a while to shutdown Redis manually
                std::cout << "sleep for 10s" << std::endl;
                std::this_thread::sleep_for(std::chrono::seconds(10));
        } catch (const Error &err) {
                std::cout << "catch error=" << err.what() << std::endl;
        }
        std::cout << "success" << std::endl;

        return 0;
}

I think the reason is that the exception is re-thrown in the destructor. Here is the source code of redispp:

void RedMutexImpl::unlock() {
    std::lock_guard<std::mutex> lock(_mtx);

    if (!_locked()) {
        throw Error("RedMutex is not locked");
    }

    try {
        _unlock(_lock_id);
    } catch (...) {
        _reset();
        throw;
    }

    _reset();
}

Environment:

  • OS: CentOS
  • Compiler: gcc 8.3.1
  • hiredis version: 1.1.1-dev
  • redis-plus-plus version: 1.3.6

regenttsui avatar May 06 '24 09:05 regenttsui

You used a very old version of redis-plus-plus. Please update to the latest one, and try again. This problem should have been fixed.

sewenew avatar May 07 '24 06:05 sewenew

You used a very old version of redis-plus-plus. Please update to the latest one, and try again. This problem should have been fixed.

I tried the version 1.3.12, but the problem still exists

regenttsui avatar May 08 '24 08:05 regenttsui

I manually shutdown the Redis before the deconstruction of std::lock_guard

Sorry, I missed this. Yes, this is a problem. Looks like that unlock should not throw. However, in this case, application which calls unlock directly, cannot tell if unlock successes or not.

Thanks for reporting it! I'll rethink it, and fix it.

Regards

sewenew avatar May 08 '24 13:05 sewenew