From patchwork Wed Mar 22 14:34:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 66744 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 4D3AD38582AB for ; Wed, 22 Mar 2023 14:35:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D3AD38582AB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1679495712; bh=Tq4o1m/Vs/oy+Mc0/Qm8uo+kPxYSfYfNJpEoSLuzzgI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=mlhh1n3oxnu9zkGRh9dgXgK/z8d776OiAL5+MF0tW+HNKv8kZkCgQIeldHC9VbB8T WTm1OlhY5IATLKh/w4P26ZIg9Maw1gPrrBeGuFEdkvG1Y054MA0R0Rr4o1PJvKbGqQ Q7Zx+DIrGg7Md6yrY0q5XK5Vn3Kt/jiwGPeaTG4M= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by sourceware.org (Postfix) with ESMTPS id 238CC385B524 for ; Wed, 22 Mar 2023 14:34:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 238CC385B524 Received: by mail-io1-xd29.google.com with SMTP id f14so8535164iow.5 for ; Wed, 22 Mar 2023 07:34:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679495665; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Tq4o1m/Vs/oy+Mc0/Qm8uo+kPxYSfYfNJpEoSLuzzgI=; b=DVuz0hWBuq/CCWvrO/S9cC6Iti+EFWCqsTXG4kpCpJHX1ulepOOGwu4QnUaRSE0Iwj Khvwq1SKJ/1v71dqQn/R+fqFXVhOl9mDBjBFBN8ruGkHqFZQdrMzWn/lCapNJcK2+zIX oqwB9K4ygQVylgkYKQNo9LK/IK8fZRSEo/cmYzperyP6jiQSkilDrOAYUgczSbzAgTR1 9K/pOuYH8u7iuZ+tCgOTEwoUXGMyOYPestbOPN8H6L5kHBH3rAOe/A2k1QxY93DrWzi2 WN9ijvUJw3Xgvt2Gbp47S8L8sxCBS4MyeMy+OxoSoWKsJ0FVkAzpPCJwMsAHHVCOLfiq wzGA== X-Gm-Message-State: AO0yUKUB2jQ7ER+goMWkIiq91DUeE/gOWRcVwbB29lhuMuB7Xz2jrGhv 5Nww4ywO1UhIvqMbjgVuqodVUDjdR4pkUVogBkWoqg== X-Google-Smtp-Source: AK7set/e4xcgJOtAoyWTKC1KO0KFdKpZ6ZgORNOa2IzKiggnh9OGnlthMtGjtRsRVAsAk00gnSZbVw== X-Received: by 2002:a6b:7b08:0:b0:74c:d67a:6fa0 with SMTP id l8-20020a6b7b08000000b0074cd67a6fa0mr5007770iop.7.1679495665380; Wed, 22 Mar 2023 07:34:25 -0700 (PDT) Received: from localhost.localdomain (71-211-185-113.hlrn.qwest.net. [71.211.185.113]) by smtp.gmail.com with ESMTPSA id bs23-20020a056638451700b004062eae53b6sm5028245jab.160.2023.03.22.07.34.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Mar 2023 07:34:25 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Fix race in DAP startup Date: Wed, 22 Mar 2023 08:34:18 -0600 Message-Id: <20230322143418.1914025-1-tromey@adacore.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Internal AdaCore DAP testing on Windows has had occasional failures that show: assert threading.current_thread() is _dap_thread I think this is a race in DAP startup: the _dap_thread global is only set on return from start_thread, but it seems possible that the thread itself could already run and encounter a @in_dap_thread decorator. This patch fixes the problem by setting the global before running any of the code in the new thread. This also lets us remove a FIXME. --- gdb/python/lib/gdb/dap/server.py | 4 +--- gdb/python/lib/gdb/dap/startup.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py index 92b4eee1c5e..ff88282049f 100644 --- a/gdb/python/lib/gdb/dap/server.py +++ b/gdb/python/lib/gdb/dap/server.py @@ -100,9 +100,7 @@ class Server: log("WROTE: <<<" + json.dumps(obj) + ">>>") self.write_queue.put(obj) - # This must be run in the DAP thread, but we can't use - # @in_dap_thread here because the global isn't set until after - # this starts running. FIXME. + @in_dap_thread def main_loop(self): """The main loop of the DAP server.""" # Before looping, start the thread that writes JSON to the diff --git a/gdb/python/lib/gdb/dap/startup.py b/gdb/python/lib/gdb/dap/startup.py index 22246a937ce..aa834cdb14c 100644 --- a/gdb/python/lib/gdb/dap/startup.py +++ b/gdb/python/lib/gdb/dap/startup.py @@ -59,14 +59,20 @@ def start_thread(name, target, args=()): with blocked_signals(): result = threading.Thread(target=target, args=args, daemon=True) result.start() - return result def start_dap(target): """Start the DAP thread and invoke TARGET there.""" - global _dap_thread exec_and_log("set breakpoint pending on") - _dap_thread = start_thread("DAP", target) + + # Functions in this thread contain assertions that check for this + # global, so we must set it before letting these functions run. + def really_start_dap(): + global _dap_thread + _dap_thread = threading.current_thread() + target() + + start_thread("DAP", really_start_dap) def in_gdb_thread(func):