mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-30 23:51:15 -03:00
Introduce the internal jobs for functions
This PR is aimed at improving how job ids are assigned. In particular, previous to this commit, a job id would be consumed by functions (and thus aliases). Since it's usual to use functions as command wrappers this results in awkward job id assignments. For example if the user is like me and just made the jump from vim -> neovim then the user might create the following alias: ``` alias vim=nvim ``` Previous to this commit if the user ran `vim` after setting up this alias, backgrounded (^Z) and ran `jobs` then the output might be: ``` Job Group State Command 2 60267 stopped nvim $argv ``` If the user subsequently opened another vim (nvim) session, backgrounded and ran jobs then they might see what follows: ``` Job Group State Command 4 70542 stopped nvim $argv 2 60267 stopped nvim $argv ``` These job ids feel unnatural, especially when transitioning away from e.g. bash where job ids are sequentially incremented (and aliases/functions don't consume a job id). See #6053 for more details. As @ridiculousfish pointed out in https://github.com/fish-shell/fish-shell/issues/6053#issuecomment-559899400, we want to elide a job's job id if it corresponds to a single function in the foreground. This translates to the following prerequisites: - A job must correspond to a single process (i.e. the job continuation must be empty) - A job must be in the foreground (i.e. `&` wasn't appended) - The job's single process must resolve to a function invocation If all of these conditions are true then we should mark a job as "internal" and somehow remove it from consideration when any infrastructure tries to interact with jobs / job ids. I saw two paths to implement these requirements: - At the time of job creation calculate whether or not a job is "internal" and use a separate list of job ids to track their ids. Additionally introduce a new flag denoting that a job is internal so that e.g. `jobs` doesn't list internal jobs - I started implementing this route but quickly realized I was computing the same information that would be computed later on (e.g. "is this job a single process" and "is this jobs statement a function"). Specifically I was computing data that populate_job_process would end up computing later anyway. Additionally this added some weird complexities to the job system (after the change there were two job id lists AND an additional flag that had to be taken into consideration) - Once a function is about to be executed we release the current jobs job id if the prerequisites are satisfied (which at this point have been fully computed). - I opted for this solution since it seems cleaner. In this implementation "releasing a job id" is done by both calling `release_job_id` and by marking the internal job_id member variable to -1. The former operation allows subsequent child jobs to reuse that same job id (so e.g. the situation described in Motivation doesn't occur), and the latter ensures that no other job / job id infrastructure will interact with these jobs because valid jobs have positive job ids. The second operation causes job_id to become non-const which leads to the list of code changes outside of `exec.c` (i.e. a codemod from `job_t::job_id` -> `job_t::job_id()` and moving the old member variable to a non-const private `job_t::job_id_`) Note: Its very possible I missed something and setting the job id to -1 will break some other infrastructure, please let me know if so! I tried to run `make/ninja lint`, but a bunch of non-relevant issues appeared (e.g. `fatal error: 'config.h' file not found`). I did successfully clang-format (`git clang-format -f`) and run tests, though. This PR closes #6053.
This commit is contained in:
committed by
ridiculousfish
parent
033a832687
commit
8e17d29e04
22
src/proc.cpp
22
src/proc.cpp
@@ -271,10 +271,12 @@ void process_t::check_generations_before_launch() {
|
||||
|
||||
job_t::job_t(job_id_t job_id, const properties_t &props, const job_lineage_t &lineage)
|
||||
: properties(props),
|
||||
job_id(job_id),
|
||||
job_id_(job_id),
|
||||
root_constructed(lineage.root_constructed ? lineage.root_constructed : this->constructed) {}
|
||||
|
||||
job_t::~job_t() { release_job_id(job_id); }
|
||||
job_t::~job_t() {
|
||||
if (job_id_ != -1) release_job_id(job_id_);
|
||||
}
|
||||
|
||||
void job_t::mark_constructed() {
|
||||
assert(!is_constructed() && "Job was already constructed");
|
||||
@@ -415,7 +417,7 @@ static void print_job_status(const job_t *j, job_status_t status) {
|
||||
if (status == JOB_STOPPED) msg = L"Job %d, '%ls' has stopped";
|
||||
outputter_t outp;
|
||||
outp.writestr("\r");
|
||||
outp.writestr(format_string(_(msg), j->job_id, truncate_command(j->command()).c_str()));
|
||||
outp.writestr(format_string(_(msg), j->job_id(), truncate_command(j->command()).c_str()));
|
||||
if (clr_eol) outp.term_puts(clr_eol, 1);
|
||||
outp.writestr(L"\n");
|
||||
fflush(stdout);
|
||||
@@ -492,14 +494,14 @@ static bool try_clean_process_in_job(process_t *p, job_t *j, std::vector<event_t
|
||||
// We want to report the job number, unless it's the only job, in which case
|
||||
// we don't need to.
|
||||
const wcstring job_number_desc =
|
||||
only_one_job ? wcstring() : format_string(_(L"Job %d, "), j->job_id);
|
||||
only_one_job ? wcstring() : format_string(_(L"Job %d, "), j->job_id());
|
||||
std::fwprintf(stdout, _(L"%ls: %ls\'%ls\' terminated by signal %ls (%ls)"),
|
||||
program_name, job_number_desc.c_str(),
|
||||
truncate_command(j->command()).c_str(), sig2wcs(s.signal_code()),
|
||||
signal_get_desc(s.signal_code()));
|
||||
} else {
|
||||
const wcstring job_number_desc =
|
||||
only_one_job ? wcstring() : format_string(L"from job %d, ", j->job_id);
|
||||
only_one_job ? wcstring() : format_string(L"from job %d, ", j->job_id());
|
||||
const wchar_t *fmt =
|
||||
_(L"%ls: Process %d, \'%ls\' %ls\'%ls\' terminated by signal %ls (%ls)");
|
||||
std::fwprintf(stdout, fmt, program_name, p->pid, p->argv0(), job_number_desc.c_str(),
|
||||
@@ -595,7 +597,7 @@ static bool process_clean_after_marking(parser_t &parser, bool allow_interactive
|
||||
proc_create_event(L"JOB_EXIT", event_type_t::exit, -j->pgid, 0));
|
||||
}
|
||||
exit_events.push_back(
|
||||
proc_create_event(L"JOB_EXIT", event_type_t::job_exit, j->job_id, 0));
|
||||
proc_create_event(L"JOB_EXIT", event_type_t::job_exit, j->job_id(), 0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -757,8 +759,8 @@ int terminal_maybe_give_to_job(const job_t *j, bool continuing_from_stopped) {
|
||||
if (errno == ENOTTY) {
|
||||
redirect_tty_output();
|
||||
}
|
||||
debug(1, _(L"Could not send job %d ('%ls') with pgid %d to foreground"), j->job_id,
|
||||
j->command_wcstr(), j->pgid);
|
||||
debug(1, _(L"Could not send job %d ('%ls') with pgid %d to foreground"),
|
||||
j->job_id(), j->command_wcstr(), j->pgid);
|
||||
wperror(L"tcsetpgrp");
|
||||
return error;
|
||||
}
|
||||
@@ -785,7 +787,7 @@ int terminal_maybe_give_to_job(const job_t *j, bool continuing_from_stopped) {
|
||||
redirect_tty_output();
|
||||
}
|
||||
|
||||
debug(1, _(L"Could not send job %d ('%ls') to foreground"), j->job_id,
|
||||
debug(1, _(L"Could not send job %d ('%ls') to foreground"), j->job_id(),
|
||||
j->preview().c_str());
|
||||
wperror(L"tcsetattr");
|
||||
return error;
|
||||
@@ -852,7 +854,7 @@ void job_t::continue_job(parser_t &parser, bool reclaim_foreground_pgrp, bool se
|
||||
mut_flags().notified = false;
|
||||
|
||||
FLOGF(proc_job_run, L"%ls job %d, gid %d (%ls), %ls, %ls",
|
||||
send_sigcont ? L"Continue" : L"Start", job_id, pgid, command_wcstr(),
|
||||
send_sigcont ? L"Continue" : L"Start", job_id_, pgid, command_wcstr(),
|
||||
is_completed() ? L"COMPLETED" : L"UNCOMPLETED",
|
||||
parser.libdata().is_interactive ? L"INTERACTIVE" : L"NON-INTERACTIVE");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user