From owner-freebsd-threads@freebsd.org Tue Dec 6 15:32:50 2016 Return-Path: Delivered-To: freebsd-threads@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0C76CC69E53 for ; Tue, 6 Dec 2016 15:32:50 +0000 (UTC) (envelope-from dimitri.staessens@intec.ugent.be) Received: from smtp2.ugent.be (smtp2.ugent.be [157.193.49.126]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B5145122B for ; Tue, 6 Dec 2016 15:32:48 +0000 (UTC) (envelope-from dimitri.staessens@intec.ugent.be) Received: from localhost (mcheck3.ugent.be [157.193.71.89]) by smtp2.ugent.be (Postfix) with ESMTP id CF529B21B3; Tue, 6 Dec 2016 16:32:46 +0100 (CET) X-Virus-Scanned: by UGent DICT Received: from smtp2.ugent.be ([157.193.49.126]) by localhost (mcheck3.ugent.be [157.193.43.11]) (amavisd-new, port 10024) with ESMTP id NV9td_TAwXQR; Tue, 6 Dec 2016 16:32:46 +0100 (CET) Received: from mail2.intec.ugent.be (mail2.intec.ugent.be [157.193.214.245]) by smtp2.ugent.be (Postfix) with ESMTP id 24323B2308; Tue, 6 Dec 2016 16:32:46 +0100 (CET) Received: from [10.10.131.46] (hal.ilabt.iminds.be [193.191.148.129]) (using TLSv1 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: dstaesse) by mail2.intec.ugent.be (Postfix) with ESMTPSA id 0DFE42C; Tue, 6 Dec 2016 16:32:46 +0100 (CET) Subject: Re: Unlocking a robust mutex in a cleanup handler To: Konstantin Belousov References: <119e59d4-6125-f313-e6e6-67055a15d224@intec.ugent.be> <20161206112558.GN54029@kib.kiev.ua> <6a7139cd-b6db-d078-ee5e-b7c590eb13d1@intec.ugent.be> <20161206144812.GS54029@kib.kiev.ua> Cc: freebsd-threads@freebsd.org From: Dimitri Staessens Message-ID: <35726dbb-75f7-682d-ad41-c78b96675485@intec.ugent.be> Date: Tue, 6 Dec 2016 16:30:14 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: <20161206144812.GS54029@kib.kiev.ua> X-Miltered: at jchkm1 with ID 5846DA1E.000 by Joe's j-chkmail (http://helpdesk.ugent.be/email/)! X-j-chkmail-Enveloppe: 5846DA1E.000 from mail2.intec.ugent.be/mail2.intec.ugent.be/157.193.214.245/mail2.intec.ugent.be/ X-j-chkmail-Score: MSGID : 5846DA1E.000 on smtp2.ugent.be : j-chkmail score : . : R=. U=. O=. B=0.000 -> S=0.000 X-j-chkmail-Status: Ham Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Dec 2016 15:32:50 -0000 Dear Konstantin, I didn't get the error, but on my machine the thread never exists the condwait when the pthread_cancel is called. gdb output: $ sudo gdb ./robust_test 3246 GNU gdb 6.1.1 [FreeBSD] Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "amd64-marcel-freebsd"...(no debugging symbols found)... Attaching to program: /usr/home/dstaesse/robust_test, process 3246 Reading symbols from /lib/libthr.so.3...Reading symbols from /usr/lib/debug//lib/libthr.so.3.debug...done. [New Thread 801416500 (LWP 100404/robust_test)] [New Thread 801416000 (LWP 100313/robust_test)] done. Loaded symbols for /lib/libthr.so.3 Reading symbols from /usr/lib/librt.so.1...done. Loaded symbols for /usr/lib/librt.so.1 Reading symbols from /lib/libc.so.7...done. Loaded symbols for /lib/libc.so.7 Reading symbols from /libexec/ld-elf.so.1...done. Loaded symbols for /libexec/ld-elf.so.1 [Switching to Thread 801416000 (LWP 100313/robust_test)] 0x00000008008386ac in _umtx_op_err () from /lib/libthr.so.3 (gdb) info threads * 2 Thread 801416000 (LWP 100313/robust_test) 0x00000008008386ac in _umtx_op_err () from /lib/libthr.so.3 1 Thread 801416500 (LWP 100404/robust_test) _thr_ast (curthread=0x801416500) at /usr/src/lib/libthr/thread/thr_sig.c:271 Current language: auto; currently minimal (gdb) bt #0 0x00000008008386ac in _umtx_op_err () from /lib/libthr.so.3 #1 0x0000000800834df6 in join_common (pthread=, thread_return=, abstime=) at /usr/src/lib/libthr/thread/thr_join.c:125 #2 0x0000000000401186 in main () (gdb) thread 1 [Switching to thread 1 (Thread 801416500 (LWP 100404/robust_test))]#0 _thr_ast (curthread=0x801416500) at /usr/src/lib/libthr/thread/thr_sig.c:271 271 check_suspend(curthread); (gdb) bt #0 _thr_ast (curthread=0x801416500) at /usr/src/lib/libthr/thread/thr_sig.c:271 #1 0x0000000800837a5b in __thr_pshared_offpage (key=, doalloc=) at /usr/src/lib/libthr/thread/thr_pshared.c:86 #2 0x00000008008363cb in cond_wait_common (cond=, mutex=0x800643004, abstime=0x0, cancel=1) at /usr/src/lib/libthr/thread/thr_cond.c:349 #3 0x0000000000400ff2 in blockfunc () #4 0x000000080082ab55 in thread_start (curthread=) at /usr/src/lib/libthr/thread/thr_create.c:289 #5 0x0000000000000000 in ?? () (gdb) cheers, Dimitri On 12/06/16 15:48, Konstantin Belousov wrote: > On Tue, Dec 06, 2016 at 03:17:41PM +0100, Dimitri Staessens wrote: >> Dear Konstantin, >> >> thanks for your immediate response. Please find attached a minimal code >> example. I do hope I'm sending in the correct format, I'm new to the >> community. >> >> The test creates an integer, robust mutex and condition variable in >> shared memory. A thread blocks on that condition variable with the >> associated mutex. After one second, the main thread cancels the blocking >> thread. >> >> I compile as follows: >> >> gcc robust_test.c -lpthread -lrt -o robust_test >> >> and run >> >> ./robust_test >> >> On linux it gives the following output: >> [dstaesse@phoneutria]$ ./robust_test >> Initializing... >> Starting thread... >> Sleeping for one second... >> Thread started... >> Cancelling thread... >> Thread finished. >> Bye. >> >> On FreeBSD I get the following: >> $ ./robust_test >> Initializing... >> Starting thread... >> Sleeping for one second... >> Thread started... >> Cancelling thread... >> Fatal error 'inact_mtx enter' at line 188 in file >> /usr/src/lib/libthr/thread/thr_mutex.c (errno = 0) >> Abort trap (core dumped) > Try this patch. It worked for me. > It is enough to patch and then rebuild only libthr: > cd /usr/src > patch -p1 (cd lib/libthr && make WITHOUT_TESTS=yes all install) > > diff --git a/lib/libthr/thread/thr_cond.c b/lib/libthr/thread/thr_cond.c > index 506b8eca9e7..64d075ca06f 100644 > --- a/lib/libthr/thread/thr_cond.c > +++ b/lib/libthr/thread/thr_cond.c > @@ -224,16 +224,26 @@ cond_wait_kernel(struct pthread_cond *cvp, struct pthread_mutex *mp, > * state and unlock the mutex without making the state > * consistent and the state will be unrecoverable. > */ > - if (error2 == 0 && cancel) > + if (error2 == 0 && cancel) { > + if (robust) { > + _mutex_leave_robust(curthread, mp); > + robust = false; > + } > _thr_testcancel(curthread); > + } > > if (error == EINTR) > error = 0; > } else { > /* We know that it didn't unlock the mutex. */ > _mutex_cv_attach(mp, recurse); > - if (cancel) > + if (cancel) { > + if (robust) { > + _mutex_leave_robust(curthread, mp); > + robust = false; > + } > _thr_testcancel(curthread); > + } > error2 = 0; > } > if (robust) -- Dimitri Staessens Ghent University - imec Dept. of Information Technology (INTEC) Internet Based Communication Networks and Services Technologiepark 15 9052 Zwijnaarde T: +32 9 331 48 70 F: +32 9 331 48 99