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

Re: unable to wait on completed job [was: should $! give the pid of subshell?]



On Nov 29, 11:04pm, Greg Klanderman wrote:
} Subject: unable to wait on completed job [was: should $! give the pid of s
} 
} Shouldn't I be able to wait on a job that has completed in zsh?

I'd be inclined to respond "only if you set the POSIX_JOBS option," but
that doesn't appear to suffice either (and in fact has a bug).

In any case this is one of those places where zsh does not claim full
compatibility with POSIX.

} Using bash or ksh, I can successfully wait on the completed job.

Hmm.  [The extra "exit 19" below is so I could check whether bash
correctly reports the status of the waited-for PID.]

[schaefer@torch]$ (sleep 10; exit 19) &
[1] 1680
[schaefer@torch]$ pid=$!
[schaefer@torch]$ sleep 12; wait $pid
[1]+  Exit 19                 ( sleep 10; exit 19 )
bash: wait: pid 1680 is not a child of this shell
[schaefer@torch]$ echo $BASH_VERSION
3.00.15(1)-release

However, "wait" works as Greg describes in bash version 3.2.25(1)-release.
(More on this below [*].)

}   #!/bin/zsh -f
}   sleep 10 &
}   pid=$!
}   sleep 12
}   wait $pid

(The bug I mentioned:  If I run those commands in a subshell of an
interactive shell with POSIX_JOBS set, the subshell gets hit with a
SIGTTOU signal and the parent shell ends up hung waiting for it.)

Apparently POSIX.2 requires "wait" be able to access the exit status
of an asynchronous job until the next asynchronous job is started.
Zsh is only able to access that status until the next job (whether
synchronous or not) completes; the completion of "sleep 12" clears
the previously-completed "sleep 10 &" from the job table, so "wait"
is no longer able to see it.

"Fixing" this may be a fairly convoluted change to the already somewhat
baroque job table management code.

[*] The CHANGES file with bash-3.2 appears to claim this was changed a
lot longer ago than bash-3.0; some possibly-relevant entries:

----------
This document details the changes between this version, bash-3.1-rc1,
and the previous version, bash-3.1-beta1.

...

e.  In Posix mode, after `wait' is called to wait for a particular process
    explicitly, that process is removed from the list of processes known to
    the shell, and subsequent attempts to wait for it return errors.

----------
This document details the changes between this version, bash-2.03-beta1,
and the previous version, bash-2.03-alpha.

...

j.  Some changes were made to the job reaping code when the shell is not
    interactive, so the shell will retain exit statuses longer for examination
    by `wait'.

----------
This document details the changes between this version, bash-2.02-alpha1,
and the previous version, bash-2.01.1-release.

...

q.  A bug was fixed so that the job containing the last asynchronous
    process is not removed from the job table until a `wait' is executed
    for that process or another asynchronous process is started.  This
    satisfies a POSIX.2 requirement.

----------



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