Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
regenttsui opened this issue May 6, 2024 · 3 comments

Comments

@regenttsui
Copy link

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
@sewenew
Copy link
Owner

sewenew commented May 7, 2024

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.

@regenttsui
Copy link
Author

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

@sewenew
Copy link
Owner

sewenew commented May 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants