From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-io1-xd29.google.com with SMTP id f14so8535164iow.5 for ; Wed, 22 Mar 2023 07:34:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1679495665; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Tq4o1m/Vs/oy+Mc0/Qm8uo+kPxYSfYfNJpEoSLuzzgI=; b=dnqTvel0Gto4it04uI9j6gfFMMxZI2QPrkFEFdwaOV4cluK7jSh0T5KCjSFwpa95ZY V9IX4tWIGXFYrcLSutmCPntCP0fB58wIoYfnooOMUifsf5zF4nMzFes8ak5fcOYv35a8 fjE23cF+gK9V9XvoD0zRB54J7H2vHg8SABt8+/YomvH/xjdDaC0rRb6ryNbkFB0hbmIM rqMjo1XJM/QMntMnwkPqf2UflS/2nJrWlPyDcaZ45L9E7yi09mXFcMfE7crsLFMHdG9X JN9mr8KasvD3GkRTAtXRHwhKeAdppw21gqBbdDc6OMM4MMKMHzy4KyaB5GjD79eHdqi5 yf1A== 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) From: Tom Tromey 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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): -- 2.39.1