From patchwork Tue Mar 24 17:21:09 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 132242 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 7B1E54BB3BEF for ; Tue, 24 Mar 2026 17:21:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B1E54BB3BEF Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=LjgN9QK1 X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id 557CB4BB5890 for ; Tue, 24 Mar 2026 17:21:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 557CB4BB5890 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 557CB4BB5890 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::2e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774372874; cv=none; b=Xu52p17+ZDSq2an3wqLdgXiBi08+JsLjo7/SezAiIUxgnm4VciZWt8SMdVZcxnXMtP4BWi4PGwjyO0/tjdIUfG+NESeeGyFDtozXnJl3tTe5AmwIll/BoCUsiBGJ+PveDQ6NJciKR5fTUEecuA6jYBMy+1oCN3/uKtY5dYcKdR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774372874; c=relaxed/simple; bh=YGP3mUQpg9vZZlJVbikPgs0ioWUYR4pQ4z55uJ3kO7U=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ksG9PhzIUhq5foVUf2WaO9G9jmgs9dR8e62rRie1zKIVwoRjdtr57RQU2IUhfs2yWPeqqj0MP9qQhA4lkMvh9BdyCVsTt+k2Ixiu2CrDHmB2HAA4tSJHtGVwwH6OKuMg9tvlH5nQ0pliAP+Xa0zWBxPkeQO7Pc2tJ+j73sFIC2k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 557CB4BB5890 Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-41c096fab64so83032fac.1 for ; Tue, 24 Mar 2026 10:21:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1774372873; x=1774977673; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7oMJliRO0g+BE0FmPGaljryDwrlkXE+LaIApy3IZY58=; b=LjgN9QK1GTgsx+xmnVcFdCQEtiPIYcG80yBwcB6Vc12JL1ykuIaBIMU47Tj+PXuKLS JPWHRGUeOIoMpo+kr83MNg2BQ6nHyXkvgQAcgwgLLpZiU0yAlyBsltELAjuIk8jKo1XU w92mYzWa3rGCD0W76gXQ/DX/IxuaCi0PjbtNcBlfq55Zx+xhXz06zO0VxZMWjlplyWqU u3380dR4cF3XlmocgZzR/QlmVBVIUPqcDIJaFuVnSzma85k6qjBK+bVci5omT69jg9nr pq58AcPfqVNatdrnapDkkhMEvQpJFl7Ly806p1rzc+ZPqBIegkwSeKb6Zn1QlK9MHwWy VQ+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774372873; x=1774977673; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=7oMJliRO0g+BE0FmPGaljryDwrlkXE+LaIApy3IZY58=; b=eV6rP8owtigtktd7/KvpAEp4fyE44kE6Q5aO8KlkruA4XycYwbIQufcjjLcXmhOVyB U98uJJryfiNPIgKFG+EnYc8lc8Iu2j5NSRD/b/Pf9UF6v/tVoR9E/F3jg0y5p0ykqT2x 35GK7lx8Yvdemn1pOiMI0uQgQuIfyH35JHpKUEoYNafLSE/dMBH0il9+SNMqGvr6OFmg F42oAn4UIMA1LElt2qC/933SrQqUgHAf/Az+mI4Eha77wYKzSYZQu+lanQdycDGwadZu +euWAPo0uUQx7h3s4PN26MnaThnrd66ebE5Olx3a6SkDoboqZWjNSBlkJyQMSpZNGSfj aShg== X-Gm-Message-State: AOJu0Yx//Rw1SfuTkT9uspAFAkCfkl0E3XOtGYBhxdk8EgaRBynSPuCD DKINQLseaxvAE2Az64Cf5FmXaGwcvgQZNlbbjMuE3xviAi+BxcnzV1jRx3B5PBrz67RZPNV7DQ+ 3p3s= X-Gm-Gg: ATEYQzzAodUcYoDn6JCiuDo4qoeAIkTWSBvhaETygqpZnAJJkcNomI8MG8nTO49vuPp rtBUDzuG+3XOkleK+N/i4Auh8E09DB9wyzVWjV2FrdW8zGwTejuzE95SZwzv7DQQ1WthoNEcU5T U6xC60JyoRs/Co2DQ3VJWUI/uMSKCQaSjU8VhC1+40nLnhDE/zX/dBC+nx892cSkwNqesJR6cEp +mjFfGSZw9UgJJPnUcfVsfVUGY471c+C4Z2bkP0b6nXQz/weUM+UQzzximxbXqJz0VjIj9W4kai pN2oZEfmtRy1Tl0k3jhskpK4JCYvEy0RZX4BlRmPXtMDNW1pboIn/DVISGGu2vy5spcuUFk0xCL Fj6HBApf2VFG9mCABXTH7UD6tGRHybdup3Oai/P46XNutjB+3FUIk85M/22NA33PKw2Newo6mip mNgUcQFB079DIjP+KgImBv2iCRyI8bkRYwhPNBts0kQvk= X-Received: by 2002:a05:6870:810f:b0:41c:65c0:cae8 with SMTP id 586e51a60fabf-41c877865b8mr2553550fac.18.1774372873469; Tue, 24 Mar 2026 10:21:13 -0700 (PDT) Received: from bapiya (75-166-225-82.hlrn.qwest.net. [75.166.225.82]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-41c14d63e29sm13890852fac.12.2026.03.24.10.21.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Mar 2026 10:21:13 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Hold the GIL while clearing gdbpy_reggroup_object_map Date: Tue, 24 Mar 2026 11:21:09 -0600 Message-ID: <20260324172109.570014-1-tromey@adacore.com> X-Mailer: git-send-email 2.53.0 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_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 Tom de Vries pointed out that a certain Python test left a core file. Looking into this, I think the problem is that the GIL is not held when gdbpy_reggroup_object_map is destroyed. This patch changes the code to explicitly clear the global while holding the GIL. This fixed the crash for me. This also adds a comment to gdbpy_initialize_file to explicitly mention that the GIL is held while the finalizers are run. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34013 Reviewed-By: Tom de Vries --- gdb/python/py-registers.c | 23 ++++++++++++++++------- gdb/python/python-internal.h | 4 +++- 2 files changed, 19 insertions(+), 8 deletions(-) base-commit: 7e0417f49792ea5865faa47735be638989fe5229 diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c index 44bc53e6a9d..c6e0748e935 100644 --- a/gdb/python/py-registers.c +++ b/gdb/python/py-registers.c @@ -80,6 +80,13 @@ struct reggroup_object : public PyObject extern PyTypeObject reggroup_object_type; +/* Map from GDB's internal reggroup objects to the Python + representation. GDB's reggroups are global, and are never deleted, + so using a map like this is safe. Note it is explicitly cleaned at + Python shutdown, see gdbpy_finalize_registers. */ +static gdb::unordered_map> + gdbpy_reggroup_object_map; + /* Return a gdb.RegisterGroup object wrapping REGGROUP. The register group objects are cached, and the same Python object will always be returned for the same REGGROUP pointer. */ @@ -87,12 +94,6 @@ extern PyTypeObject reggroup_object_type; static gdbpy_ref<> gdbpy_get_reggroup (const reggroup *reggroup) { - /* Map from GDB's internal reggroup objects to the Python representation. - GDB's reggroups are global, and are never deleted, so using a map like - this is safe. */ - static gdb::unordered_map> - gdbpy_reggroup_object_map; - /* If there is not already a suitable Python object in the map then create a new one, and add it to the map. */ if (gdbpy_reggroup_object_map[reggroup] == nullptr) @@ -433,7 +434,15 @@ gdbpy_initialize_registers () return 0; } -GDBPY_INITIALIZE_FILE (gdbpy_initialize_registers); +static void +gdbpy_finalize_registers () +{ + /* Have to hold the GIL while clearing the global map; this is + guaranteed for finalizers. */ + gdbpy_reggroup_object_map.clear (); +} + +GDBPY_INITIALIZE_FILE (gdbpy_initialize_registers, gdbpy_finalize_registers); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index bdc960ed208..b24b9314994 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -616,7 +616,9 @@ class gdbpy_initialize_file There is no error return in this case. This function is only called when GDB is already shutting down. The function should make a best - effort to clean up, and then return. */ + effort to clean up, and then return. + + The GIL will be held while calling the finalizers. */ using gdbpy_finalize_file_ftype = void (*) (void);