Properly fail with ESRCH when pthread_killing an exited thread.
Previously, we were callign tgkill(pid, 0, signal) instead, which would
fail with EINVAL instead.
Test: bionic-unit-tests
Change-Id: I25b127dcf347e0223274502b0516a950b6c2093e
diff --git a/libc/bionic/pthread_kill.cpp b/libc/bionic/pthread_kill.cpp
index 3761a75..1531574 100644
--- a/libc/bionic/pthread_kill.cpp
+++ b/libc/bionic/pthread_kill.cpp
@@ -36,7 +36,9 @@
ErrnoRestorer errno_restorer;
pid_t tid = pthread_gettid_np(t);
- if (tid == -1) return ESRCH;
+
+ // tid gets reset to 0 on thread exit by CLONE_CHILD_CLEARTID.
+ if (tid == 0 || tid == -1) return ESRCH;
return (tgkill(getpid(), tid, sig) == -1) ? errno : 0;
}
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index fc8945c..e68f1ff 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -31,6 +31,7 @@
#include <unwind.h>
#include <atomic>
+#include <future>
#include <vector>
#include <android-base/parseint.h>
@@ -539,6 +540,25 @@
ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
}
+TEST(pthread, pthread_kill__exited_thread) {
+ static std::promise<pid_t> tid_promise;
+ pthread_t thread;
+ ASSERT_EQ(0, pthread_create(&thread, nullptr,
+ [](void*) -> void* {
+ tid_promise.set_value(gettid());
+ return nullptr;
+ },
+ nullptr));
+
+ pid_t tid = tid_promise.get_future().get();
+ while (TEMP_FAILURE_RETRY(syscall(__NR_tgkill, getpid(), tid, 0)) != -1) {
+ continue;
+ }
+ ASSERT_EQ(ESRCH, errno);
+
+ ASSERT_EQ(ESRCH, pthread_kill(thread, 0));
+}
+
TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) {
pthread_t dead_thread;
MakeDeadThread(dead_thread);