Bug 1459185 - Writing to closed pipe from "Process Substitution" exits the shell
Writing to closed pipe from "Process Substitution" exits the shell
Status: CLOSED WONTFIX
Product: Fedora
Classification: Fedora
Component: bash (Show other bugs)
25
Unspecified Unspecified
unspecified Severity unspecified
: ---
: ---
Assigned To: Siteshwar Vashisht
Fedora Extras Quality Assurance
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2017-06-06 09:31 EDT by Pavel Raiskup
Modified: 2017-06-14 00:43 EDT (History)
3 users (show)

See Also:
Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2017-06-13 11:11:08 EDT
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Pavel Raiskup 2017-06-06 09:31:09 EDT
Reproducer:

$ cat test_it
#! /bin/bash                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                             
exec 3> <(cat)                                                                                                                                                                                                                               
echo Hello                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                             
# This exits the shell, but it is innocent command actually.                                                                                                                                                                                 
echo my dear >&3                                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
# This is not processed.                                                                                                                                                                                                                     
echo world! 
$ bash test_it
Hello                                                                                                                                                                                                                                        
cat: -: Input/output error
Comment 1 Siteshwar Vashisht 2017-06-13 11:09:40 EDT
On 6/12/17 11:50 AM, Siteshwar Vashisht wrote:
> Hello Chet,
>
> I am currently investigating bug mentioned here [1]. I have come up with attached patch. However it seems there is a race condition while setting up the pipe in process substitution and this fix does not work always. Would it be possible for you to check how to correctly fix this ?

Why is the test case opening a proces substitution that has been opened for
reading (<(cat)) for writing (exec 3>)?

That's not going to work, and most likely will result in the shell getting
a SIGPIPE.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/
Comment 2 Siteshwar Vashisht 2017-06-13 11:10:36 EDT
On 6/13/17 10:36 AM, Siteshwar Vashisht wrote:

>
> SIGPIPE causes shell to exit. I believe if a user makes a mistake of writing to process subsitution that was opened for writing, bash should not exit.

But it wasn't opened for writing.

> But if you believe this is not a bug, I will close it as WONTFIX.

It's a user error the same as any other. SIGPIPE is a fatal signal. You can
always put in a trap handler if you want to try to write to invalid pipe
file descriptors.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/
Comment 3 Pavel Raiskup 2017-06-14 00:43:37 EDT
Some post-mortem facts:

* doing 's|echo my dear|/bin/echo my dear|' behaves differently -- SIGPIPE
  is received by /bin/echo, but it doesn't exit the whole shell.  This is
  what I would expect from builtin too. 

* ksh behaves much more expectedly from this POV:
  $ exec 3> <(cat)
  ksh: syntax error: `>' unexpected

  ... This looks like ksh doesn't support Process Substitution, but in fact
  ksh knows that '<(cat)' is typo and '>(cat)' should be used instead.  So
  the shell exits rather sooner making debugging easier.  Zsh (in bash mode)
  also prints the "syntax error" message.

* is this unexpected behavior of ksh?  Bash/Zsh behave differently when
  I open the pipe for writing:

    $ ln -s /bin/zsh /tmp/bash
    $ cat /tmp/repro 
    #! /bin/bash
    exec 3> >(cat)
    echo Hello
    echo Crazy >&3
    echo $?
    echo World
    $ diff -u <(bash /tmp/repro) <(/tmp/bash /tmp/repro)
    $ diff -u <(bash /tmp/repro) <(ksh /tmp/repro)
    --- /dev/fd/63  2017-06-14 06:37:42.624613999 +0200
    +++ /dev/fd/62  2017-06-14 06:37:42.619614008 +0200
    @@ -1,4 +1,3 @@
     Hello
     0
     World
    -Crazy

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