util: set os-level thread names on Windows #35884

pull ViniciusCestarii wants to merge 1 commits into bitcoin:master from ViniciusCestarii:name-windows-os-thread changing 1 files +9 −0
  1. ViniciusCestarii commented at 6:41 PM on August 4, 2026: contributor

    Update SetThreadName to set os-level thread names on Windows too.

    This is useful for debugging-ergonomics on Windows. Threads currently show up unnamed in debuggers, crash dumps on Windows and mismatch what's documented under https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#threads.

    Tested with the mingw cross build running on Windows 11, print from WinDbg:

    <img width="713" height="631" alt="image" src="https://github.com/user-attachments/assets/05e03383-c9b1-4e6b-91f3-9088b2fc7e90" />

  2. DrahtBot added the label Utils/log/libs on Aug 4, 2026
  3. DrahtBot commented at 6:41 PM on August 4, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35884.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    Type Reviewers
    ACK hebasto, l0rinc, winterrdog

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. l0rinc commented at 6:45 PM on August 4, 2026: contributor

    Concept ACK

  5. maflcko commented at 8:20 AM on August 18, 2026: member

    Should netbsd be supported as well?

  6. in src/util/threadnames.cpp:22 in d04e650169
      17 | @@ -18,6 +18,10 @@
      18 |  #include <sys/prctl.h>
      19 |  #endif
      20 |  
      21 | +#ifdef WIN32
      22 | +#include <windows.h>
    


    hebasto commented at 10:37 AM on August 18, 2026:

    Why not <processthreadsapi.h>?


    ViniciusCestarii commented at 1:17 PM on August 18, 2026:

    Good point, switched to <processthreadsapi.h> on 531768ab8cfcc2b73afd969fe590c19247d42d6e.


    ViniciusCestarii commented at 2:46 PM on August 18, 2026:

    CI failed:

      C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winnt.h(169,1): error C1189: #error:  "No Target Architecture" [D:\a\bitcoin\bitcoin\build\src\util\bitcoin_util.vcxproj]
    

    I will take a look at it.


    ViniciusCestarii commented at 5:57 PM on August 18, 2026:

    I reproduced it in my Windows 11 machine and have found that MSVC processthreadsapi.h insn't self contained and only the windows.h set the architecture flag for it. So I will revert back to include windows.h to work on MSVC build.

  7. in src/util/threadnames.cpp:37 in d04e650169
      32 | @@ -29,6 +33,10 @@ static void SetThreadName(const char* name)
      33 |      pthread_set_name_np(pthread_self(), name);
      34 |  #elif defined(__APPLE__)
      35 |      pthread_setname_np(name);
      36 | +#elif defined(WIN32)
      37 | +    // Thread names are ASCII-only, so widening each character is sufficient.
    


    hebasto commented at 10:42 AM on August 18, 2026:

    Could you please clarify the comment? "... is sufficient" for what?


    ViniciusCestarii commented at 1:18 PM on August 18, 2026:

    Sufficient as a conversion to UTF-16. SetThreadDescription takes a PCWSTR, and in general converting const char* to wchar_t* needs to know the source encoding, since non-ASCII bytes are multibyte sequences that a per-character widening would mangle. All thread names here are hardcoded ASCII literals and maps 1:1 onto the same UTF-16 code unit, which means the std::wstring construction is correct and MultiByteToWideChar isn't needed.

    Updated comment on 531768ab8cfcc2b73afd969fe590c19247d42d6e so it explains better.


    hebasto commented at 1:37 PM on August 18, 2026:

    Is this still needed after #32380?


    ViniciusCestarii commented at 2:25 PM on August 18, 2026:

    I didn't know there were -A variants, pretty nice.

    I searched for the -A version of SetThreadDescription but I found no variant, windows api documentation states no variant for SetThreadDescription (req.unicode-ansi is empty). So it is needed.

  8. ViniciusCestarii force-pushed on Aug 18, 2026
  9. ViniciusCestarii commented at 1:21 PM on August 18, 2026: contributor

    Thanks for the reviews! Forced-push 531768ab8cfcc2b73afd969fe590c19247d42d6e to include <processthreadsapi.h> instead of <windows.h> and also updated comment on std::wstring construction to explain better why it is sufficient to convert thread name to UTF-16 and doens't need MultiByteToWideChar.

  10. ViniciusCestarii commented at 1:34 PM on August 18, 2026: contributor

    Should netbsd be supported as well?

    IMO it should since it is a supported build target.

  11. DrahtBot added the label CI failed on Aug 18, 2026
  12. util: set os-level thread names on Windows dd669f40b9
  13. ViniciusCestarii force-pushed on Aug 18, 2026
  14. ViniciusCestarii commented at 6:01 PM on August 18, 2026: contributor

    Forced push dd669f40b98bb864bb9713673f0c38d946040591 to include <windows.h> back, since <processthreadsapi.h> isn't self contained for MSVC build.

  15. DrahtBot removed the label CI failed on Aug 18, 2026
  16. hebasto approved
  17. hebasto commented at 3:58 PM on August 20, 2026: member

    ACK dd669f40b98bb864bb9713673f0c38d946040591, tested Guix-built bitcoind.exe on Windows 11 Pro using WinDbg:

    <img width="578" height="659" alt="Image" src="https://github.com/user-attachments/assets/4976b474-6235-4f91-b83a-b4048b61fa86" />

  18. DrahtBot requested review from l0rinc on Aug 20, 2026
  19. l0rinc commented at 5:11 PM on August 20, 2026: contributor

    code review ACK dd669f40b98bb864bb9713673f0c38d946040591

  20. winterrdog commented at 6:08 PM on August 20, 2026: contributor

    utACK dd669f40b98bb864bb9713673f0c38d946040591

  21. fanquake merged this on Aug 21, 2026
  22. fanquake closed this on Aug 21, 2026

  23. hebasto commented at 8:52 AM on August 22, 2026: member

    Build fails on Ubuntu 24.04:

    /home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/src/util/threadnames.cpp: In function ‘void SetThreadName(const char*)’:
    /home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/src/util/threadnames.cpp:40:7: error: ‘::SetThreadDescription’ has not been declared; did you mean ‘SetThreadInformation’?
       40 |     ::SetThreadDescription(::GetCurrentThread(), wname.c_str());
          |       ^~~~~~~~~~~~~~~~~~~~
          |       SetThreadInformation
    gmake[2]: *** [src/util/CMakeFiles/bitcoin_util.dir/build.make:440: src/util/CMakeFiles/bitcoin_util.dir/threadnames.cpp.obj] Error 1
    gmake[1]: *** [CMakeFiles/Makefile2:1416: src/util/CMakeFiles/bitcoin_util.dir/all] Error 2
    
  24. in src/util/threadnames.cpp:40 in dd669f40b9
      32 | @@ -29,6 +33,11 @@ static void SetThreadName(const char* name)
      33 |      pthread_set_name_np(pthread_self(), name);
      34 |  #elif defined(__APPLE__)
      35 |      pthread_setname_np(name);
      36 | +#elif defined(WIN32)
      37 | +    // Thread names are ASCII-only, so widening each character is sufficient as
      38 | +    // a conversion to UTF-16.
      39 | +    const std::wstring wname{name, name + std::strlen(name)};
      40 | +    ::SetThreadDescription(::GetCurrentThread(), wname.c_str());
    


    hebasto commented at 9:06 AM on August 22, 2026:

    Apparently, SetThreadDescription is only available in MinGW-w64 >= 12.0.

    See https://github.com/mingw-w64/mingw-w64/commit/3a137bd87ea83d174f4a225568b75cabdbc70852.

  25. hebasto commented at 9:13 AM on August 22, 2026: member

    @maflcko @fanquake

    Should we document the MinGW-w64 minimum required version and / or revert this change?

  26. ViniciusCestarii commented at 2:20 PM on August 22, 2026: contributor

    Opened #36057, which checks for the symbol at configure time so older mingw-w64 builds fine.

    Happy to document a minimum or revert instead if that's preferred.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-09-04 07:51 UTC

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