Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Peng Yu Newsgroups: gnu.bash.bug Subject: Re: Why does ctrl-c behave differently for builtin sleep in a subshell? Date: Tue, 25 Dec 2018 10:28:22 -0600 Lines: 159 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: usenet.stanford.edu 1545755321 27390 208.118.235.17 (25 Dec 2018 16:28:41 GMT) X-Complaints-To: action@cs.stanford.edu To: help-bash , bug-bash Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=4GK+eX8eNSNPdOjQzu9/gpQduVDYpNF2YTXc0UU4sR0=; b=OT1pBOOzq3zhLvZYAJx58w5FVPCvxHFwUk8CqCYJjBXzr4aiQnui6Fyuonh7HjzoaI DkFEp5In2RbudGPQj3ql4X50UMIJYUX6nAWSIW6IhJBXCS2oGfv1IBDpxnA8hWPUFnfU unm7DSwITU2LFgJE+X42l7NHEoIL5utr7qNgmblstaHQsI7yO5mkavdbRYOePVj7QFFf LpLqm6Rvcs7n3p7DwIzFvgz8/+PvQGtGRQrU0aoBrH3SCcSmXSle+cDq24FOmRnouSnK 3IoZ69xNXZJb6OOZjdePQeflz4RDH1eyKo/dwOdrP3b2XAxebwnt34YEQHu1Zv4TPVoQ +K9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=4GK+eX8eNSNPdOjQzu9/gpQduVDYpNF2YTXc0UU4sR0=; b=aMIaCgttPaNzk+lGqodZgWKRmpWcrxAKQl4JqUCbpuVIq1vhlWrl0ZTHnG3f96JY5Q OaFdrKkcNos0ndxAWON9zaS0xST8bcu/emFGgHmShX+To1pMgOLEa8kaLL1qKhgr3oCV 2V9dwN1uumG43zzbNf3M1HwWp+1U7rqU/RvSebg8ZkyZjih28/fbQ3iOhQO6yeWk/OoI TS7voXJ4EUZlYoWX3XqkF8+sZ6JEs0pI3Hm7VF5smHFb/pVhOGruKYeeVkSJ5+epqxrj JdsoCFOOohqgfCqk86iDmqL5bqME2H3esEwfktV+LaFUh6pJ1G6Xh2ed5JcufZ3g6n2d Mlkg== X-Gm-Message-State: AA+aEWYXRjiG6Jg3Z4mzOjMFidJ34qr+J4/5CN37b0nAX63f/fNUqrp6 7MdFo40+7IUXp2w2+yKtNB6RJ3gVK6FP6ntanBBYE5mW X-Google-Smtp-Source: AFSGD/XhjUG9d1xnDAu8jocXh4vAI0dEbmCFcjIUm+bNiEdM5c0wKyTJKwyWiTXUMtC7GrdPK4bzR7QccDWku4Jyjsk= X-Received: by 2002:a24:648f:: with SMTP id t137mr10268063itc.176.1545755315270; Tue, 25 Dec 2018 08:28:35 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::129 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:14949 If I add a command after the builtin sleep, then the EXIT trap will be triggered upon typing ctrl-C. If the last command is removed, then the EXIT trap will not be triggered upon typing ctrl-C. Is this a bug in the built-in sleep? I am trying to inspect the C code of sleep builtin, but it is very simple. I am not sure where the problem comes from. Could anybody take a look? Thanks. $ cat main_INT.sh #!/usr/bin/env bash # vim: set noexpandtab tabstop=2: source ../../../trapexit.sh ( trapexit -q <<-'EOF' echo Hello World! EOF source enable.sh enable sleep sleep 10 echo "$?" ) $ ./main_INT.sh ^CHello World! $ cat main_INT.sh #!/usr/bin/env bash # vim: set noexpandtab tabstop=2: source ../../../trapexit.sh ( trapexit -q <<-'EOF' echo Hello World! EOF source enable.sh enable sleep sleep 10 #echo "$?" ) $ ./main_INT.sh ^C $ source enable.sh $ enable sleep $ help sleep sleep: sleep seconds[.fraction] Suspend execution for specified period. sleep suspends execution for a minimum of SECONDS[.FRACTION] seconds. On Mon, Dec 17, 2018 at 1:57 PM Peng Yu wrote: > > Note that when SIGINT is set, whether an external or built-in sleep is > used will not result in any difference. > > If I use some other method (say a for loop) instead of built-in sleep > to introduce a delay, the EXIT can be reached upon ctrl-c. So this > sounds like that the builtin sleep somehow causes the problem. Is it > the case? > > $ cat main_builtin_INT.sh > #!/usr/bin/env bash > trap 'echo at EXIT' EXIT > trap 'echo at SIGINT' SIGINT > enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep > sleep 10 > $ cat \(/main_builtin_INT.sh > #!/usr/bin/env bash > ( > trap 'echo at EXIT' EXIT > enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep > sleep 10 > ) > $ ./main_builtin_INT.sh > ^Cat SIGINT > at EXIT > $ '('/main_builtin_INT.sh > ^Cat SIGINT > at EXIT > > $ cat ./main_builtin_for.sh > #!/usr/bin/env bash > ( > trap 'echo at EXIT' EXIT > for ((i=0;i<100000;++i)); do > : > done > ) > $ ./main_builtin_for.sh > ^Cat EXIT > > On Mon, Dec 17, 2018 at 1:41 PM Peng Yu wrote: > > > > Hi, > > > > See the follow code, '(/main_builtin.sh' behaves differently from the > > other three scripts upon receiving ctrl-c. This is counter-intuitive. > > I'd expect whether a built-in command called the result should be the > > same. > > > > How to understand why there is such a difference? Is there an easy > > workaround to make the result the same as the other three? > > > > $ ./main.sh > > ^Cat EXIT > > > > $ ./main_builtin.sh > > ^Cat EXIT > > > > $ \(/main.sh > > ^Cat EXIT > > > > $ \(/main_builtin.sh > > ^C > > > > $ cat main.sh > > #!/usr/bin/env bash > > trap 'echo at EXIT' EXIT > > sleep 10 > > > > $ cat main_builtin.sh > > #!/usr/bin/env bash > > trap 'echo at EXIT' EXIT > > enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep > > sleep 10 > > > > $ cat '(/main.sh' > > #!/usr/bin/env bash > > ( > > trap 'echo at EXIT' EXIT > > sleep 10 > > ) > > > > $ cat '(/main_builtin.sh' > > #!/usr/bin/env bash > > ( > > trap 'echo at EXIT' EXIT > > enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep > > sleep 10 > > ) > > > > -- > > Regards, > > Peng > > > > -- > Regards, > Peng -- Regards, Peng