Skip to content

Commit

Permalink
librbd: fix race condition with AIO completion callbacks
Browse files Browse the repository at this point in the history
Now that librbd utilizes multiple threads for the IO path, it's
possible for a race condition to occur if a client app is waiting
on a completion to fire and the actual invokation of the
completion.

Signed-off-by: Jason Dillaman <dillaman@redhat.com>
  • Loading branch information
Jason Dillaman committed Jul 16, 2020
1 parent cfbd188 commit 50694f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/librbd/io/AioCompletion.cc
Expand Up @@ -107,15 +107,11 @@ void AioCompletion::complete() {
} else {
complete_cb(rbd_comp, complete_arg);
complete_event_socket();
notify_callbacks_complete();
}
} else {
complete_event_socket();
}
state = AIO_STATE_COMPLETE;

{
std::unique_lock<std::mutex> locker(lock);
cond.notify_all();
notify_callbacks_complete();
}

if (image_dispatcher_ctx != nullptr) {
Expand Down Expand Up @@ -263,6 +259,7 @@ void AioCompletion::complete_external_callback() {
boost::asio::dispatch(ictx->asio_engine->get_api_strand(), [this]() {
complete_cb(rbd_comp, complete_arg);
complete_event_socket();
notify_callbacks_complete();
put();
});
}
Expand All @@ -274,5 +271,12 @@ void AioCompletion::complete_event_socket() {
}
}

void AioCompletion::notify_callbacks_complete() {
state = AIO_STATE_COMPLETE;

std::unique_lock<std::mutex> locker(lock);
cond.notify_all();
}

} // namespace io
} // namespace librbd
2 changes: 1 addition & 1 deletion src/librbd/io/AioCompletion.h
Expand Up @@ -181,7 +181,7 @@ struct AioCompletion {
void queue_complete();
void complete_external_callback();
void complete_event_socket();

void notify_callbacks_complete();
};

class C_AioRequest : public Context {
Expand Down

0 comments on commit 50694f7

Please sign in to comment.