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

issue-2809: fix data race while shutting down TFakeTestServer #2810

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Changes from 1 commit
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
37 changes: 33 additions & 4 deletions cloud/blockstore/libs/discovery/test_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct TFakeBlockStoreServer::TImpl
ui32 LastByteCount = 0;
bool DropPingRequests = false;
TVector<std::unique_ptr<TRequestContext>> DroppedRequests;
TAtomic ShouldStop = 0;

TImpl(
ui16 port,
Expand All @@ -102,12 +103,19 @@ struct TFakeBlockStoreServer::TImpl
}

if (Server) {
Server->Shutdown();
CQ->Shutdown();

// Need to stop the loop explicitly to avoid possible data race
// with grpc server shutdown.
// See https://github.com/ydb-platform/nbs/issues/2809
AtomicSet(ShouldStop, 1);
if (Thread) {
Thread->Join();
}

Server->Shutdown();
CQ->Shutdown();
// Need to drain completion queue, otherwise it fails on assert when destroying:
// https://github.com/ydb-platform/nbs/blob/fbf6b9fa568b7b3861fbccffcf3177ee445f498a/contrib/libs/grpc/src/core/lib/surface/completion_queue.cc#L258
DrainCompletionQueue();
}
}

Expand Down Expand Up @@ -153,7 +161,13 @@ struct TFakeBlockStoreServer::TImpl

void* tag;
bool ok;
while (CQ->Next(&tag, &ok)) {
tpashkin marked this conversation as resolved.
Show resolved Hide resolved
while (!AtomicGet(ShouldStop)) {
const auto deadline = gpr_time_0(GPR_TIMESPAN);
auto status = CQ->AsyncNext(&tag, &ok, deadline);
if (status != grpc::CompletionQueue::GOT_EVENT) {
continue;
SvartMetal marked this conversation as resolved.
Show resolved Hide resolved
}

auto* requestContext = static_cast<TRequestContext*>(tag);

if (requestContext->Done || !ok) {
Expand All @@ -172,6 +186,21 @@ struct TFakeBlockStoreServer::TImpl
}
}

void DrainCompletionQueue()
{
void* tag;
bool ok;
while (CQ->Next(&tag, &ok)) {
auto* requestContext = static_cast<TRequestContext*>(tag);

if (requestContext->Done || !ok) {
delete requestContext;
} else {
FinishRequest(requestContext);
}
}
}

void FinishRequest(TRequestContext* requestContext)
{
requestContext->Done = true;
Expand Down
Loading