Commit ddaac7a7d9fba1eae5d964b64a2ae9eddde3c325
1 parent
5440bf64
Added more instances of correct locking while reading the connect_state_ variable.
Showing
1 changed file
with
15 additions
and
4 deletions
src/client.cpp
| @@ -148,8 +148,12 @@ void Redox::connectedCallback(const redisAsyncContext *ctx, int status) { | @@ -148,8 +148,12 @@ void Redox::connectedCallback(const redisAsyncContext *ctx, int status) { | ||
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | rdx->connect_waiter_.notify_all(); | 150 | rdx->connect_waiter_.notify_all(); |
| 151 | + { | ||
| 152 | + unique_lock<mutex> lk(rdx->connect_lock_); | ||
| 153 | + int state = rdx->connect_state_; | ||
| 154 | + } | ||
| 151 | if (rdx->user_connection_callback_) | 155 | if (rdx->user_connection_callback_) |
| 152 | - rdx->user_connection_callback_(rdx->connect_state_); | 156 | + rdx->user_connection_callback_(state); |
| 153 | } | 157 | } |
| 154 | 158 | ||
| 155 | void Redox::disconnectedCallback(const redisAsyncContext *ctx, int status) { | 159 | void Redox::disconnectedCallback(const redisAsyncContext *ctx, int status) { |
| @@ -168,8 +172,12 @@ void Redox::disconnectedCallback(const redisAsyncContext *ctx, int status) { | @@ -168,8 +172,12 @@ void Redox::disconnectedCallback(const redisAsyncContext *ctx, int status) { | ||
| 168 | 172 | ||
| 169 | rdx->stop(); | 173 | rdx->stop(); |
| 170 | rdx->connect_waiter_.notify_all(); | 174 | rdx->connect_waiter_.notify_all(); |
| 175 | + { | ||
| 176 | + unique_lock<mutex> lk(rdx->connect_lock_); | ||
| 177 | + int state = rdx->connect_state_; | ||
| 178 | + } | ||
| 171 | if (rdx->user_connection_callback_) | 179 | if (rdx->user_connection_callback_) |
| 172 | - rdx->user_connection_callback_(rdx->connect_state_); | 180 | + rdx->user_connection_callback_(state); |
| 173 | } | 181 | } |
| 174 | 182 | ||
| 175 | bool Redox::initEv() { | 183 | bool Redox::initEv() { |
| @@ -317,8 +325,11 @@ void Redox::runEventLoop() { | @@ -317,8 +325,11 @@ void Redox::runEventLoop() { | ||
| 317 | this_thread::sleep_for(chrono::milliseconds(10)); | 325 | this_thread::sleep_for(chrono::milliseconds(10)); |
| 318 | ev_run(evloop_, EVRUN_NOWAIT); | 326 | ev_run(evloop_, EVRUN_NOWAIT); |
| 319 | 327 | ||
| 320 | - if (connect_state_ == CONNECTED) | ||
| 321 | - redisAsyncDisconnect(ctx_); | 328 | + { |
| 329 | + unique_lock<mutex> ul(connect_lock_); | ||
| 330 | + if (connect_state_ == CONNECTED) | ||
| 331 | + redisAsyncDisconnect(ctx_); | ||
| 332 | + } | ||
| 322 | 333 | ||
| 323 | // Run once more to disconnect | 334 | // Run once more to disconnect |
| 324 | ev_run(evloop_, EVRUN_NOWAIT); | 335 | ev_run(evloop_, EVRUN_NOWAIT); |