Fix assertion failure on job redirection error

Fix an error caused by `exec_job()` assuming a job launched with the
intention of being backgrounded would have a pgid assigned in all cases,
without considering the status of `exec_error` which could have resulted
in the job failing before it was launched into its own process group.

Fixes (but doesn't close) #7423 - that can be closed if this assertion
failure doesn't happen in any released fish versions.
This commit is contained in:
Mahmoud Al-Qudsi 2020-10-24 16:11:46 -05:00
parent 4bfda47449
commit 64671c64a1

View file

@ -1045,9 +1045,11 @@ bool exec_job(parser_t &parser, const shared_ptr<job_t> &j, const io_chain_t &bl
j->command_wcstr(), j->get_pgid() ? *j->get_pgid() : -2);
j->mark_constructed();
if (!j->is_foreground()) {
auto pgid = j->get_pgid();
assert(pgid.has_value() && "Background jobs should always have a pgroup");
// If exec_error then a backgrounded job would have been terminated before it was ever assigned
// a pgroup, so error out before setting last_pid.
auto pgid = j->get_pgid();
if (j->is_foreground() && pgid.has_value()) {
parser.vars().set_one(L"last_pid", ENV_GLOBAL, to_string(*pgid));
}