Skip to content

Commit

Permalink
Fix deadlock caused by ExecutorService::close (#11882)
Browse files Browse the repository at this point in the history
  • Loading branch information
BewareMyPower authored Sep 2, 2021
1 parent df9ae16 commit 43b4ff6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pulsar-client-cpp/lib/ClientConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,11 @@ void ClientConnection::handleRead(const boost::system::error_code& err, size_t b

if (err || bytesTransferred == 0) {
if (err) {
LOG_ERROR(cnxString_ << "Read failed: " << err.message());
if (err == boost::asio::error::operation_aborted) {
LOG_DEBUG(cnxString_ << "Read failed: " << err.message());
} else {
LOG_ERROR(cnxString_ << "Read operation was cancelled");
}
} // else: bytesTransferred == 0, which means server has closed the connection
close();
} else if (bytesTransferred < minReadSize) {
Expand Down
8 changes: 3 additions & 5 deletions pulsar-client-cpp/lib/ExecutorService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ void ExecutorService::close() {

io_service_->stop();
work_.reset();
// If this thread is attempting to join itself, do not. The destructor's
// call to close will handle joining if it does not occur here. This also ensures
// join is not called twice since it is not re-entrant on windows
if (std::this_thread::get_id() != worker_.get_id() && worker_.joinable()) {
worker_.join();
// Detach the worker thread instead of join to avoid potential deadlock
if (worker_.joinable()) {
worker_.detach();
}
}

Expand Down

0 comments on commit 43b4ff6

Please sign in to comment.