#1988 non-interactive /bin/sh crash running simple script with pipe, shell function and stdout & stderr redirection

MSYS
closed
nobody
None
Bug
invalid
User_Error
False
2014-12-11
2013-06-09
No

Msys /bin/sh shell crashes when running the following script without using an interactive /bin/sh shell:

1
2
3
4
5
#!/bin/sh
fifi () {
    /bin/sh -c "echo ...lala..."
}
echo huhu | fifi >/dev/null 2>&1

To reproduce, save the script as 'script.sh', start an Windows cmd.exe shell in the folder containing the script and run the following from its console (adjust the /bin/sh executable path as needed):

C:\MinGW\msys\1.0\bin\sh.exe script.sh

Crashed shell takes down the whole calling Windows cmd.exe shell process along with it (its console 'disappears' & the calling cmd.exe process terminates).

The crash does NOT occur if there is an interactive /bin/sh shell anywhere in the calling process chain, e.g. in any of the following scenarios:
1. an interactive /bin/sh shell running the script.
2. an interactive /bin/sh shell calling a Windows cmd.exe shell calling a non-interactive /bin/sh shell running the script.

The crash does NOT cause the crash if any of the following changes are made to the script:
1. Removing the pipe and the piped-to ('fifi') command.
2. Removing the pipe and the piped ('echo huhu') command.
3. Removing stderr redirection.
4. Removing stdout redirection.
5. Swapping the order of stderr & stdout redirection.
6. Replacing the piped-to ('fifi') command with a direct external process call instead of a shell function.
7. Simplifying the 'fifi()' shell function body to just 'echo ...lala...' without the explicit /bin/sh call.

The crash has been reproduced using the following Windows OS:

Windows 7 Ultimate, x64, SP1, full Windows Updates applied

The crash has been reproduced using the following shell versions:

GNU bash, version 3.1.17(1)-release (i686-pc-msys)
(as distributed by the msys installer)

GNU bash, version 3.1.0(1)-release (i686-pc-msys)
(as distributed by the msysgit/Git for Windows project)

The crash could NOT be reproduced using the Cygwin /bin/sh shell:

GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)

Hope this helps.

Best regards,
Jurko Gospodnetić

Discussion

  • The crash occurs even if stdout is redirected to a regular file instead of just /dev/null. In that case the stdout content is correctly stored in the target file before the crash.

     
  • It seems that applying the KB2458000 Windows hot-fix resolves the issue (suggested by Peter Harris on the msysgit mailing list).

    That hot-fix resolves a known issue in the Microsoft Windows implementation:

    This issue occurs because of a reference-counting problem in the csrss.exe process for console handles. The csrss.exe process that maintains the reference count for console buffers is wrong, and the output buffer is freed prematurely.

    Since the hot-fix is at the moment over 2 and a half years old and still has not been included in any of the officially released Windows Updates, and the issues caused by it seem real difficult to debug and diagnose, I would suggest updating the msys /bin/sh implementation to avoid the problem all together. Otherwise users are forced to jump through hoops and then after a long debugging period either give up or possibly find out they need to manually install some obscure M$ hot-fix.

    Hope this helps.

    Best regards,
    Jurko Gospodnetić

     
  • Earnie Boyd
    Earnie Boyd
    2013-06-10

    • status: unread --> closed
    • Resolution: none --> invalid
    • Category: Unknown --> User_Error
     
  • Earnie Boyd
    Earnie Boyd
    2013-06-10

    We do not support the use of MSYS outside of the interactive MSYS shell itself.

    The issue becomes who controls the pipes and redirection and in your scenario MSYS is not in control of all of them. The mixture is extremely volatile and we cannot control the outcome.