From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id BE43A385AC2D; Mon, 12 Sep 2022 08:05:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE43A385AC2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1662969939; bh=xiQ2wW1b6KKqiOlPTDEItpeo7/mAb4jW19jrM+lTI80=; h=From:To:Subject:Date:From; b=auPIr4zTdhvDopZ2/I50t22TGMYX1nXO2umuTFRk+59yQ9P3W12JDhiKxdMYbGARt r+0zIAhjyppn95KGzBBp0EkWaCMp2NzgVbyzegTuQfkoZFNfjDSIyItzau7XiEDAf5 UIn+YcXsG24p56bkjKRi9l2vZ9VYK3lVEgmTaQok= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb] Fix abort in selftest run_on_main_thread with ^C X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: abe47e91d8c4b40429bc5834f8a91c25b10172ca X-Git-Newrev: 3d36a6396fbfacd7b1941527d84ff6c0f40ff121 Message-Id: <20220912080539.BE43A385AC2D@sourceware.org> Date: Mon, 12 Sep 2022 08:05:39 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D3d36a6396fbf= acd7b1941527d84ff6c0f40ff121 commit 3d36a6396fbfacd7b1941527d84ff6c0f40ff121 Author: Tom de Vries Date: Mon Sep 12 10:05:18 2022 +0200 [gdb] Fix abort in selftest run_on_main_thread with ^C =20 When running selftest run_on_main_thread and pressing ^C, we can run in= to: ... Running selftest run_on_main_thread. terminate called without an active exception =20 Fatal signal: Aborted ... =20 The selftest function looks like this: ... static void run_tests () { std::thread thread; =20 done =3D false; =20 { gdb::block_signals blocker; =20 thread =3D std::thread (set_done); } =20 while (!done && gdb_do_one_event () >=3D 0) ; =20 /* Actually the test will just hang, but we want to test something. */ SELF_CHECK (done); =20 thread.join (); } ... =20 The error message we see is due to the destructor of thread being calle= d while thread is joinable. =20 This is supposed to be taken care of by thread.join (), but the ^C prev= ents that one from being called, while the destructor is still called. =20 Fix this by ensuring thread.join () is called (if indeed required) befo= re the destructor using SCOPE_EXIT. =20 Tested on x86_64-linux. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29549 Diff: --- gdb/unittests/main-thread-selftests.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gdb/unittests/main-thread-selftests.c b/gdb/unittests/main-thr= ead-selftests.c index 7e1a30d7e80..8ab0c445d42 100644 --- a/gdb/unittests/main-thread-selftests.c +++ b/gdb/unittests/main-thread-selftests.c @@ -20,6 +20,7 @@ #include "defs.h" #include "gdbsupport/selftest.h" #include "gdbsupport/block-signals.h" +#include "gdbsupport/scope-exit.h" #include "run-on-main-thread.h" #include "gdbsupport/event-loop.h" #if CXX_STD_THREAD @@ -52,6 +53,11 @@ run_tests () { gdb::block_signals blocker; =20 + SCOPE_EXIT + { + if (thread.joinable ()) + thread.join (); + }; thread =3D std::thread (set_done); } =20 @@ -61,8 +67,6 @@ run_tests () /* Actually the test will just hang, but we want to test something. */ SELF_CHECK (done); - - thread.join (); } =20 #endif