From 751fae1effde32b441dd232a0bf3340a419f2d17 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Thu, 13 Feb 2020 23:22:12 +0000 Subject: [PATCH] Return success, instead of ESRCH, from pthread_cancel(3) applied to the exited but not yet joined thread. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before, if the thread exited but was not yet joined, we returned ESRCH. According to IEEE Std 1003.1™-2017 recommendation in the description of pthread_cancel(3): If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an [ESRCH] error. So it seems desirable to not return ESRCH until the lifetime of the thread ID ends. According to the section 2.9.2 Thread IDs, The lifetime of a thread ID ends after the thread terminates if it was created with the detachstate attribute set to PTHREAD_CREATE_DETACHED or if pthread_detach() or pthread_join() has been called for that thread. In other words, lifetime for thread ID of exited but not yet joined thread did not ended yet. Prompted by: cperciva Sponsored by: The FreeBSD Foundation MFC after: 1 week --- lib/libthr/thread/thr_cancel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/libthr/thread/thr_cancel.c b/lib/libthr/thread/thr_cancel.c index 4194fe180565..7e78fe9163c7 100644 --- a/lib/libthr/thread/thr_cancel.c +++ b/lib/libthr/thread/thr_cancel.c @@ -71,7 +71,7 @@ _thr_cancel(pthread_t pthread) * _thr_find_thread and THR_THREAD_UNLOCK will enter and leave critical * region automatically. */ - if ((ret = _thr_find_thread(curthread, pthread, 0)) == 0) { + if ((ret = _thr_find_thread(curthread, pthread, 1)) == 0) { if (!pthread->cancel_pending) { pthread->cancel_pending = 1; if (pthread->state != PS_DEAD)