From patchwork Wed Mar 20 02:54:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ding X-Patchwork-Id: 1913837 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=MDaCdDhk; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=smtp4.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (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 4TztWG29Wcz1yWy for ; Wed, 20 Mar 2024 13:55:38 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 10DFA407E7; Wed, 20 Mar 2024 02:55:36 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id I_U0rFd-KTt5; Wed, 20 Mar 2024 02:55:34 +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 smtp4.osuosl.org A795F407DB Authentication-Results: smtp4.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=MDaCdDhk Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id A795F407DB; Wed, 20 Mar 2024 02:55:34 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id E4289C0077; Wed, 20 Mar 2024 02:55:33 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 570E1C0037 for ; Wed, 20 Mar 2024 02:55:33 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 3F41960B67 for ; Wed, 20 Mar 2024 02:55:33 +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 UknmWE1N3Jsb for ; Wed, 20 Mar 2024 02:55:32 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=2607:f8b0:4864:20::1034; helo=mail-pj1-x1034.google.com; envelope-from=danieldin186@gmail.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp3.osuosl.org 5601C60B61 Authentication-Results: smtp3.osuosl.org; dmarc=pass (p=none dis=none) header.from=gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 5601C60B61 Authentication-Results: smtp3.osuosl.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=MDaCdDhk Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by smtp3.osuosl.org (Postfix) with ESMTPS id 5601C60B61 for ; Wed, 20 Mar 2024 02:55:32 +0000 (UTC) Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-29deb7e2f7aso3903689a91.1 for ; Tue, 19 Mar 2024 19:55:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710903331; x=1711508131; 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=1DcFK+b84J6RK25qX95OuT6wKnoHRFdO6YireBDcTZI=; b=MDaCdDhkTKsjs2eOb1okXu9i/40eLL5SpD5bNNs8KeTE5n9Xn3K3fdVhnsDP24s6Dc u6YIZDjyA4wLgQpd/CX2pclRuvqkyZNESAqTjAKjAgBtvov/N1Z6dDaGGajm2JsS+Ix7 7x3isqK9pxJUAEWhPzJFJ+meKW+s00Uhfka6E2bUUHRGbKAjtOOrwUglZJc7KMg+8FGa OS4fqvaDtSTWpXHNQPrllHFDeNt2gUqKXHSijUbvsjobxeAbriQfKBWwKjsM4c6cBFMu DTfGcqvpw+J6uSD9OrKwBhu9GbZBkvAfr8YhqaDTSr0WCFutiWFm9VhRSHCVNT3gJVvm sEvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710903331; x=1711508131; 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=1DcFK+b84J6RK25qX95OuT6wKnoHRFdO6YireBDcTZI=; b=JfMp5U5TcCFcPUvZcajmFWrn6uTFSvLs4cQzEC7FTuvUheIDQeG4gVbj1hJlqdyH6o z4MJ2D487uMvPS79wWnO48T82Vt4hoPpReBqwUADqi2N+84X+g1S0mPEcU6GboplcnUT lJtrm/42Zmu8rwI3GkL4soeICxSVFFOnZMutQguPPe4T/JkRQTsNhfRIeXvqfyQy/wzf AMAHy+dOhxJkr7nHYGLgBAccFU4RbyrZ5pPEt8PJQK7l85D/Z6QQMr0qv/fHBITZ9+Sn 31NDmzz6u+dGKLpLOUZG+7/i+PdEj+WCufwl22RiJmCrj0lPNlbJP2MuBnmM8f3coPuc wlJQ== X-Gm-Message-State: AOJu0YzdJzXJPYImPftTPiwih79kQRNhziPYRTmzvY5w810PAVpze5nM MlDSGmYj1wTpngW35t6eeSC2SvOsLotIgR/TA7Wh1Vl2KOtkgaN9IMTq4dtM X-Google-Smtp-Source: AGHT+IF+Xx19Cusue3ALD1ygtGZNBk/ahmqya1p/z/AIbXREoyklUmyzepjsSlTW2nRISRI4fbsZFg== X-Received: by 2002:a17:90b:3798:b0:29c:6a5c:b8f5 with SMTP id mz24-20020a17090b379800b0029c6a5cb8f5mr4181318pjb.43.1710903330753; Tue, 19 Mar 2024 19:55:30 -0700 (PDT) Received: from localhost.localdomain ([45.159.50.93]) by smtp.gmail.com with ESMTPSA id ds20-20020a17090b08d400b0029c741ca894sm328005pjb.29.2024.03.19.19.55.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 19:55:30 -0700 (PDT) From: Daniel Ding To: dev@openvswitch.org Date: Wed, 20 Mar 2024 10:54:07 +0800 Message-ID: <20240320025407.33530-1-danieldin186@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Cc: Daniel Ding Subject: [ovs-dev] [PATCH v3] 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 was not registered to the signal handler. When received CTRL+C again, the program was broken, and calling hook could not be executed completely. Signed-off-by: Daniel Ding --- python/ovs/fatal_signal.py | 24 +++++++++++++----------- utilities/ovs-tcpdump.in | 32 +++++++++++--------------------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/python/ovs/fatal_signal.py b/python/ovs/fatal_signal.py index cb2e99e87..16a7e78a0 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..eada803bb 100755 --- a/utilities/ovs-tcpdump.in +++ b/utilities/ovs-tcpdump.in @@ -534,29 +534,19 @@ def main(): ovsdb.close_idl() pipes = _doexec(*([dump_cmd, '-i', mirror_interface] + tcpdargs)) - 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 + while pipes.poll() is None: + data = pipes.stdout.readline().strip(b'\n') + if len(data) == 0: + break + print(data.decode('utf-8')) - if pipes.poll() is None: - pipes.terminate() + try: + sys.stdout.close() + except IOError: + pass - sys.exit(0) + if pipes.poll() is None: + pipes.terminate() if __name__ == '__main__':