Repository navigation
Conversation
The "has OS thread name" tests were compiled whenever `pthread_getname_np()` exists, but they also need a working `SetOsThreadName()`. Replace the `HAVE_PTHREAD_GETNAME_NP` guard with a runtime probe. If it fails, log a warning and return from the three tests.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
|
cc @ViniciusCestarii as the author of #324. |
|
From the illumos CI job added in #393, which is based on this PR: |
The three "has OS thread name" tests in `mptest` are compiled whenever `pthread_getname_np()` exists, but `SetOsThreadName()` has no branch for illumos's two-argument `pthread_setname_np()`, so the names are never set and the tests fail on both illumos jobs. bitcoin-core/libmultiprocess#392 skips the tests where thread names do not work. Until it reaches the Bitcoin Core subtree, compile them out after fetching the source tarball. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VSoBw6fwePBKa4PD531zyQ
|
Concept ACK. I was wondering if we could keep the compile-time check, and my agent came up with the following diff: diff --git a/cmake/pthread_checks.cmake b/cmake/pthread_checks.cmake
index c5e6d98..b8209f4 100644
--- a/cmake/pthread_checks.cmake
+++ b/cmake/pthread_checks.cmake
@@ -7,6 +7,7 @@
include(CMakePushCheckState)
include(CheckCXXSourceCompiles)
+include(CheckSymbolExists)
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES Threads::Threads)
@@ -47,3 +48,11 @@ check_cxx_source_compiles("
}"
HAVE_PTHREAD_GETTHREADID_NP)
cmake_pop_check_state()
+
+check_symbol_exists(PR_SET_NAME "sys/prctl.h" HAVE_PR_SET_NAME)
+
+# OS thread names can be set by SetOsThreadName() and read back by
+# ThreadName(). The setter conditions must match SetOsThreadName().
+if(HAVE_PTHREAD_GETNAME_NP AND (HAVE_PR_SET_NAME OR HAVE_PTHREAD_SETNAME_NP_3ARG OR HAVE_PTHREAD_GETTHREADID_NP OR APPLE))
+ set(HAVE_OS_THREAD_NAMES 1)
+endif()
diff --git a/include/mp/config.h.in b/include/mp/config.h.in
index b4ca6de..bb51c43 100644
--- a/include/mp/config.h.in
+++ b/include/mp/config.h.in
@@ -13,5 +13,7 @@
#cmakedefine HAVE_PTHREAD_SETNAME_NP_3ARG @HAVE_PTHREAD_SETNAME_NP_3ARG@
#cmakedefine HAVE_PTHREAD_THREADID_NP @HAVE_PTHREAD_THREADID_NP@
#cmakedefine HAVE_PTHREAD_GETTHREADID_NP @HAVE_PTHREAD_GETTHREADID_NP@
+#cmakedefine HAVE_PR_SET_NAME @HAVE_PR_SET_NAME@
+#cmakedefine HAVE_OS_THREAD_NAMES @HAVE_OS_THREAD_NAMES@
#endif // MP_CONFIG_H
diff --git a/src/mp/util.cpp b/src/mp/util.cpp
index e1eae4d..281dee6 100644
--- a/src/mp/util.cpp
+++ b/src/mp/util.cpp
@@ -177,9 +177,11 @@ void KillAndReapChild(ProcessId pid)
} // namespace
// Copied from https://github.com/bitcoin/bitcoin/blob/d3e40af2597/src/util/threadnames.cpp#L21-L36
+// Keep the conditions below in sync with HAVE_OS_THREAD_NAMES in
+// cmake/pthread_checks.cmake.
void SetOsThreadName(const char* name)
{
-#if defined(PR_SET_NAME)
+#if defined(HAVE_PR_SET_NAME)
// Only the first 15 characters are used (16 - NUL terminator)
::prctl(PR_SET_NAME, name, 0, 0, 0);
#elif defined(HAVE_PTHREAD_SETNAME_NP_3ARG)
diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp
index 45db1f9..566164a 100644
--- a/test/mp/test/test.cpp
+++ b/test/mp/test/test.cpp
@@ -27,6 +27,7 @@
#include <list> // IWYU pragma: keep
#include <map>
#include <memory>
+#include <mp/config.h>
#include <mp/proxy.h>
#include <mp/proxy.capnp.h>
#include <mp/proxy-io.h>
@@ -667,27 +668,9 @@ KJ_TEST("Call async IPC method dispatched to pool thread")
}
}
-//! Return whether OS thread names set by SetOsThreadName()
-//! can be read back by ThreadName() on this platform.
-static bool OsThreadNamesSupported()
-{
- static const bool supported = [] {
- bool found = false;
- std::thread([&found] {
- SetOsThreadName("mptest-probe");
- found = ThreadName("").find("/mptest-probe-") != std::string::npos;
- }).join();
- if (!found) {
- KJ_LOG(WARNING, "OS thread names cannot be set and read back on this platform; skipping thread name tests");
- }
- return found;
- }();
- return supported;
-}
-
+#ifdef HAVE_OS_THREAD_NAMES
KJ_TEST("Worker thread has OS thread name")
{
- if (!OsThreadNamesSupported()) return;
TestSetup setup;
ProxyClient<messages::FooInterface>* foo = setup.client.get();
foo->initThreadMap();
@@ -702,7 +685,6 @@ KJ_TEST("Worker thread has OS thread name")
KJ_TEST("Pool thread has OS thread name")
{
- if (!OsThreadNamesSupported()) return;
TestSetup setup;
ProxyClient<messages::FooInterface>* foo = setup.client.get();
foo->initThreadMap();
@@ -735,7 +717,6 @@ KJ_TEST("Pool thread has OS thread name")
KJ_TEST("Async cleanup thread has OS thread name")
{
- if (!OsThreadNamesSupported()) return;
std::promise<std::string> thread_name;
{
TestSetup setup;
@@ -747,6 +728,7 @@ KJ_TEST("Async cleanup thread has OS thread name")
const std::string name{thread_name.get_future().get()};
KJ_EXPECT(name.find("/capnp-async-") != std::string::npos, name);
}
+#endif // HAVE_OS_THREAD_NAMES
KJ_TEST("Call async IPC method without thread or pool errors correctly")
{ |
I considered this option while working on this PR. It implies more maintenance effort. For example, whoever adds Windows support to |
|
I'd prefer adding illumos's two-argument |
Would agree I don't think the runtime probe is a good idea because it seems like it could let our code break and then our tests not catch the breakage because they are automatically skipped. Or if a runtime check is necessary would like to know why. Like do these platforms provide APIs that compile but don't work at runtime? |
|
Thank you for the review and your feedback!
Tests that fail whenever thread naming does not work would be ideal, but that is not what happens today. Consider OpenBSD, where the thread name tests are silently compiled out and |
This PR is a follow-up to #324.
The “has OS thread name” tests were compiled whenever
pthread_getname_np()is available, but they also need a workingSetOsThreadName(). For example, this happened while working on #324, before support for NetBSD’spthread_setname_np()was added (see this comment). The same currently happens on illumos-based systems.This PR replaces the
HAVE_PTHREAD_GETNAME_NPguard with a runtime probe. If the probe fails, the three tests log a warning and return early.Possible follow-ups:
Add an illumos job to CI to prevent regressions (
I have a WIP branch for thatsee ci: Add OpenIndiana job #393).Add support for illumos’s two-argument
pthread_setname_np()toSetOsThreadName(), which is the actual fix for the illumos failures.