From patchwork Mon Feb 19 08:23:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 85951 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB23F384DB7E for ; Mon, 19 Feb 2024 08:24:38 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id 60F30384F00C for ; Mon, 19 Feb 2024 08:23:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 60F30384F00C 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 60F30384F00C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708331037; cv=none; b=baF0FvZimH22YDcRq0kbUm2SUpmhYjiZyIU4g0Lv7fLtIh0LsZkgwQT6DEEamm6bCDdQRO85sMSXcz6p6+rlEapl8aBygE4Yk2Ek874sXqqpqgxEfcdp74BiH63f5PWiNwzb7AS55Rv01BndC2oTW3cAgUGiFG0/lHCk5JUcDxQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708331037; c=relaxed/simple; bh=qvcjXAoC/WnpRkGbvCJF+f9VKr+75DWTshJ8phrkZUc=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=sNCTO5J6yZ/fyTO04CtaPiVEgLLF6zyJUppa7ZI++oHY0GYbNG8Ksw0jZjrjuGR3gyJI3dmdSSk/J8CjNKtmxMWjdt5C4wEd0a393bZAzT3W496jipzLFju94YE5vS7BB9RG9XFttSI6ZEKyx/BMZYcy2tviTivfugfd0k6v1dk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [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-out2.suse.de (Postfix) with ESMTPS id 7B6731FD08 for ; Mon, 19 Feb 2024 08:23:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708331032; 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=DoUTRBiSLTf94iNq//l75McNZZ/y2mAtgnjdh6NHMDA=; b=jR0wqrpQ1lMHzSls6ukOXB6LPDyomgd6r7CVtt3vcd05exLyUWRMvYJJEFivNHqGO/X+br ygP3aA/Gmh5h+evdnKeqg4tR1Sztpo9HpALVdIWPDV4/xy9bvwI/TIPcFlOIZj+gRDA/93 bsxXI7MwaibTaAr5iGYway0/8EVR1QQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708331032; 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=DoUTRBiSLTf94iNq//l75McNZZ/y2mAtgnjdh6NHMDA=; b=3qXvqg+dV4LiTDw3KSFHFn32MBTHLhSOdi0nDKGN/giVDQbe6YySEG0l8nGL09VPuZlMR5 wlfBrRLLHGYeOlBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708331032; 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=DoUTRBiSLTf94iNq//l75McNZZ/y2mAtgnjdh6NHMDA=; b=jR0wqrpQ1lMHzSls6ukOXB6LPDyomgd6r7CVtt3vcd05exLyUWRMvYJJEFivNHqGO/X+br ygP3aA/Gmh5h+evdnKeqg4tR1Sztpo9HpALVdIWPDV4/xy9bvwI/TIPcFlOIZj+gRDA/93 bsxXI7MwaibTaAr5iGYway0/8EVR1QQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708331032; 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=DoUTRBiSLTf94iNq//l75McNZZ/y2mAtgnjdh6NHMDA=; b=3qXvqg+dV4LiTDw3KSFHFn32MBTHLhSOdi0nDKGN/giVDQbe6YySEG0l8nGL09VPuZlMR5 wlfBrRLLHGYeOlBg== 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 63B1613AAA 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 sD8wFxgQ02WaBAAAD6G6ig (envelope-from ) for ; Mon, 19 Feb 2024 08:23:52 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH 4/8] [gdb/dap] Make dap log printing thread-safe Date: Mon, 19 Feb 2024 09:23:37 +0100 Message-Id: <20240219082341.21313-4-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 Authentication-Results: smtp-out2.suse.de; none X-Spamd-Result: default: False [4.32 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; 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]; MID_CONTAINS_FROM(1.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-0.58)[81.41%] X-Spam-Score: 4.32 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org I read that printing from different python threads is thread-unsafe, and I noticed that the dap log printing is used from different threads, but doesn't take care to make the printing thread-safe. Fix this by using a lock to access LoggingParam.log_file. Tested on aarch64-linux. --- gdb/python/lib/gdb/dap/startup.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/gdb/python/lib/gdb/dap/startup.py b/gdb/python/lib/gdb/dap/startup.py index edffc00902b..fc86442db42 100644 --- a/gdb/python/lib/gdb/dap/startup.py +++ b/gdb/python/lib/gdb/dap/startup.py @@ -145,7 +145,9 @@ class LoggingParam(gdb.Parameter): set_doc = "Set the DAP logging status." show_doc = "Show the DAP logging status." - log_file = None + lock = threading.Lock() + with lock: + log_file = None def __init__(self): super().__init__( @@ -154,12 +156,13 @@ class LoggingParam(gdb.Parameter): self.value = None def get_set_string(self): - # Close any existing log file, no matter what. - if self.log_file is not None: - self.log_file.close() - self.log_file = None - if self.value is not None: - self.log_file = open(self.value, "w") + with dap_log.lock: + # Close any existing log file, no matter what. + if self.log_file is not None: + self.log_file.close() + self.log_file = None + if self.value is not None: + self.log_file = open(self.value, "w") return "" @@ -168,9 +171,10 @@ dap_log = LoggingParam() def log(something, level=LogLevel.DEFAULT): """Log SOMETHING to the log file, if logging is enabled.""" - if dap_log.log_file is not None and level <= _log_level.value: - print(something, file=dap_log.log_file) - dap_log.log_file.flush() + with dap_log.lock: + if dap_log.log_file is not None and level <= _log_level.value: + print(something, file=dap_log.log_file) + dap_log.log_file.flush() def thread_log(something, level=LogLevel.DEFAULT): @@ -183,9 +187,10 @@ def thread_log(something, level=LogLevel.DEFAULT): def log_stack(level=LogLevel.DEFAULT): """Log a stack trace to the log file, if logging is enabled.""" - if dap_log.log_file is not None and level <= _log_level.value: - traceback.print_exc(file=dap_log.log_file) - dap_log.log_file.flush() + with dap_log.lock: + if dap_log.log_file is not None and level <= _log_level.value: + traceback.print_exc(file=dap_log.log_file) + dap_log.log_file.flush() @in_gdb_thread