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

Close sideband socket gracefully when closing the grpc server #1132

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ add_executable(ni_grpc_device_server
"source/server/calibration_operations_restricted_service_registrar.cpp"
"source/server/calibration_operations_restricted_service.cpp"
"source/server/core_server.cpp"
"source/server/sideband_manager.cpp"
"source/server/core_services_registrar.cpp"
"source/server/data_moniker_service.cpp"
"source/server/debug_session_properties_restricted_service_registrar.cpp"
Expand Down
7 changes: 4 additions & 3 deletions source/server/core_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "version.h"
#include "data_moniker_service.h"
#include "sideband_manager.h"

using FeatureState = nidevice_grpc::FeatureToggles::FeatureState;
using FeatureToggles = nidevice_grpc::FeatureToggles;
Expand Down Expand Up @@ -69,10 +70,12 @@ static ServerConfiguration GetConfiguration(const std::string& config_file_path)
static std::mutex server_mutex;
static std::unique_ptr<grpc::Server> server;
static bool shutdown = false;
SidebandManager sideband_manager;

static void StopServer()
{
std::lock_guard<std::mutex> guard(server_mutex);
sideband_manager.stop_sideband_thread();
shutdown = true;
if (server) {
server->Shutdown();
Expand Down Expand Up @@ -112,9 +115,7 @@ static void RunServer(const ServerConfiguration& config)
}
server = builder.BuildAndStart();
if (ni::data_monikers::is_sideband_streaming_enabled(config.feature_toggles)) {
auto sideband_socket_thread = new std::thread(RunSidebandSocketsAccept, config.sideband_address.c_str(), config.sideband_port);
// auto sideband_rdma_send_thread = new std::thread(AcceptSidebandRdmaSendRequests);
// auto sideband_rdma_recv_thread = new std::thread(AcceptSidebandRdmaReceiveRequests);
sideband_manager.start_sideband_thread(config.sideband_address.c_str(), config.sideband_port);
}
}

Expand Down
14 changes: 14 additions & 0 deletions source/server/sideband_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "sideband_manager.h"
#include "data_moniker_service.h"

void SidebandManager::start_sideband_thread(const std::string& address, int port) {
sideband_socket_thread = std::make_unique<std::thread>(RunSidebandSocketsAccept, address.c_str(), port, std::ref(stop_flag));
}

void SidebandManager::stop_sideband_thread() {
stop_flag.store(true);
if (sideband_socket_thread && sideband_socket_thread->joinable()) {
sideband_socket_thread->join();
}
sideband_socket_thread.reset();
}
18 changes: 18 additions & 0 deletions source/server/sideband_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef SIDEBAND_MANAGER_H
#define SIDEBAND_MANAGER_H

#include <thread>
#include <string>
#include <atomic>

class SidebandManager {
public:
void start_sideband_thread(const std::string& address, int port);
void stop_sideband_thread();

private:
std::atomic<bool> stop_flag {false};
std::unique_ptr<std::thread> sideband_socket_thread;
};

#endif // SIDEBAND_MANAGER_H
Loading