Skip to content

Commit

Permalink
fix race condition in grpc streams
Browse files Browse the repository at this point in the history
Write should not be called again before it is finished.
That can happen with our thread pool, hence the mutex.

It seems like it was not crashing before, and the recent
update of grpc made it visible when running on iOS.
  • Loading branch information
JonasVautherin committed Jul 5, 2019
1 parent 4ea885a commit 3e75734
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 75 deletions.
10 changes: 8 additions & 2 deletions src/backend/src/core/core_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ class CoreServiceImpl final : public mavsdk::rpc::core::CoreService::Service {
const rpc::core::SubscribeConnectionStateRequest * /* request */,
grpc::ServerWriter<rpc::core::ConnectionStateResponse> *writer) override
{
_dc.register_on_discover([&writer](const uint64_t uuid) {
std::mutex connection_state_mutex{};

_dc.register_on_discover([&writer, &connection_state_mutex](const uint64_t uuid) {
const auto rpc_connection_state_response = createRpcConnectionStateResponse(uuid, true);

std::lock_guard<std::mutex> lock(connection_state_mutex);
writer->Write(rpc_connection_state_response);
});

_dc.register_on_timeout([&writer](const uint64_t uuid) {
_dc.register_on_timeout([&writer, &connection_state_mutex](const uint64_t uuid) {
const auto rpc_connection_state_response =
createRpcConnectionStateResponse(uuid, false);

std::lock_guard<std::mutex> lock(connection_state_mutex);
writer->Write(rpc_connection_state_response);
});

Expand Down
Loading

0 comments on commit 3e75734

Please sign in to comment.