Skip to content

test: Skip thread name tests where OS thread names do not work - #392

Open
hebasto wants to merge 1 commit into
bitcoin-core:masterfrom
hebasto:261106-thread-name-tests
Open

hebasto wants to merge 1 commit into
bitcoin-core:masterfrom
hebasto:261106-thread-name-tests

Conversation

@hebasto

@hebasto hebasto commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

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 working SetOsThreadName(). For example, this happened while working on #324, before support for NetBSD’s pthread_setname_np() was added (see this comment). The same currently happens on illumos-based systems.

This PR replaces the HAVE_PTHREAD_GETNAME_NP guard with a runtime probe. If the probe fails, the three tests log a warning and return early.


Possible follow-ups:

  1. Add an illumos job to CI to prevent regressions (I have a WIP branch for that see ci: Add OpenIndiana job #393).

  2. Add support for illumos’s two-argument pthread_setname_np() to SetOsThreadName(), which is the actual fix for the illumos failures.

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.
@DrahtBot

DrahtBot commented Nov 6, 2026 •

Copy link
Copy Markdown

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK xyzconstant

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #342 (Allow request cancellation for wrapped C++ methods by xyzconstant)

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.

@hebasto

hebasto commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

cc @ViniciusCestarii as the author of #324.

@hebasto hebasto mentioned this pull request Nov 6, 2026
@hebasto

hebasto commented Nov 6, 2026 •

Copy link
Copy Markdown
Member Author

From the illumos CI job added in #393, which is based on this PR:

[ TEST ] test.cpp:688: Worker thread has OS thread name
/home/runner/work/libmultiprocess/libmultiprocess/test/mp/test/test.cpp:681: OS thread names cannot be set and read back on this platform; skipping thread name tests
[ PASS ] test.cpp:688: Worker thread has OS thread name (241 μs)
[ TEST ] test.cpp:703: Pool thread has OS thread name
[ PASS ] test.cpp:703: Pool thread has OS thread name (0 μs)
[ TEST ] test.cpp:736: Async cleanup thread has OS thread name
[ PASS ] test.cpp:736: Async cleanup thread has OS thread name (0 μs)

hebasto pushed a commit to hebasto/bitcoin-core-nightly that referenced this pull request Nov 6, 2026
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
@xyzconstant

Copy link
Copy Markdown
Contributor

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")
 {

@hebasto

hebasto commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

@xyzconstant

I was wondering if we could keep the compile-time check, and my agent came up with the following diff...

I considered this option while working on this PR. It implies more maintenance effort. For example, whoever adds Windows support to SetOsThreadName() will have to remember to update pthread_checks.cmake as well. The runtime probe cannot get out of sync.

@ViniciusCestarii

Copy link
Copy Markdown
Contributor

I'd prefer adding illumos's two-argument pthread_setname_np() to SetOsThreadName() over the runtime probe. The probe turns a SetOsThreadName() regression on any platform into a warning with passing tests. I also believe a failing test on a new platform is a useful reminder to add support, as happened with NetBSD in #324.

@ryanofsky

Copy link
Copy Markdown
Collaborator

I'd prefer adding illumos's two-argument pthread_setname_np() to SetOsThreadName() over the runtime probe.

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?

@hebasto

hebasto commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

@ViniciusCestarii @ryanofsky

Thank you for the review and your feedback!

I also believe a failing test on a new platform is a useful reminder to add support...

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.

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 SetOsThreadName() does nothing, yet CI is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants