Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C96BAC4743C for ; Fri, 4 Jun 2021 19:05:25 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2EEAE613FA for ; Fri, 4 Jun 2021 19:05:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2EEAE613FA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A385B82F24; Fri, 4 Jun 2021 21:05:20 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="szZF7f5A"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8A74E82F24; Fri, 4 Jun 2021 21:05:18 +0200 (CEST) Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8C21D82EF0 for ; Fri, 4 Jun 2021 21:05:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=alpernebiyasak@gmail.com Received: by mail-ed1-x530.google.com with SMTP id u24so12287317edy.11 for ; Fri, 04 Jun 2021 12:05:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oCYgQxFQadgL3W79+69zmyniyAzKufm6a96ziRjgp40=; b=szZF7f5A73Z6rLXWNED2yJsaiHmWsb85pix834jBHLliOz50utPgiaEZz4wDTuvNYb G/PYezZx/pdxrn6WZKsVAUIA3XDVOwKQimMznNuj4JzGHUuf9Wm/cHbKjlAfP310owSG rRtArQUlJXxMYr8PTkwyO2uZIdCvOkcyXXpe1x+zTxkGd6VICDx9RnJ1Oy5fL4mPoZCg N8QKmX6C1/8H4ksrmWPgxLhxFrT1RsB2NS+G6wD8q+SIhTHzuF3q+Oku7f8jEs1C2twa 7JozFZgTDMi0DCzvAjJ+ngzoCpbyzBgQxSuOioVJ96OQ1Tcc5StGwZxdnQpgkumQbyfx WQQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oCYgQxFQadgL3W79+69zmyniyAzKufm6a96ziRjgp40=; b=D1k903ARTy58CbL5v0YYRwlsPWS6n37YyP0G6t6pbLO4QjKYU1krQs1JFCg+qnemzP 9OkMQrhSx3QZRdO4nK9DlWlE0VVHiLQwNwhTO/jo34ypkYGmPM0KhqeR2L/wQUUm6p5S KST9ZNaixfswSOWDhkzBQgG4USIHc0phzo9kkjQc+SlLp/UNuMBCZ1HbXOAxx+/87/xw H5IM/AxR/EopQ059UgyGLAAgKqOVTCrnWM1GzCbHOHcM2rpp3e5TReE9sfEGvWQIsrog e2qdnRzaF81zxEjz+xf/yWckbwB5oiVYbTyHkCKvcp9Ur4VjbJ4EzG15vE4tXtl9UGYa vH9Q== X-Gm-Message-State: AOAM531xnGF6P99cYv94pIMmNsp3BMHGIiENGLC2Vv9AY4km5CSX1mn1 WwsvRKalLlTtnzy9Fpu6xXfmwASI8hg= X-Google-Smtp-Source: ABdhPJymG9+jInvbr05NKQoIEpiuaOaNVOVSA0Eh7SSP92eMVrZ0qzi7t4AVclQwzC/aKwhyHYDQsw== X-Received: by 2002:a05:6402:2317:: with SMTP id l23mr6389681eda.265.1622833513082; Fri, 04 Jun 2021 12:05:13 -0700 (PDT) Received: from localhost.localdomain ([178.233.26.119]) by smtp.gmail.com with ESMTPSA id d24sm3112484ejr.51.2021.06.04.12.05.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Jun 2021 12:05:12 -0700 (PDT) From: Alper Nebi Yasak To: u-boot@lists.denx.de Cc: Simon Glass , Heinrich Schuchardt , Stephen Warren , Andy Shevchenko , Tom Rini , Alper Nebi Yasak Subject: [PATCH 2/2] test/py: Wait for guestmount worker to exit after running guestunmount Date: Fri, 4 Jun 2021 22:04:46 +0300 Message-Id: <20210604190447.45342-2-alpernebiyasak@gmail.com> X-Mailer: git-send-email 2.32.0.rc2 In-Reply-To: <20210604190447.45342-1-alpernebiyasak@gmail.com> References: <20210604190447.45342-1-alpernebiyasak@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de X-Virus-Status: Clean Some filesystem tests are failing when their image is prepared with guestmount, but succeeding if loop mounts are used instead. The reason seems to be a race condition the guestmount(1) manual page explains: When guestunmount(1)/fusermount(1) exits, guestmount may still be running and cleaning up the mountpoint. The disk image will not be fully finalized. This means that scripts like the following have a nasty race condition: guestmount -a disk.img -i /mnt # copy things into /mnt guestunmount /mnt # immediately try to use 'disk.img' ** UNSAFE ** The solution is to use the --pid-file option to write the guestmount PID to a file, then after guestunmount spin waiting for this PID to exit. The Python standard library has an os.waitpid() function for waiting a child to terminate, but it cannot wait on non-child processes. Implement a utility function that can do this by polling the process repeatedly for a given duration, optionally killing the process if it won't terminate on its own. Apply the suggested solution with this utility function, which makes the failing tests succeed again. Signed-off-by: Alper Nebi Yasak --- test/py/tests/test_fs/conftest.py | 13 ++++++++++- test/py/u_boot_utils.py | 36 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/test/py/tests/test_fs/conftest.py b/test/py/tests/test_fs/conftest.py index e3c461635f8e..6b1ff05a8143 100644 --- a/test/py/tests/test_fs/conftest.py +++ b/test/py/tests/test_fs/conftest.py @@ -8,6 +8,7 @@ import re from subprocess import call, check_call, check_output, CalledProcessError from fstest_defs import * +import u_boot_utils as util supported_fs_basic = ['fat16', 'fat32', 'ext4'] supported_fs_ext = ['fat16', 'fat32'] @@ -206,7 +207,7 @@ def mount_fs(fs_type, device, mount_point): global fuse_mounted try: - check_call('guestmount -a %s -m /dev/sda %s' + check_call('guestmount --pid-file guestmount.pid -a %s -m /dev/sda %s' % (device, mount_point), shell=True) fuse_mounted = True return @@ -235,6 +236,16 @@ def umount_fs(mount_point): if fuse_mounted: call('sync') call('guestunmount %s' % mount_point, shell=True) + + try: + with open("guestmount.pid", "r") as pidfile: + pid = int(pidfile.read()) + util.waitpid(pid, kill=True) + os.remove("guestmount.pid") + + except FileNotFoundError: + pass + else: call('sudo umount %s' % mount_point, shell=True) diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py index 939d82eec12a..e816c7fbb6a3 100644 --- a/test/py/u_boot_utils.py +++ b/test/py/u_boot_utils.py @@ -8,6 +8,7 @@ import os import os.path import pytest +import signal import sys import time import re @@ -339,3 +340,38 @@ def crc32(u_boot_console, address, count): assert m, 'CRC32 operation failed.' return m.group(1) + +def waitpid(pid, timeout=60, kill=False): + """Wait a process to terminate by its PID + + This is an alternative to a os.waitpid(pid, 0) call that works on + processes that aren't children of the python process. + + Args: + pid: PID of a running process. + timeout: Time in seconds to wait. + kill: Whether to forcibly kill the process after timeout. + + Returns: + True, if the process ended on its own. + False, if the process was killed by this function. + + Raises: + TimeoutError, if the process is still running after timeout. + """ + try: + for _ in range(timeout): + os.kill(pid, 0) + time.sleep(1) + + if kill: + os.kill(pid, signal.SIGKILL) + return False + + except ProcessLookupError: + return True + + raise TimeoutError( + "Process with PID {} did not terminate after {} seconds." + .format(pid, timeout) + ) -- 2.32.0.rc2