From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id C565C386187C for ; Mon, 19 Feb 2024 08:23:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C565C386187C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C565C386187C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708331041; cv=none; b=tYKHrvnh0N0LClGsgzFkpkqfYSWvhUiMvE+te2265SoxPPbf8KqIChmoPzE3Bvk+8aKq6n+loymhI0BlodAgMHr1abiCp9ufPI3foeVbSno1qmz0w5GiFzWCx9w/MIfziXkAkDOWRWn6/o1wQTLeFF1c7x25z5bTOMxmD0N+BRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708331041; c=relaxed/simple; bh=z8c0Ocf/HFFHuqqmgKTD50zkRQnowX7Hbc0nO1Rfqcg=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=NDR85SoAENEdYghn8WmPLp5krTRtFIK0RoRvIAppg3/S6Cls6ree4/7/KOyGePjxZvn2/y3nrUNS2Lbqd/vSyMYQ8cGtvyq1pqhDf6YhE79LuMiDln7gjI79AF0KGPp60cEdvBe4RDEcv6SauQ6tJE+0EA1x1O/GCmuRmdso7/Y= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 41DA1222EE for ; Mon, 19 Feb 2024 08:23:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708331033; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QSRqAzJKEFAmAg6CRXLjxKsvnNlwLGfNtsVuymnSzrI=; b=h1wNxk0Vqs8TFqH479ODRUqhmJ3H/aKY4kuJquc3YZnIg9YalG7ajLv6tXrwCRU60JMLlC SeeFHvvKHVV5AC1K+vcXJIXyuFYZjFdaTv4DG6VcTpwcKSd7zck98MRCBHxfoIjGz01Mtb YeZk3LU2ZCHtd9i8rNWS/3FiH65hEII= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708331033; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QSRqAzJKEFAmAg6CRXLjxKsvnNlwLGfNtsVuymnSzrI=; b=wgeRyP+XzqnY7fLiC4ls6BzOyRVB4o5fshfQV3VlQDUI5lwhtUic7zwNR7r1eOcCOEvIRK GvttK9n2S91/ZvDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708331033; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QSRqAzJKEFAmAg6CRXLjxKsvnNlwLGfNtsVuymnSzrI=; b=h1wNxk0Vqs8TFqH479ODRUqhmJ3H/aKY4kuJquc3YZnIg9YalG7ajLv6tXrwCRU60JMLlC SeeFHvvKHVV5AC1K+vcXJIXyuFYZjFdaTv4DG6VcTpwcKSd7zck98MRCBHxfoIjGz01Mtb YeZk3LU2ZCHtd9i8rNWS/3FiH65hEII= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708331033; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QSRqAzJKEFAmAg6CRXLjxKsvnNlwLGfNtsVuymnSzrI=; b=wgeRyP+XzqnY7fLiC4ls6BzOyRVB4o5fshfQV3VlQDUI5lwhtUic7zwNR7r1eOcCOEvIRK GvttK9n2S91/ZvDw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id C85EC13AB6 for ; Mon, 19 Feb 2024 08:23:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id WFi7LxgQ02WaBAAAD6G6ig (envelope-from ) for ; Mon, 19 Feb 2024 08:23:52 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH 8/8] [gdb/dap] Fix race between dap exit and gdb exit Date: Mon, 19 Feb 2024 09:23:41 +0100 Message-Id: <20240219082341.21313-8-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240219082341.21313-1-tdevries@suse.de> References: <20240219082341.21313-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=h1wNxk0V; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=wgeRyP+X X-Spamd-Result: default: False [1.69 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FROM_HAS_DN(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; BROKEN_CONTENT_TYPE(1.50)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_NONE(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.de:+]; MX_GOOD(-0.01)[]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[sourceware.org:url,suse.de:dkim]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: 1.69 X-Rspamd-Queue-Id: 41DA1222EE X-Spamd-Bar: + X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: When DAP shuts down due to an EOF event, there's a race between: - gdb's main thread handling a SIGHUP, and - the DAP main thread exiting. Fix this by waiting for DAP's main thread exit during the gdb_exiting event. [ Note that the variable _dap_thread is set in the DAP main thread, but possibly accessed from other threads in ensure_dap_thread: ... assert threading.current_thread() is _dap_thread ... In principle access should be guarded with a lock, but I suppose seeing the old None value also gives the desired outcome of the comparison, so this looks harmless. ] Since this means accessing _dap_thread in gdb's main thread, we have to make sure it's available there as well. Do this by introducing a variable _dap_thread_for_gdb_thread, to avoid needing locking access to _dap_thread. Tested on aarch64-linux. PR dap/31380 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31380 --- gdb/python/lib/gdb/dap/events.py | 8 +++++++- gdb/python/lib/gdb/dap/startup.py | 21 +++++++++++++++++++-- gdb/testsuite/gdb.dap/eof.exp | 10 ++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/gdb/python/lib/gdb/dap/events.py b/gdb/python/lib/gdb/dap/events.py index 41302229ee5..d9e3a6f8d3f 100644 --- a/gdb/python/lib/gdb/dap/events.py +++ b/gdb/python/lib/gdb/dap/events.py @@ -16,7 +16,7 @@ import gdb from .server import send_event -from .startup import exec_and_log, in_gdb_thread, log +from .startup import exec_and_log, in_gdb_thread, log, dap_thread_join from .modules import is_module, make_module @@ -275,6 +275,11 @@ def _on_inferior_call(event): send_event("stopped", obj) +@in_gdb_thread +def _on_gdb_exiting(event): + dap_thread_join() + + gdb.events.stop.connect(_on_stop) gdb.events.exited.connect(_on_exit) gdb.events.new_thread.connect(_new_thread) @@ -283,3 +288,4 @@ gdb.events.cont.connect(_cont) gdb.events.new_objfile.connect(_new_objfile) gdb.events.free_objfile.connect(_objfile_removed) gdb.events.inferior_call.connect(_on_inferior_call) +gdb.events.gdb_exiting.connect(_on_gdb_exiting) diff --git a/gdb/python/lib/gdb/dap/startup.py b/gdb/python/lib/gdb/dap/startup.py index 8dd89df2614..2c34ddac820 100644 --- a/gdb/python/lib/gdb/dap/startup.py +++ b/gdb/python/lib/gdb/dap/startup.py @@ -37,10 +37,15 @@ else: _gdb_thread = threading.current_thread() -# The DAP thread. +# The DAP thread, stored in the DAP thread. _dap_thread = None +# The DAP thread, stored in the GDB main thread. This to avoid using one +# variable and having to use a lock to access it. +_dap_thread_for_gdb_thread = None + + # "Known" exceptions are wrapped in a DAP exception, so that, by # default, only rogue exceptions are logged -- this is then used by # the test suite. @@ -94,7 +99,9 @@ def start_dap(target): _dap_thread = threading.current_thread() target() - start_thread("DAP", really_start_dap) + # Note that we set _dap_thread in both the DAP and the GDB main thread. + global _dap_thread_for_gdb_thread + _dap_thread_for_gdb_thread = start_thread("DAP", really_start_dap) def in_gdb_thread(func): @@ -223,6 +230,16 @@ def exec_and_log(cmd): log_stack() +def dap_thread_join(): + global _dap_thread_for_gdb_thread + if _dap_thread_for_gdb_thread != None: + thread_log("joining dap thread ...") + _dap_thread_for_gdb_thread.join() + thread_log("joining dap thread done") + else: + thread_log("no dap thread to join") + + class Invoker(object): """A simple class that can invoke a gdb command.""" diff --git a/gdb/testsuite/gdb.dap/eof.exp b/gdb/testsuite/gdb.dap/eof.exp index 404a069246f..27661f793b7 100644 --- a/gdb/testsuite/gdb.dap/eof.exp +++ b/gdb/testsuite/gdb.dap/eof.exp @@ -38,3 +38,13 @@ dap_check_log_file # Check that first log message is present. dap_check_log_file_re [string_to_regexp "+++ set python print-stack full"] + +# There should be one "READ:" for the initialize request, and at least one +# "WROTE:" for the initialize response. +dap_check_log_file_re "READ:" +dap_check_log_file_re "WROTE:" + +# Check that all thread termination messages are there. +dap_check_log_file_re "JSON writer: terminating" +dap_check_log_file_re "JSON reader: terminating" +dap_check_log_file_re "DAP: terminating" -- 2.35.3