Skip to content

Link Threads::Threads in tests that use threads directly#2112

Open
jnooree wants to merge 1 commit into
abseil:masterfrom
jnooree:fix/test-pthread-link
Open

Link Threads::Threads in tests that use threads directly#2112
jnooree wants to merge 1 commit into
abseil:masterfrom
jnooree:fix/test-pthread-link

Conversation

@jnooree

@jnooree jnooree commented Jul 16, 2026

Copy link
Copy Markdown

Summary

Link Threads::Threads in tests that use threads directly.

Problem

Building Abseil as a monolithic shared library (-DABSL_BUILD_MONOLITHIC_SHARED_LIBS=ON with -DBUILD_SHARED_LIBS=ON) with tests enabled fails to link on platforms where libpthread is a separate DSO (glibc < 2.34):

ld: absl_lifetime_test.dir/lifetime_test.cc.o: undefined reference to symbol 'pthread_join@@GLIBC_2.2.5'
ld: /lib64/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Root cause

In monolithic-DLL mode, absl_cc_test collapses every in-DLL absl:: dependency down to the single abseil_dll target. abseil_dll links Threads::Threads privately (CMake/AbseilDll.cmake), so -lpthread is not propagated to the test executables.

  • lifetime_test links no GoogleTest target (it has its own main), so its entire link line is just libabseil_dll.so. It uses std::thread directly → unresolved pthread_joinhard link failure.
  • ~46 other thread-using tests only link today because GTest::gtest's imported target happens to drag -lpthread onto their link line. That is a fragile, incidental transitive dependency — exactly what breaks under the monolithic DLL once a test stops pulling GoogleTest.

Fix

Every test that references pthread symbols now depends on Threads::Threads explicitly, matching the existing convention already used by low_level_alloc_test and thread_identity_test.

Notes

  • Rather than guess from source greps, the exact set was measured. All tests were built inside quay.io/pypa/manylinux_2_28_x86_64 (glibc 2.28) container with -Wl,--as-needed -lpthread appended at the end of every test link line. The binaries that retained libpthread, minus the two that already declared Threads::Threads, are exactly the 47 targets fixed here.
  • Some tests — e.g. config_test, errno_saver_test, several log_* — pull pthread via thread-local storage pthread_getspecific/pthread_setspecific rather than std::thread. They still genuinely need libpthread on old glibc, so they are included.
  • No change needed for Bazel. Bazel has no monolithic-DLL mode; thread-using libraries (base, malloc_internal, synchronization) carry linkopts = ["-pthread"], and Bazel propagates library linkopts transitively through deps to test binaries.

These tests reference pthread symbols (via std::thread or thread-local
storage) but relied on transitive linkage through the gtest shared library
to pull in libpthread. That is fragile and breaks when building the
monolithic shared library (ABSL_BUILD_MONOLITHIC_SHARED_LIBS) on platforms
where libpthread is a separate DSO (glibc < 2.34): the monolithic DLL links
Threads::Threads privately, so a test whose only non-gtest dependency is the
DLL (e.g. lifetime_test) fails to link with an undefined reference to
pthread_join.

Depend on Threads::Threads explicitly, matching low_level_alloc_test and
thread_identity_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.

1 participant