Skip to content

[6.2] Fix worker thread name handling on Linux#935

Open
KushalP wants to merge 5 commits intoswiftlang:release/6.2from
KushalP:release/6.2-worker-thread-name-cherry-pick
Open

[6.2] Fix worker thread name handling on Linux#935
KushalP wants to merge 5 commits intoswiftlang:release/6.2from
KushalP:release/6.2-worker-thread-name-cherry-pick

Conversation

@KushalP
Copy link
Copy Markdown
Contributor

@KushalP KushalP commented Mar 17, 2026

Explanation: The existing functionality is brittle on Linux, and the UX can be improved.
Scope: Narrow. Hard codes the thread name for queue workers.
Original PRs:

Risk: Very low.
Testing: CI

**Explanation:** The existing functionality is brittle on Linux, and the UX can be improved.
**Scope:** Narrow. Hard codes the thread name for queue workers.
**Original PRs:** swiftlang#934
**Risk:** Very low.
**Testing:** CI
@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Mar 17, 2026

@swift-ci please test.

@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Mar 17, 2026

@swift-ci Please test Windows platform.

@KushalP KushalP marked this pull request as draft March 18, 2026 23:17
## Problem

PR swiftlang#934 (fd153d8) adds a `dispatch_thread_name` regression test that
asserts worker threads have a non-empty name. This test fails on
Amazon Linux 2 CI because `HAVE_PTHREAD_SETNAME_NP` is never set.

The root cause is CMake's `check_function_exists` attempting to link a
test program without `-lpthread`. On glibc < 2.34 (Amazon Linux 2
ships glibc 2.26), `pthread_setname_np` lives in a separate
`libpthread.so`[1].

The link fails, `HAVE_PTHREAD_SETNAME_NP` evaluates to false, and the
`pthread_setname_np` call in `queue.c` is compiled out. Worker threads
get no name, and the test assertion `strlen(thread_name) > 0` fails.

## Fix

Replace `check_function_exists` with `check_symbol_exists` against
`<pthread.h>`, and temporarily add `Threads::Threads` to
`CMAKE_REQUIRED_LIBRARIES` so the linker can resolve it.

This matches the pattern used by `llvm/cmake/config-ix.cmake`[2].

Guard the `dispatch_thread_name` test on `HAVE_PTHREAD_SETNAME_NP` so
it is only built when available.

## References

[1] https://github.com/bminor/glibc/blob/glibc-2.34/NEWS#L12-L16
[2] https://github.com/swiftlang/llvm-project/blob/8050c17d40a6bcf781c3fdf7120b8f2d3c6363ac/llvm/cmake/config-ix.cmake#L434-L443
@KushalP KushalP marked this pull request as ready for review March 19, 2026 16:53
@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Mar 19, 2026

@swift-ci please test.

@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Mar 19, 2026

@swift-ci please test.

@etcwilde
Copy link
Copy Markdown
Member

Please ensure that this works cross-platform before merging this (FreeBSD and OpenBSD are both running into issues with it on main now). 6.2 is required for bringing Swift up on new platforms and Dispatch is a required library for building the toolchain, so we do need this to work reliably.

@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Mar 23, 2026

@etcwilde I'll work on a PR now. What is the @swift-ci command I can use to run CI for FreeBSD/OpenBSD ?

@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Mar 23, 2026

Do not merge until #937 is merged to main and we've cherry picked those changes into this PR.

Here we just do this by token swapping, which feels a little dirty, but
fixing it otherwise would induce a larger diff to factor out and switch
on the pthread_get_name_np spelling variations.

Fixing this requires making setting the thread name non-Linux specific.
This is safe, because we've added cmake conditionals for the symbols.
@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Mar 24, 2026

@swift-ci please test.

Copy link
Copy Markdown
Contributor

@al45tair al45tair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Co-authored-by: Alastair Houghton <alastair@alastairs-place.net>
@KushalP
Copy link
Copy Markdown
Contributor Author

KushalP commented Apr 3, 2026

@swift-ci please test.

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