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

fix(common): gRPC alarms require more locking #12406

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions google/cloud/internal/default_completion_queue_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,25 @@ class AsyncTimerFuture : public AsyncGrpcOperation {

void Set(grpc::CompletionQueue& cq,
std::chrono::system_clock::time_point deadline, void* tag) {
std::unique_lock<std::mutex> lk(mu_);
if (state_ != kIdle) return;
coryan marked this conversation as resolved.
Show resolved Hide resolved
deadline_ = deadline;
state_ = kSet;
alarm_.Set(&cq, deadline, tag);
}

void Cancel() override {
ScopedCallContext scope(call_context_);
std::unique_lock<std::mutex> lk(mu_);
if (state_ == kCancelled) return;
if (state_ == kIdle) {
coryan marked this conversation as resolved.
Show resolved Hide resolved
state_ = kCancelled;
lk.unlock();
// Release the lock before (potentially) calling application code.
promise_.set_value(Cancelled());
return;
}
state_ = kCancelled;
alarm_.Cancel();
}

Expand All @@ -78,18 +91,22 @@ class AsyncTimerFuture : public AsyncGrpcOperation {

bool Notify(bool ok) override {
ScopedCallContext scope(call_context_);
promise_.set_value(ok ? ValueType(deadline_) : Canceled());
promise_.set_value(ok ? ValueType(deadline_) : Cancelled());
return true;
}

static ValueType Canceled() {
static ValueType Cancelled() {
return Status{StatusCode::kCancelled, "timer canceled"};
}

enum State { kIdle, kSet, kCancelled };

promise<ValueType> promise_;
std::chrono::system_clock::time_point deadline_;
grpc::Alarm alarm_;
CallContext call_context_;
std::mutex mu_;
State state_ = kIdle;
grpc::Alarm alarm_;
};

} // namespace
Expand Down Expand Up @@ -190,11 +207,9 @@ void DefaultCompletionQueueImpl::Run() {
}

void DefaultCompletionQueueImpl::Shutdown() {
{
std::lock_guard<std::mutex> lk(mu_);
shutdown_ = true;
shutdown_guard_.reset();
}
std::lock_guard<std::mutex> lk(mu_);
shutdown_ = true;
shutdown_guard_.reset();
}

void DefaultCompletionQueueImpl::CancelAll() {
Expand Down