Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: bug in zsh wait builtin - rhbz#1150541



On Tue, 21 Oct 2014 09:53:33 +0200
Tim Speetjens <tim.speetjens@xxxxxxxxx> wrote:
> I'ld like to report a bug originally filed in
> https://bugzilla.redhat.com/show_bug.cgi?id=1150541 which is still present
> in the latest version, 5.0.7
> 
> Title:
> zsh wait builtin shows an error and doesn't propagate exit code for a
> finished child process

There's an explanatory note in the latest POSIX standard about this,
quoted below.  It seems that the shell is basically required to remember
all background processes indefinitely (up to not very helpful get out
clauses).  As a baseline, CHILD_MAX here is 1024.  This probably needs
to be a special hash.


On most implementations, wait is a shell built-in. If it is called in a
subshell or separate utility execution environment, such as one of the
following:

    (wait)
    nohup wait ...
    find . -exec wait ... \;

it returns immediately because there are no known process IDs to wait
for in those environments.

Historical implementations of interactive shells have discarded the exit
status of terminated background processes before each shell
prompt. Therefore, the status of background processes was usually lost
unless it terminated while wait was waiting for it. This could be a
serious problem when a job that was expected to run for a long time
actually terminated quickly with a syntax or initialization error
because the exit status returned was usually zero if the requested
process ID was not found. This volume of POSIX.1-2008 requires the
implementation to keep the status of terminated jobs available until the
status is requested, so that scripts like:

    j1&
    p1=$!
    j2&
    wait $p1
    echo Job 1 exited with status $?
    wait $!
    echo Job 2 exited with status $?

work without losing status on any of the jobs. The shell is allowed to
discard the status of any process if it determines that the application
cannot get the process ID for that process from the shell. It is also
required to remember only {CHILD_MAX} number of processes in this
way. Since the only way to get the process ID from the shell is by using
the '!' shell parameter, the shell is allowed to discard the status of
an asynchronous list if "$!" was not referenced before another
asynchronous list was started. (This means that the shell only has to
keep the status of the last asynchronous list started if the application
did not reference "$!". If the implementation of the shell is smart
enough to determine that a reference to "$!" was not saved anywhere that
the application can retrieve it later, it can use this information to
trim the list of saved information. Note also that a successful call to
wait with no operands discards the exit status of all asynchronous
lists.)

If the exit status of wait is greater than 128, there is no way for the
application to know if the waited-for process exited with that value or
was killed by a signal. Since most utilities exit with small values,
there is seldom any ambiguity. Even in the ambiguous cases, most
applications just need to know that the asynchronous job failed; it does
not matter whether it detected an error and failed or was killed and did
not complete its job normally.

pws



Messages sorted by: Reverse Date, Date, Thread, Author