Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Roger Newsgroups: gnu.bash.bug Subject: Re: Protect Loop Execution with Traps Date: Tue, 28 Jan 2020 16:25:51 -0500 Lines: 61 Approved: bug-bash@gnu.org Message-ID: References: <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> <20200128210721.GU1350@eeg.ccf.org> <20200128212551.GD12574@localhost4.local> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1580246758 11331 209.51.188.17 (28 Jan 2020 21:25:58 GMT) X-Complaints-To: action@cs.stanford.edu To: Greg Wooledge , bug-bash@gnu.org Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=jwuWF1NL+uljBUTvqs0ehV9yWGSMJfEmyGbgrVKA488=; b=Mr3Jji9KeefHj3NwDIGajmkUoXPegwVfRBo1lQaKKNTydPZf+ZmKwvunV68hgTAppB ylM5Swwyi8r0Hs6odLvc+gWuw6YsKugRKMXGWMBnlrd6pzXMYui3nDapnEAinuYRe1t7 8OrA1p1CS6DA6QbjEZzuMx//RfxBmassuDNTfUgDYEaATyD84KvnQsG9V2ph/0Oi65oI WIZakQXsZ5tc78Yl1Dd5cHi2dZbjdBhjmpOvrn3nOIRlGPDMgWlMQzO6rV/zZS/8gemz 8Tvpk0t9eoywK4ZHpOwso9CMGEG2mxRd/u5lLZIPu2uF0pJsWlkvYRFA4yuX5k4557Wz hBPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=jwuWF1NL+uljBUTvqs0ehV9yWGSMJfEmyGbgrVKA488=; b=LlhPQ4GPnSJ3x0+XK4reCO90p2wBarlBoicA4Lq5t+Y3tBUTxGP/WFeurJ3w7KuiA8 MwP3ovHF12/qDlcqnkEkQQOqFjnk7aEWpoopnygkOQ878uOyTTFFvRW12Xgux+B0iIpp mcRceviB+bJUg5Dfhn6hlyKZ9QJrfbiXNNmTdTvt34lDC+PiJi+7qnRV8XbmG9AfH6xq k7jnL+RWr993Q2AekcVHxgvxmvWzOZq3ztK9Y2PGvqD5GDlsbHQiur1poOz43R36+eTn 0iXMey7MD+96QjKYvONJ9rblVSrbfo7FnRwbn0BL1YL1K1A0FoEbDay9yd9IGSwv70e1 nuqQ== X-Gm-Message-State: APjAAAXsgjruWIfxU3r9a5CXjZljkVT/bYxN4KG7RV00qxco6mTmLMhd B1rU7kxJDZGW0a+wO/HIwbOHy/Azg0w= X-Google-Smtp-Source: APXvYqwlc6f8owpgroobWXCqnMqxzeA/clNgzbXBCo4lTfCcGQQgcJkvL1g+1oi6VyTMq15ueg7p0g== X-Received: by 2002:a05:6214:923:: with SMTP id dk3mr23728793qvb.96.1580246752555; Tue, 28 Jan 2020 13:25:52 -0800 (PST) Content-Disposition: inline In-Reply-To: <20200128210721.GU1350@eeg.ccf.org> X-PGP-Key: http://rogerx.sdf.org/about/armor-F515AC1F231FFC4EF0BCDE7606CEA0ECE2887C75-20170104.asc User-Agent: Mutt/1.10.1 (2018-07-13) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::f34 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20200128212551.GD12574@localhost4.local> X-Mailman-Original-References: <20200128020322.GA31704@localhost4.local> <20200128134935.GM1350@eeg.ccf.org> <20200128204932.GB12574@localhost4.local> <20200128210721.GU1350@eeg.ccf.org> Xref: csiph.com gnu.bash.bug:15831 >Here's a simple fix, that involves setting up ONE trap within the >shell script, to override the shell's default SIGINT handling heuristic. > > >#!/bin/bash >trap exit INT >while true; do > ping -c 3 8.8.8.8 >done > > >There. Now, when I hit Ctrl-C, the whole script exits, not just one >instance of ping. > >(Switching from -c 1 to -c 3 made it a *lot* less spammy, and also much >harder to kill using the "press Ctrl-C twice really fast" approach. >The INT trap works around it beautifully for me, though.) > >Whether this same stuff applies to ffmpeg, I have no idea. I also >don't know why you're Ctrl-C'ing out of an ffmpeg loop often enough >that this has become a concern. > >But, the "simple fix" that I used here has the same issue that ping >itself has -- we're catching SIGINT and handling it and exiting, >instead of letting SIGINT kill us. If something runs *us* in a loop, >it'll have the same problem that *we* had when we tried to run ping >in a loop. Yes. That's not desirable then, as sweet and short as it is. >So, in the interest of not causing problems for other programs, here's >the more correct fix: > > >#!/bin/bash >trap 'trap INT; kill -INT $$' INT >while true; do > ping -c 3 8.8.8.8 >done Wow, " trap 'trap INT; kill -INT $$' INT " not easily readable for me. A trap calling kill inside of a trap. I'm thinking, put "trap INT; kill -INT $$" inside of it's own function, named something like exit_recurs_loop, so the first line reads "trap exit_recurs_loop INT" I'll put these additional trap ideas into practice here whenever I'm doing loops and see what happens. This trap stuff has me pulling out my hair, as I likely do not know the details of how programs use signals and how Bash interacts with such activity. Thanks for your time! -- Roger http://rogerx.sdf.org/