Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Chet Ramey Newsgroups: gnu.bash.bug Subject: Re: bash loses control of jobs inside a command substitution Date: Wed, 20 Nov 2019 16:52:19 -0500 Lines: 32 Approved: bug-bash@gnu.org Message-ID: References: Reply-To: chet.ramey@case.edu NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: usenet.stanford.edu 1574286747 27150 209.51.188.17 (20 Nov 2019 21:52:27 GMT) X-Complaints-To: action@cs.stanford.edu Cc: chet.ramey@case.edu To: Luiz Angelo Daros de Luca , bug-bash@gnu.org Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=case.edu; s=smtp-primary; t=1574286741; bh=YV3z9wnuxVGOk9G2t6B3hUrkDYjh033R3FFItdwOUrM=; h=Reply-To:Cc:Subject:To:References:From:Message-ID:Date: MIME-Version:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=Cc02HgBBlzZmFihqdk2kDuIi+m25B/Yu5plx1qbDUxt/ErB5eW9zRF6OpFF4891bST 1P81daiWE9yA8loqEy9UfXlirddGmb4IyfW3cmx/ArDsbZZQPVRKyBDl14D3iHnsERv /FoSIBnUZhG9MsrDY0kMxRDTy3D56ZR7hA1Coc5XR9PRLAYxhKFWFt2RSXsmaZ8KSpT 6zSvpbzBnafwWkm1hDALUSUror9ePt3rZUNZeEWrrYGSf/7zgfOK7N4KPX+JvGHJg1O JWpnH7v5c59AtnbNFUCEjUdKUVEst+91uvlmKi1z59spx8m2I/sNJANZA54OJR/ai0Z aEkvovlw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=case.edu; s=smtp-primary; t=1574286740; bh=mguDBhgz9IPYxXGGouY/jkkdTdF191WfykaNFbvEEXA=; h=Reply-To:Cc:Subject:To:References:From:Message-ID:Date: MIME-Version:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=dJZ6Xyr963XwoaoSCcwsgfLeEn/xXS1iKDpujIwjN6HwcJMQqBZ8VSi+LZ1xktmEyR IjmY43orxoxru4nLZlGvVvux7wPSbanwjofpkNX246eFcgCTp+FlKW7oAHkNM343vtT lHOgf2QNLxYQ4ijZ1bG7YzuPzqHGqnfh4ZTvk7htL69j7bL8zyNGyQepeegRnrnNn7f itsexlNyTVcPk4D+e0fek0WgBAjb0wWsZtAzvgwVmGUmLn5Ksx98jYvptlfr4a8zedg rIMoLS8SnwOTFhQAXBL589e1bf02W6YFyb1JILME/fcGpC7CxDjEgK84Qp1A9QHv3G8 CUESmCwg== User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 In-Reply-To: Content-Language: en-US X-Junkmail-Status: score=7/90, host=mpv2-2015.case.edu X-Junkmail-PrAS-Raw: score=7/90, refid=2.7.2:2019.11.20.205417:17:7.944, ip=, rules=DKIM_SIGNATURE, __HAS_REPLYTO, __HAS_CC_HDR, __SUBJ_REPLY, __BOUNCE_CHALLENGE_SUBJ, __BOUNCE_NDR_SUBJ_EXEMPT, __TO_MALFORMED_2, __TO_NAME, __TO_NAME_DIFF_FROM_ACC, __HAS_REFERENCES, __REFERENCES, __HAS_FROM, FROM_EDU_TLD, __HAS_MSGID, __SANE_MSGID, DATE_TZ_NA, __USER_AGENT, __MOZILLA_USER_AGENT, __MIME_VERSION, __IN_REP_TO, __CT, __CT_TEXT_PLAIN, __CTE, __REPLYTO_SAMEAS_FROM_ADDY, __REPLYTO_SAMEAS_FROM_ACC, __FROM_DOMAIN_IN_ANY_CC1, __FROM_DOMAIN_IN_ANY_CC2, __REPLYTO_SAMEAS_FROM_DOMAIN, __DKIM_ALIGNS_1, __DKIM_ALIGNS_2, __ANY_URI, __URI_MAILTO, __URI_WITH_PATH, __URI_NO_WWW, __CP_NAME_BODY, __CP_URI_IN_BODY, __SUBJ_ALPHA_NEGATE, __URI_IN_BODY, __URI_NOT_IMG, __FORWARDED_MSG, __BODY_NO_MAILTO, __NO_HTML_TAG_RAW, BODY_SIZE_1500_1599, BODYTEXTP_SIZE_3000_LESS, __MIME_TEXT_P1, __MIME_TEXT_ONLY, __URI_NS, HTML_00_01, HTML_00_10, DKIM_ALIGNS, [TRUNCATED], so=2010-03-03 19:42:08, dmn=2016-08-03-0138 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 129.22.103.227 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: X-Mailman-Original-References: Xref: csiph.com gnu.bash.bug:15620 On 11/19/19 2:51 PM, Luiz Angelo Daros de Luca wrote: > Hello, > > Normally 'wait -n' will return the exit code of background process when > they terminate before wait is runned. However, when bg processes and 'wait > -n' runs inside a command substitution, bash loses control of bg process as > soon as they exit. This looks a lot like a race condition. If you run the sleep with a non-zero argument (or, really, even if you don't, but the non-zero sleep increases the odds), you have two chances -- the sleep and the command substitution in the second loop -- for child processes to exit and be reaped. Since they're asynchronous children, bash will save their statuses, but they won't be available for a subsequent `wait -n' call. They're not lost: you can fetch those statuses using wait with a pid argument. The difference is when jobs are moved from the `active jobs' list, where they are available to `wait -n', into the `saved background processes' list, where they are not, and that does differ based on whether the shell thinks it will eventually notify the user about the job's exit status. When the shell is started to run a command substitution, it knows it will not have to notify the user about any jobs, so it's more aggressive about moving jobs out of the jobs list. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/