Bug 584007 - Return of a command substitution from within another command substitution causes stdin of coprocess to be closed in a ksh script
Summary: Return of a command substitution from within another command substitution cau...
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Fedora
Classification: Fedora
Component: ksh
Version: rawhide
Hardware: All
OS: Linux
low
medium
Target Milestone: ---
Assignee: Michal Hlavinka
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2010-04-20 13:44 UTC by Siddhesh Poyarekar
Modified: 2015-09-14 00:21 UTC (History)
2 users (show)

Fixed In Version: ksh-20100309-5.fc11
Doc Type: Bug Fix
Doc Text:
Clone Of:
: 584704 (view as bug list)
Environment:
Last Closed: 2010-05-05 11:40:19 UTC


Attachments (Terms of Use)
Close coprocess file descriptors only if the coprocess pid is nonzero (493 bytes, patch)
2010-04-20 13:50 UTC, Siddhesh Poyarekar
no flags Details | Diff

Description Siddhesh Poyarekar 2010-04-20 13:44:49 UTC
Description of problem:
Return of a command substitution from within another command substitution causes stdin of coprocess to be closed in a ksh script.

Version-Release number of selected component (if applicable):
ksh-20100309-3.fc14.x86_64

How reproducible:
Always

Steps to Reproduce:
1. Execute the following script in ksh:

#######################################
#!/bin/ksh

set -e

co_process()
{
    while read line
    do
    printf "\n%s(): Read line from 'stdin'\n" "${.sh.fun}" >/dev/tty
    done
    printf "\n%s(): Exiting -- EOF on read of 'stdin'.\n" "${.sh.fun}" >/dev/tty
    # kill the main shell process when the end-of-file is detected on 'stdin'.
    kill $$
}

co_process |&

loop_count=0

while :
do
    (( loop_count++ ))
    printf "%05d: %s\r" "${loop_count}" "$( echo $( /bin/ls ) )"
done 
#######################################

Actual results:

Script ends with the following output:

test_script.ksh: Exiting -- EOF on read of 'stdin'.\n

Expected results:

Script should run in an infinite loop.

Additional info:

Comment 1 Siddhesh Poyarekar 2010-04-20 13:50:50 UTC
Created attachment 407821 [details]
Close coprocess file descriptors only if the coprocess pid is nonzero

The root cause for this is the race condition between the job_reap called within the signal handler and job_wait in the parent process. job_reap sets the lastpid to the pid of the process that exited last, so if job_wait is called after it, it correctly compares a valid pid against the coprocess pid to see if it is the coprocess that has ended.

But in case of a command substitution, the job env (including the coprocess pid) is backed up and reinitialized, hence making the coprocess pid 0. In this case, if job_wait happens to execute before job_reap, it compares the 0 coprocess id with the 0 lastpid (since it has not been set through the signal handler yet) and goes on to close the coprocess assuming that it has quit.

Attached fix ensures that the coprocess pid is actually set before trying to close its handlers.

Comment 2 Michal Hlavinka 2010-04-20 14:38:50 UTC
Hi,

thanks for the report and especially for the patch. I can reproduce this bug and confirm attached patch fixes it. I've emailed all information to upstream and I'm waiting for their response.

Comment 3 Fedora Update System 2010-05-05 11:46:48 UTC
ksh-20100309-4.fc12 has been submitted as an update for Fedora 12.
http://admin.fedoraproject.org/updates/ksh-20100309-4.fc12

Comment 4 Fedora Update System 2010-05-05 11:46:54 UTC
ksh-20100309-4.fc11 has been submitted as an update for Fedora 11.
http://admin.fedoraproject.org/updates/ksh-20100309-4.fc11

Comment 5 Fedora Update System 2010-05-05 11:46:58 UTC
ksh-20100309-4.fc13 has been submitted as an update for Fedora 13.
http://admin.fedoraproject.org/updates/ksh-20100309-4.fc13

Comment 6 Fedora Update System 2010-05-31 18:13:42 UTC
ksh-20100309-5.fc13 has been pushed to the Fedora 13 stable repository.  If problems still persist, please make note of it in this bug report.

Comment 7 Fedora Update System 2010-05-31 18:23:21 UTC
ksh-20100309-5.fc12 has been pushed to the Fedora 12 stable repository.  If problems still persist, please make note of it in this bug report.

Comment 8 Fedora Update System 2010-05-31 18:27:59 UTC
ksh-20100309-5.fc11 has been pushed to the Fedora 11 stable repository.  If problems still persist, please make note of it in this bug report.


Note You need to log in before you can comment on or make changes to this bug.