From 6335017058dc433aa19a9a36d14feae91372438d Mon Sep 17 00:00:00 2001 From: Paul Shved Date: Thu, 3 Nov 2022 17:44:10 -0700 Subject: [PATCH 1/2] Remove the call to isAlive() which is broken The update of glibc from 2.31 to 2.35 in Ubuntu changed the semantics of pthread_kill. It used to return an error when a dead thread exited, and now it returns 0. This new behavior is POSIX-compatible as the result of pthread_kill on a thread that died produces undefined behavior, so techincally 0 is valid result. This isAlive() will return that a dead thread is alive in the newer glibc versions. This patch removes the use of isAlive and instead uses tryjoin, which should behave correctly for dead threads. --- lib/xspublic/xscommon/threading.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/xspublic/xscommon/threading.cpp b/lib/xspublic/xscommon/threading.cpp index 261c353..3177a2e 100644 --- a/lib/xspublic/xscommon/threading.cpp +++ b/lib/xspublic/xscommon/threading.cpp @@ -383,10 +383,18 @@ void StandardThread::stopThread(void) noexcept // return; // } //#else - while (isAlive()) - xsYield(); + int rv = 0; + while (true) { + rv = pthread_tryjoin_np(m_thread, NULL); + if (rv == 0) { + break; + } + if (errno == EBUSY) { + xsYield(); + } + } //#endif - if (pthread_join(m_thread, NULL)) + if (rv != 0) { switch (errno) { From 706ea91f4a91f7fa7a9b87ff6c9f7eb79407fdf4 Mon Sep 17 00:00:00 2001 From: Paul Shved Date: Fri, 11 Nov 2022 17:53:49 -0800 Subject: [PATCH 2/2] Add check for error condition do not spin forever if some thread is invalid or it's a EDEADLK --- lib/xspublic/xscommon/threading.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/xspublic/xscommon/threading.cpp b/lib/xspublic/xscommon/threading.cpp index 3177a2e..e036ec2 100644 --- a/lib/xspublic/xscommon/threading.cpp +++ b/lib/xspublic/xscommon/threading.cpp @@ -391,6 +391,9 @@ void StandardThread::stopThread(void) noexcept } if (errno == EBUSY) { xsYield(); + } else { + // Some other error--maybe the thread is invalid? + break; } } //#endif