From patchwork Fri Feb 23 03:37:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ding X-Patchwork-Id: 1903070 X-Patchwork-Delegate: i.maximets@samsung.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=eE5zUQYL; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TgwhJ6nW7z23hc for ; Fri, 23 Feb 2024 14:38:07 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 87E0E60B32; Fri, 23 Feb 2024 03:38:04 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZqZapOwmUFy0; Fri, 23 Feb 2024 03:38:02 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=140.211.9.56; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 094CF60AA1 Authentication-Results: smtp3.osuosl.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=eE5zUQYL Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp3.osuosl.org (Postfix) with ESMTPS id 094CF60AA1; Fri, 23 Feb 2024 03:38:02 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id D6228C0077; Fri, 23 Feb 2024 03:38:01 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 2D7FEC0037 for ; Fri, 23 Feb 2024 03:38:00 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 15F4F40150 for ; Fri, 23 Feb 2024 03:38:00 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3NGlyXu88zxR for ; Fri, 23 Feb 2024 03:37:59 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=2607:f8b0:4864:20::32a; helo=mail-ot1-x32a.google.com; envelope-from=danieldin186@gmail.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp2.osuosl.org C278E400BA Authentication-Results: smtp2.osuosl.org; dmarc=pass (p=none dis=none) header.from=gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org C278E400BA Authentication-Results: smtp2.osuosl.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=eE5zUQYL Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) by smtp2.osuosl.org (Postfix) with ESMTPS id C278E400BA for ; Fri, 23 Feb 2024 03:37:58 +0000 (UTC) Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-6ddf26eba3cso242057a34.0 for ; Thu, 22 Feb 2024 19:37:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708659477; x=1709264277; darn=openvswitch.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=jgrJiFDqDyyxhfRzlSMzGq0sDRtj+eDqRsR2XdWUQQQ=; b=eE5zUQYL1jRfCCG/0eOo9vdaU2qdWa4C/gPpkbNKcYO1Qj5ref3+eDH8nIkxl5ay/C oIsHfXnOx4qqiAut0ASxSxAIKgXwR8SuSonVBFqdYFv8K18PysD+Um/jpgt5LFjWmQzo Ie4rlZmGOe4XGsB5+6JporcImkmnPnJ3meZO0a6n7+4QXaFeW0gnnM6ZmyNeaxDZdTsp wxp7zkTWooVQHdAC++nzfrNM8GJMfGMjsqS9y18Wt4uQ3M85Scta/lwpa0BHt9WJnbnq DimuRsetgTdC8InPTJ2XTu75Ifhk50KiP2mJS2Tc3I2dI1gP5Gl90i3JMpb/EYIRy+Aq YIkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708659477; x=1709264277; 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=jgrJiFDqDyyxhfRzlSMzGq0sDRtj+eDqRsR2XdWUQQQ=; b=rCeX2yl5ojFiwYgvtHe92xu/C3cdA72U6pCX6HRkY8WpIEjtb1lvH8+wYsvo5nREqP +Sn+Sfv5p+remGfAqWPTC9AGIxeC66sc5P75wFmwM7/ZjfzVRPCKwzdlDHRBeZYAJR+2 VU3lqnSFoqG5xqFOQqjeClpzKRPwC/wUi64mrU90pQWMe8g12pyyliPflhuxItJ2XHHg swecNOlLcx+rvdDZjKHZqM52W/TA4B9oqbfsR75c8iRfFv6HS4dSUkpneMXuVvTyewmQ rRyhDYy30YsiOoJJqF7ZXAAwOtq9yNQMQmVyaZF7t8mEXiu8uZDinijnSsSRrB/BZfVy rZSA== X-Gm-Message-State: AOJu0YyAtMXF/cwlAXqexs2fhjjPVS0TXi/Mczz9QjPjzhx6FvF9eIeU wCWi8R9Nys3npk94UV50nh06b0I/VtCSB85KGSsitCNvCT5h1WseN227PgT7 X-Google-Smtp-Source: AGHT+IF9tHCFQ5nCxt4J3lr4GsK/X71VKippu2lyBbXNMBSUzzFQoNE5lrP3aNwzRxZ9+PATlR/B4A== X-Received: by 2002:a05:6358:9692:b0:176:d5b7:344a with SMTP id o18-20020a056358969200b00176d5b7344amr901143rwa.29.1708659477078; Thu, 22 Feb 2024 19:37:57 -0800 (PST) Received: from localhost.localdomain ([45.159.50.93]) by smtp.gmail.com with ESMTPSA id gx3-20020a17090b124300b0029932d64c2bsm282955pjb.17.2024.02.22.19.37.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 19:37:56 -0800 (PST) From: Daniel Ding To: dev@openvswitch.org Date: Fri, 23 Feb 2024 11:37:27 +0800 Message-ID: <20240223033727.71989-1-danieldin186@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Cc: Daniel Ding Subject: [ovs-dev] [PATCH v2] ovs-tcpdump: Fix cleanup mirror failed with twice fatal signals. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" After running ovs-tcpdump and inputs multiple CTRL+C, the program will raise the following exception. Error in atexit._run_exitfuncs: Traceback (most recent call last): File "/usr/bin/ovs-tcpdump", line 421, in cleanup_mirror ovsdb = OVSDB(db_sock) File "/usr/bin/ovs-tcpdump", line 168, in __init__ OVSDB.wait_for_db_change(self._idl_conn) # Initial Sync with DB File "/usr/bin/ovs-tcpdump", line 155, in wait_for_db_change while idl.change_seqno == seq and not idl.run(): The default handler of SIGINT is default_int_handler, so it not register the signal handler successfully. When received CTRL+C again, the program be break, and calling hook can't execute completely. Signed-off-by: Daniel Ding Signed-off-by: Daniel Ding --- python/ovs/fatal_signal.py | 24 +++++++++++++----------- utilities/ovs-tcpdump.in | 38 +++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/python/ovs/fatal_signal.py b/python/ovs/fatal_signal.py index cb2e99e87..077f50dd5 100644 --- a/python/ovs/fatal_signal.py +++ b/python/ovs/fatal_signal.py @@ -16,6 +16,7 @@ import atexit import os import signal import sys +import threading import ovs.vlog @@ -112,29 +113,29 @@ def _unlink(file_): def _signal_handler(signr, _): _call_hooks(signr) - # Re-raise the signal with the default handling so that the program - # termination status reflects that we were killed by this signal. - signal.signal(signr, signal.SIG_DFL) - os.kill(os.getpid(), signr) - def _atexit_handler(): _call_hooks(0) -recurse = False +mutex = threading.Lock() def _call_hooks(signr): - global recurse - if recurse: + global mutex + if not mutex.acquire(blocking=False): return - recurse = True for hook, cancel, run_at_exit in _hooks: if signr != 0 or run_at_exit: hook() + if signr != 0: + # Re-raise the signal with the default handling so that the program + # termination status reflects that we were killed by this signal. + signal.signal(signr, signal.SIG_DFL) + os.kill(os.getpid(), signr) + _inited = False @@ -150,7 +151,9 @@ def _init(): signal.SIGALRM] for signr in signals: - if signal.getsignal(signr) == signal.SIG_DFL: + handler = signal.getsignal(signr) + if (handler == signal.SIG_DFL or + handler == signal.default_int_handler): signal.signal(signr, _signal_handler) atexit.register(_atexit_handler) @@ -165,7 +168,6 @@ def signal_alarm(timeout): if sys.platform == "win32": import time - import threading class Alarm (threading.Thread): def __init__(self, timeout): diff --git a/utilities/ovs-tcpdump.in b/utilities/ovs-tcpdump.in index 4cbd9a5d3..0731b4ac8 100755 --- a/utilities/ovs-tcpdump.in +++ b/utilities/ovs-tcpdump.in @@ -534,29 +534,25 @@ def main(): ovsdb.close_idl() pipes = _doexec(*([dump_cmd, '-i', mirror_interface] + tcpdargs)) + while pipes.poll() is None: + data = pipes.stdout.readline().strip(b'\n') + if len(data) == 0: + break + print(data.decode('utf-8')) + + # If there is a pipe behind ovs-tcpdump (such as ovs-tcpdump + # -i eth0 | grep "192.168.1.1"), the pipe is no longer available + # after received Ctrl+C. + # If we write data to an unavailable pipe, a pipe error will be + # reported, so we turn off stdout to avoid subsequent flushing + # of data into the pipe. try: - while pipes.poll() is None: - data = pipes.stdout.readline().strip(b'\n') - if len(data) == 0: - raise KeyboardInterrupt - print(data.decode('utf-8')) - raise KeyboardInterrupt - except KeyboardInterrupt: - # If there is a pipe behind ovs-tcpdump (such as ovs-tcpdump - # -i eth0 | grep "192.168.1.1"), the pipe is no longer available - # after received Ctrl+C. - # If we write data to an unavailable pipe, a pipe error will be - # reported, so we turn off stdout to avoid subsequent flushing - # of data into the pipe. - try: - sys.stdout.close() - except IOError: - pass - - if pipes.poll() is None: - pipes.terminate() + sys.stdout.close() + except IOError: + pass - sys.exit(0) + if pipes.poll() is None: + pipes.terminate() if __name__ == '__main__':