Skip to content
Merged
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
6 changes: 3 additions & 3 deletions projects/rdc/include/rdc_lib/RdcLibraryLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class RdcLibraryLoader {
rdc_status_t load_symbol(T* func_handler, const char* func_name);

template <typename T>
rdc_status_t load(const char* filename, T* func_make_handler);
rdc_status_t load(const char* filename, T* func_make_handler, int dlopen_flags = RTLD_LAZY);

rdc_status_t unload();

Expand Down Expand Up @@ -77,13 +77,13 @@ rdc_status_t RdcLibraryLoader::load_symbol(T* func_handler, const char* func_nam
}

template <typename T>
rdc_status_t RdcLibraryLoader::load(const char* filename, T* func_make_handler) {
rdc_status_t RdcLibraryLoader::load(const char* filename, T* func_make_handler, int dlopen_flags) {
if (filename == nullptr || func_make_handler == nullptr) {
return RDC_ST_FAIL_LOAD_MODULE;
}

try {
rdc_status_t status = load(filename);
rdc_status_t status = load(filename, dlopen_flags);
if (status != RDC_ST_OK) {
return status;
}
Expand Down
5 changes: 4 additions & 1 deletion projects/rdc/rdc_libs/bootstrap/src/RdcBootStrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ rdc_status_t rdc_connect(const char* ipAddress, rdc_handle_t* p_rdc_handle, cons
return RDC_ST_FAIL_LOAD_MODULE;
}

rdc_status_t status = rdc_lib_loader.load("librdc_client.so", &func_make_handler);
// gRPC worker threads in librdc_client.so keep running after the channel is
// destroyed, so rdc_shutdown() must not unmap the code they execute.
rdc_status_t status =
rdc_lib_loader.load("librdc_client.so", &func_make_handler, RTLD_LAZY | RTLD_NODELETE);
if (status != RDC_ST_OK) {
*p_rdc_handle = nullptr;
return status;
Expand Down
19 changes: 18 additions & 1 deletion projects/rdc/tests/ci/test_gtest_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
GoogleTest runner -- executes the rdctst binary in embedded batch mode.
GoogleTest runner -- executes the rdctst binary in embedded batch mode and the
rdc_client_tests binary.
"""

import subprocess
Expand Down Expand Up @@ -33,6 +34,22 @@ def test_rdctst_embedded_batch(self, rdctst_path, rdcd_path, has_gpu):
f"rdctst batch mode failed (rc={result.returncode}):\n{result.stderr[-2000:]}"
)

def test_rdc_client_shutdown(self, rocm_dir, rdctst_path):
# rdctst_path skips this test unless the RDC test package is installed. rdctst
# is built only with the standalone client, so rdc_client_tests must be too.
binary = os.path.join(rocm_dir, "share", "rdc", "rdctst_tests", "rdc_client_tests")
assert os.path.isfile(binary), f"{binary} is missing from the RDC test package"

result = subprocess.run([binary], capture_output=True, text=True, timeout=120)

print(result.stdout)
if result.stderr:
print(result.stderr)

assert result.returncode == 0, (
f"rdc_client_tests failed (rc={result.returncode}):\n{result.stdout[-2000:]}"
)

def test_rdctst_help(self, rdctst_path):
result = subprocess.run(
[rdctst_path, "--rdctst_help"], capture_output=True, text=True, timeout=30
Expand Down
28 changes: 28 additions & 0 deletions projects/rdc/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ target_compile_definitions(
target_link_libraries(rdc_diag_kernel_tests PRIVATE GTest::gtest_main pthread)
add_test(NAME rdc_diag_kernel_tests COMMAND rdc_diag_kernel_tests)

# rdc_client_tests: standalone (client) mode as rdci uses it. rdc_connect()
# dlopens librdc_client.so (gRPC); needs no GPU and no rdcd. Built only when the
# client library is.
if(TARGET rdc_client)
add_executable(rdc_client_tests ${SRC_DIR}/test_client_shutdown.cc)
target_include_directories(rdc_client_tests PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(
rdc_client_tests
PRIVATE rdc_bootstrap GTest::gtest_main pthread ${CMAKE_DL_LIBS}
)
add_dependencies(rdc_client_tests rdc_client)
set_target_properties(
rdc_client_tests
PROPERTIES INSTALL_RPATH "\$ORIGIN:\$ORIGIN/../../../lib"
)
add_test(NAME rdc_client_tests COMMAND rdc_client_tests)
# librdc_bootstrap dlopens librdc_client.so by name; find it in the build tree.
set_tests_properties(
rdc_client_tests
PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=$<TARGET_FILE_DIR:rdc_client>"
)
install(
TARGETS rdc_client_tests
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${RDC}/rdctst_tests
COMPONENT ${TESTS_COMPONENT}
)
endif()

# rdc_unit_tests: hardware-aware SmiUtils tests. Links librdc (flat table,
# partition helpers) and amd_smi. Provides its own main() to init amdsmi once.
# Hardware-dependent tests self-skip when no AMD GPU is present.
Expand Down
79 changes: 79 additions & 0 deletions projects/rdc/tests/unit/test_client_shutdown.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright (c) 2026 - present Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// rdc_shutdown() closes librdc_client.so once the client is done with it. gRPC
// inside that library keeps detached worker threads running after the channel is
// destroyed, so the library has to stay mapped: unmapping it under those threads
// crashed rdci with SIGSEGV on exit, after it had printed its results.
//
// glibc never unloads a library that has an STB_GNU_UNIQUE symbol. GCC emits
// those for static variables in inline functions and templates, so a client
// library with GCC-compiled code in it usually passes this test with or
// without the fix. A clang-only build, as TheRock ships, needs the fix.

#include <dlfcn.h>
#include <gtest/gtest.h>

#include <chrono>
#include <thread>

#include "rdc/rdc.h"

namespace {

// Nothing listens here, so RPCs fail at once without touching the network.
constexpr char kNoServer[] = "unix:/nonexistent/rdc_client_tests.sock";

// gRPC's thread-pool watchdog wakes at least once a second. One wakeup after
// the library is unmapped is enough to crash the process.
constexpr auto kThreadWakeupWait = std::chrono::milliseconds(1500);

} // namespace

TEST(RdcClientShutdown, KeepsClientLibraryLoaded) {
// The second pass reconnects after rdc_shutdown(), as rdctst does between tests.
for (int pass = 0; pass < 2; ++pass) {
SCOPED_TRACE(pass);
ASSERT_EQ(rdc_init(0), RDC_ST_OK);

rdc_handle_t handle = nullptr;
ASSERT_EQ(rdc_connect(kNoServer, &handle, nullptr, nullptr, nullptr), RDC_ST_OK)
<< "librdc_client.so is not on the library search path";
ASSERT_NE(handle, nullptr);

// A failed RPC is enough to start gRPC's worker threads.
uint32_t gpu_index_list[RDC_MAX_NUM_DEVICES] = {};
uint32_t count = 0;
EXPECT_EQ(rdc_device_get_all(handle, gpu_index_list, &count), RDC_ST_CLIENT_ERROR);

ASSERT_EQ(rdc_disconnect(handle), RDC_ST_OK);
ASSERT_EQ(rdc_shutdown(), RDC_ST_OK);

void* client = dlopen("librdc_client.so", RTLD_LAZY | RTLD_NOLOAD);
EXPECT_NE(client, nullptr) << "rdc_shutdown() unloaded librdc_client.so";
if (client != nullptr) {
dlclose(client);
}
}

std::this_thread::sleep_for(kThreadWakeupWait);
}
Loading