This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 555
Fix memory corruption in case of pending callbacks on destruction of client object #200
Open
skeetor
wants to merge
2
commits into
Cylix:master
Choose a base branch
from
skeetor:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// The MIT License (MIT) | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2015-2017 Simon Ninon <[email protected]> | ||
// | ||
|
@@ -161,6 +162,13 @@ class client { | |
//! | ||
void cancel_reconnect(void); | ||
|
||
//! | ||
//! Wait until all callbacks are completed. | ||
//! | ||
//! \return current instance | ||
//! | ||
client& wait_for_completion(void); | ||
|
||
public: | ||
//! | ||
//! reply callback called whenever a reply is received | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,11 @@ client::~client(void) { | |
m_client.disconnect(true); | ||
} | ||
|
||
// [email protected] | ||
// If there are pending callbacks we have to wait until they are completed | ||
// otherwise this will cause a memory corruption if they return after this point. | ||
wait_for_completion(); | ||
|
||
__CPP_REDIS_LOG(debug, "cpp_redis::client destroyed"); | ||
} | ||
|
||
|
@@ -125,6 +130,7 @@ client::disconnect(bool wait_for_removal) { | |
|
||
//! make sure we clear buffer of unsent commands | ||
clear_callbacks(); | ||
wait_for_completion(); | ||
|
||
__CPP_REDIS_LOG(info, "cpp_redis::client disconnected"); | ||
} | ||
|
@@ -201,11 +207,7 @@ client::sync_commit(void) { | |
try_commit(); | ||
} | ||
|
||
std::unique_lock<std::mutex> lock_callback(m_callbacks_mutex); | ||
__CPP_REDIS_LOG(debug, "cpp_redis::client waiting for callbacks to complete"); | ||
m_sync_condvar.wait(lock_callback, [=] { return m_callbacks_running == 0 && m_commands.empty(); }); | ||
__CPP_REDIS_LOG(debug, "cpp_redis::client finished waiting for callback completion"); | ||
return *this; | ||
return wait_for_completion(); | ||
} | ||
|
||
void | ||
|
@@ -215,11 +217,11 @@ client::try_commit(void) { | |
m_client.commit(); | ||
__CPP_REDIS_LOG(info, "cpp_redis::client sent pipelined commands"); | ||
} | ||
catch (const cpp_redis::redis_error&) { | ||
catch (const cpp_redis::redis_error& e) { | ||
__CPP_REDIS_LOG(error, "cpp_redis::client could not send pipelined commands"); | ||
//! ensure commands are flushed | ||
clear_callbacks(); | ||
throw; | ||
throw e; | ||
} | ||
} | ||
|
||
|
@@ -250,8 +252,17 @@ client::connection_receive_handler(network::redis_connection&, reply& reply) { | |
} | ||
} | ||
|
||
client& | ||
client::wait_for_completion(void) { | ||
std::unique_lock<std::mutex> lock_callback(m_callbacks_mutex); | ||
__CPP_REDIS_LOG(debug, "cpp_redis::client waiting for callbacks to complete"); | ||
m_sync_condvar.wait(lock_callback, [=] { return m_callbacks_running == 0 && m_commands.empty(); }); | ||
__CPP_REDIS_LOG(debug, "cpp_redis::client finished waiting for callback completion"); | ||
return *this; | ||
} | ||
|
||
void | ||
client::clear_callbacks(void) { | ||
client::clear_callbacks() { | ||
if (m_commands.empty()) { | ||
return; | ||
} | ||
|
@@ -288,7 +299,7 @@ client::resend_failed_commands(void) { | |
//! dequeue commands and move them to a local variable | ||
std::queue<command_request> commands = std::move(m_commands); | ||
|
||
while (commands.size() > 0) { | ||
while (m_commands.size() > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change would cause a regression since m_commands would be empty after moving it into the local variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If m_commands is empty after the move, then the loop is never entered. And this will always be the case, or am I wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are correct but it would revert the fix introduced in 2a22c32 |
||
//! Reissue the pending command and its callback. | ||
unprotected_send(commands.front().command, commands.front().callback); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if no callbacks are pending, then
notify_all()
will not be called fromconnection_receive_handler
andm_sync_condvar.wait
will hang. So I don't think this works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I take that back. However, there is another problem: If there are uncommitted commands, then wait for completion will not return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@skeetor , have a look at this please https://github.com/steple/cpp_redis/commit/b69991470c4ea35b29279b79b8b836c0321ad315
This will pass all tests. I'll try to add a test case that tests for the original bug of the pending callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So does this mean that I can withdraw my PR and you will submit yours?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will, but my PR is missing a test case for the bug that you had found. I'll need to add that before I can submit it. If you have an idea on how to test it, please let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unable to reproduce what I think is the bug you described. Can you provide an example or a more detailed description?