From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id BPraF4vwtmrIpQQAWB0awg (envelope-from ) for ; Fri, 25 Sep 2026 18:07:07 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=XA0kSvYj; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=sTbHKrTt; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=rz1z3P3l; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=5EFFf+A/; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 2FFEB1E06B; Fri, 25 Sep 2026 18:07:07 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 1E6351E01F for ; Fri, 25 Sep 2026 18:07:03 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1BF0D4B920BD for ; Fri, 25 Sep 2026 22:07:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BF0D4B920BD Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=XA0kSvYj; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=sTbHKrTt; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=rz1z3P3l; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=5EFFf+A/ Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 66A164B920F2 for ; Fri, 25 Sep 2026 22:06:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66A164B920F2 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 66A164B920F2 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790373976; cv=none; b=ZRfL8540/ANIn1PTYd9cuAZsqvWj9m+enHnbr5QY9MCqpmgjRktbc8rQzj3c8P9Ivvy0Rns982TqzV1VEMtRBFkNHUYc/n/cRBLbt+6vvHmsq7OVqHTmxIuf9s5abUtplCOgZruhvadMKeluIDsIpQDnxL2PQ+rvYPEWjLiS0lg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790373976; c=relaxed/simple; bh=PaDj1VFcK3R6oLYdqVopVxCGSBpcG1CEuXYIHOWY7nM=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=mxu5Ff1wQFIa6tqNGJbdVY5b94rH63IgkuhvoEiL8jgPQg7a6fwljuczt6G6fv1pKFEPHjKG3gZrC4dzkIQrU9wtn44D2u+K9taeLNaIdf7rFumQLNhbF79v/xA4WhQ+hun8wRLngRZUHU8QLmbElkpERMMd8r9OAkWHER/OImk= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=XA0kSvYj; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=sTbHKrTt; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=rz1z3P3l; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=5EFFf+A/ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 66A164B920F2 Received: from imap1.dmz-prg2.suse.org (unknown [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 D47F721E98 for ; Fri, 25 Sep 2026 22:06:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1790373966; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=fms5mzAC3mbuL/PbZtQXZUdovQjbCOhlLS4AiqiY988=; b=XA0kSvYjLZAmFv7xRZYPBgALfOxum+mkv9AG1Ftg0963oNakTlMpV7HcLjGX+rUVPz6soJ EzPup4v7knKQIADepHynmpXkVccj7JtCAomseEPo7ittPr22/s/NLeQEj5Vv6ZYoO+mnMb h/dEfyApHfjJVhHnDGbtA10vZTLp6hw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1790373966; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=fms5mzAC3mbuL/PbZtQXZUdovQjbCOhlLS4AiqiY988=; b=sTbHKrTte2f5408nFypkgeFoRUYr9KVDax8CvEFEzqqTt7p6EKIfjMjVQCl+plQYKtoOvs aWfqDQ82RTLl2pBw== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1790373961; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=fms5mzAC3mbuL/PbZtQXZUdovQjbCOhlLS4AiqiY988=; b=rz1z3P3lizcKx2ZqgOalAPS1K2MwRmtfEQcSKkCwP20NUR6fHAyEpWofHIiH8stTCCIDDA kVlVF1UjwRX5IPzDWSAM8y1Ruo5DayGDZ2P4JdLklopIG8pOY3qhftQ2tMiGrLg9ymcbmI mnk5GfAaaVtwUGOsc/d/zEDMNYUiwOg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1790373961; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=fms5mzAC3mbuL/PbZtQXZUdovQjbCOhlLS4AiqiY988=; b=5EFFf+A/goEmo0gBVONm4hL1jnovHvKL/HsbIAyb1q8LI0XGuUtpPs6EkxHWFcWymghTXF ruiwPyDroSnoLFAA== 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 A186F1321C for ; Fri, 25 Sep 2026 22:06:01 +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 wLDFF0nwtmpcSAAAD6G6ig (envelope-from ) for ; Fri, 25 Sep 2026 22:06:01 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH] [gdb/testsuite] Refactor lock_file_{acquire,release} Date: Sat, 26 Sep 2026 00:06:01 +0200 Message-ID: <20260925220601.2021240-1-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.993]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid,imap1.dmz-prg2.suse.org:helo,gnu.org:url]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; RCVD_TLS_ALL(0.00)[] 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~public-inbox=simark.ca@sourceware.org Refactor lock_file_acquire to limit the loop body to the minimum. Also make error handling more precise by only ignoring EEXIST. Refactor both lock_file_acquire and lock_file_release to use better variable names. Add a test-case checking how the two procs interact. In order to facilitate more complex testing, factor out a lock_file_acquire parameter called on_retry, without changing default behavior. --- gdb/testsuite/gdb.testsuite/lock.exp | 86 ++++++++++++++++++++++++++++ gdb/testsuite/lib/gdb-utils.exp | 59 ++++++++++++------- 2 files changed, 123 insertions(+), 22 deletions(-) create mode 100644 gdb/testsuite/gdb.testsuite/lock.exp diff --git a/gdb/testsuite/gdb.testsuite/lock.exp b/gdb/testsuite/gdb.testsuite/lock.exp new file mode 100644 index 00000000000..86a972e807c --- /dev/null +++ b/gdb/testsuite/gdb.testsuite/lock.exp @@ -0,0 +1,86 @@ +# Copyright 2026 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set lockfile [build_standard_output_file "lock.txt"] +gdb_assert {![file exists $lockfile]} "Initial" + +# Scenario 1: +# - acquire 1 +# - release 1 +with_test_prefix "simple" { + set res [lock_file_acquire $lockfile] + gdb_assert {[file exists $lockfile]} "Acquire lock" + + lock_file_release $res + gdb_assert {![file exists $lockfile]} "Release lock" +} + +# Scenario 2: +# - acquire 1 +# - acquire 2 fails. On retry, release 1. Acquire 2 succeeds. +# - release 2 +with_test_prefix "on retry unlock" { + set res [lock_file_acquire $lockfile] + gdb_assert {[file exists $lockfile]} "Acquire lock" + + set on_retry [list lock_file_release $res] + # Try to acquire the lock. That should fail. + # Then on retry, release the lock. + # The next try to acquire the lock should succeed. + set res2 [lock_file_acquire $lockfile $on_retry] + gdb_assert {[file exists $lockfile]} "Re-acquire lock" + + lock_file_release $res2 + gdb_assert {![file exists $lockfile]} "Release lock" +} + +# Scenario 3: +# - acquire 1 +# - acquire 2 fails, waits, and tries again in a loop. +# - release 1, acquire 2 succeeds. +# - release 2 +with_test_prefix "on retry wait" { + set res [lock_file_acquire $lockfile] + gdb_assert {[file exists $lockfile]} "Acquire lock" + + after 1000 {lock_file_release $res} + + set on_retry { + update + after 100 + } + set res2 [lock_file_acquire $lockfile $on_retry] + gdb_assert {[file exists $lockfile]} "Re-acquire lock" + + lock_file_release $res2 + gdb_assert {![file exists $lockfile]} "Release lock" +} + +# Scenario 4: +# - acquire 1 +# - acquire 2 fails +with_test_prefix "on retry return" { + set res [lock_file_acquire $lockfile] + gdb_assert {[file exists $lockfile]} "Acquire lock" + + set on_retry { + return {} + } + set res2 [lock_file_acquire $lockfile $on_retry] + gdb_assert {$res2 == {}} "Acquire lock again" + + lock_file_release $res + gdb_assert {![file exists $lockfile]} "Release lock" +} diff --git a/gdb/testsuite/lib/gdb-utils.exp b/gdb/testsuite/lib/gdb-utils.exp index 35329f3c46e..aa286b74abc 100644 --- a/gdb/testsuite/lib/gdb-utils.exp +++ b/gdb/testsuite/lib/gdb-utils.exp @@ -178,27 +178,39 @@ proc version_compare { l1 op l2 } { return 1 } -# Acquire lock file LOCKFILE. Tries forever until the lock file is -# successfully created. - -proc lock_file_acquire {lockfile} { +# Acquire lock file LOCKFILE. +# If the lock file doesn't exist, create it and return a file handle/file name +# pair. +# If lock file does exist, with default ON_RETRY try again. A custom ON_RETRY +# may do something different like, for instance, using return. +# Returns {} on failure. + +proc lock_file_acquire {lockfile {on_retry {after 10}}} { verbose -log "acquiring lock file: $::subdir/${::gdb_test_file_name}.exp" - while {true} { + + while {![info exists fh]} { try { - open $lockfile {WRONLY CREAT EXCL} - } on ok {rc} { - set msg "locked by $::subdir/${::gdb_test_file_name}.exp" - verbose -log "lock file: $msg" - # For debugging, put info in the lockfile about who owns - # it. - puts $rc $msg - flush $rc - return [list $rc $lockfile] - } on error {} { - # Ignore and try again. + set fh [open $lockfile {WRONLY CREAT EXCL}] + } trap "POSIX EEXIST" {} { + # Lock in use. Ignore and try again. Propagate all other errors. + uplevel 1 $on_retry } - after 10 } + + # Handle the case that ON_RETRY breaks out of the loop. + if {![info exists fh]} { + return {} + } + + set msg "locked by $::subdir/${::gdb_test_file_name}.exp" + verbose -log "lock file: $msg" + + # For debugging, put info in the lockfile about who owns + # it. + puts $fh $msg + flush $fh + + return [list $fh $lockfile] } # Release a lock file. @@ -206,17 +218,20 @@ proc lock_file_acquire {lockfile} { proc lock_file_release {info} { verbose -log "releasing lock file: $::subdir/${::gdb_test_file_name}.exp" + set fh [lindex $info 0] + set lockfile [lindex $info 1] + try { - fconfigure [lindex $info 0] + fconfigure $fh } on error {} { error "invalid lock" } try { - close [lindex $info 0] - file delete -force [lindex $info 1] - } on error {rc} { - error "Error releasing lockfile: '$rc'" + close $fh + file delete -force $lockfile + } on error {err_msg} { + error "Error releasing lockfile: '$err_msg'" } return "" base-commit: aea8b82af17261c88ab4e4b8e623903c49fc81c0 -- 2.51.0