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

pull hebasto wants to merge 1 commits into bitcoin-core:master from hebasto:261106-thread-name-tests changing 1 files +21 −3
  1. hebasto commented at 3:02 PM on November 6, 2026: 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 #393).

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

  2. test: Skip thread name tests where OS thread names do not work
    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.
    88fd4610c2
  3. DrahtBot commented at 3:03 PM on November 6, 2026: none

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. hebasto commented at 3:04 PM on November 6, 2026: member

    cc @ViniciusCestarii as the author of #324.

  5. hebasto commented at 4:08 PM on November 6, 2026: member

    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)
    
  6. hebasto referenced this in commit a154d982be on Nov 6, 2026
  7. xyzconstant commented at 3:00 AM on November 7, 2026: 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](/bitcoin-core-multiprocess/contributor/have/)_PTHREAD_SETNAME_NP_3ARG@
     #cmakedefine HAVE_PTHREAD_THREADID_NP [@HAVE](/bitcoin-core-multiprocess/contributor/have/)_PTHREAD_THREADID_NP@
     #cmakedefine HAVE_PTHREAD_GETTHREADID_NP [@HAVE](/bitcoin-core-multiprocess/contributor/have/)_PTHREAD_GETTHREADID_NP@
    +#cmakedefine HAVE_PR_SET_NAME [@HAVE](/bitcoin-core-multiprocess/contributor/have/)_PR_SET_NAME@
    +#cmakedefine HAVE_OS_THREAD_NAMES [@HAVE](/bitcoin-core-multiprocess/contributor/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")
     {
    
  8. hebasto commented at 9:24 AM on November 7, 2026: member

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

  9. ViniciusCestarii commented at 4:12 PM on November 8, 2026: 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.

  10. ryanofsky commented at 2:53 PM on November 10, 2026: 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?

  11. hebasto commented at 6:19 PM on November 15, 2026: member

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

  12. ViniciusCestarii commented at 11:30 AM on November 16, 2026: contributor

    Fair point about OpenBSD. The current guard flagged missing support on those platforms for mere coincidence and not design. OpenBSD is an existing gap that can be fixed separately and I'm happy to open a PR for it.

    The main point still holds, though. With probe CI still turns green if it breaks in a platform that is supported today.

  13. in test/mp/test/test.cpp:686 in 88fd4610c2
     682 | +            KJ_LOG(WARNING, "OS thread names cannot be set and read back on this platform; skipping thread name tests");
     683 | +        }
     684 | +        return found;
     685 | +    }();
     686 | +    return supported;
     687 | +}
    


    ryanofsky commented at 7:23 PM on November 16, 2026:

    re: #392 (comment)

    In commit "test: Skip thread name tests where OS thread names do not work" (88fd4610c2ebe488d42824e8beb1839db6b1a09a)

    Consider OpenBSD, where the thread name tests are silently compiled out and SetOsThreadName() does nothing, yet CI is green.

    Agree OpenBSD is a real gap, but I don't think the probe helps with it: on OpenBSD the probe also fails and the tests are skipped, so the only change from master is a warning in the test log. On platforms that work today, the probe makes a later breakage pass the same way.

    I think with #396 this PR isn't needed for illumos anymore. The current HAVE_PTHREAD_GETNAME_NP guard caught exactly the case it should catch: names can be read back but SetOsThreadName() had no branch to set them. #396 adds that branch, so rebasing #393 on #396 instead of this PR should show the tests passing on illumos without a probe.

    For OpenBSD, the no-op looks like it came from #324 porting this code: the original in Bitcoin Core calls pthread_set_name_np() for __OpenBSD__ too, but here that branch is keyed on HAVE_PTHREAD_GETTHREADID_NP, which only FreeBSD has. Restoring it, and reading names back with pthread_get_name_np() in ThreadName(), should cover OpenBSD as @ViniciusCestarii offered #392 (comment). After that, every platform in CI could set and read names, and the HAVE_PTHREAD_GETNAME_NP guard around the tests could be dropped, so a new platform without support would fail the tests instead of skipping them, which I think is the ideal you described.


    hebasto commented at 7:57 PM on November 16, 2026:

    Consider OpenBSD, where the thread name tests are silently compiled out and SetOsThreadName() does nothing, yet CI is green.

    Agree OpenBSD is a real gap, but I don't think the probe helps with it: on OpenBSD the probe also fails and the tests are skipped, so the only change from master is a warning in the test log. On platforms that work today, the probe makes a later breakage pass the same way.

    Catching unsupported platforms was never a goal of this PR.

    I fundamentally disagree with overloading tests with such a task in addition to actually testing the code.

    Closing anyway.


    ryanofsky commented at 8:58 PM on November 16, 2026:

    re: #392 (review)

    Catching unsupported platforms was never a goal of this PR.

    Agreed, and the last paragraph of my comment was wrong to suggest dropping the guard entirely. I think the tests should be skipped on platforms that can't read thread names and run everywhere else, so a bug in SetOsThreadName() on a supported platform makes them fail instead of skip. HAVE_PTHREAD_GETNAME_NP mostly does this, but it misses platforms with a different getter: I believe OpenBSD has pthread_get_name_np(), so the tests are skipped there even though it supports thread names.

    Ideally SetOsThreadName() and ThreadName() would both support OpenBSD, so the tests run and pass there instead of being skipped. The guard could then be one macro saying whether ThreadName() can read names on the platform, used both in ThreadName() and around the tests so they can't get out of sync.

    Thanks for closing this and for the quick #396 and #393 updates. The OpenIndiana job on #393 now runs and passes all three thread name tests.

  14. hebasto closed this on Nov 16, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/libmultiprocess. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-17 18:30 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me