Skip to content

Commit

Permalink
Shell: Avoid waiting for jobs that were *just* unblocked
Browse files Browse the repository at this point in the history
This fixes the issue with C-z not suspending the job on the first try.
...and further signal issues when the suspended job is contiued.
  • Loading branch information
alimpfard authored and awesomekling committed Jul 13, 2020
1 parent 46661f0 commit dc62371
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Shell/Builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ int Shell::builtin_bg(int argc, const char** argv)
}

job->set_running_in_background(true);
job->set_is_suspended(false);

dbg() << "Resuming " << job->pid() << " (" << job->cmd() << ")";
fprintf(stderr, "Resuming job %" PRIu64 " - %s\n", job->job_id(), job->cmd().characters());
Expand Down Expand Up @@ -339,6 +340,7 @@ int Shell::builtin_fg(int argc, const char** argv)
}

job->set_running_in_background(false);
job->set_is_suspended(false);

dbg() << "Resuming " << job->pid() << " (" << job->cmd() << ")";
fprintf(stderr, "Resuming job %" PRIu64 " - %s\n", job->job_id(), job->cmd().characters());
Expand Down
4 changes: 4 additions & 0 deletions Shell/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Job : public RefCounted<Job> {
bool should_be_disowned() const { return m_should_be_disowned; }
void disown() { m_should_be_disowned = true; }
bool is_running_in_background() const { return m_running_in_background; }
bool is_suspended() const { return m_is_suspended; }
void unblock() const
{
if (!m_exited && on_exit)
Expand All @@ -94,6 +95,8 @@ class Job : public RefCounted<Job> {
on_exit(*this);
}

void set_is_suspended(bool value) const { m_is_suspended = value; }

void set_running_in_background(bool running_in_background)
{
m_running_in_background = running_in_background;
Expand All @@ -111,5 +114,6 @@ class Job : public RefCounted<Job> {
int m_exit_code { -1 };
Core::ElapsedTimer m_command_timer;
mutable bool m_active { true };
mutable bool m_is_suspended { false };
bool m_should_be_disowned { false };
};
8 changes: 5 additions & 3 deletions Shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,18 @@ Vector<RefPtr<Job>> Shell::run_commands(Vector<AST::Command>& commands)
}
#endif
auto job = run_command(command);
if (!job)
continue;

if (command.should_wait) {
block_on_job(job);
jobs_to_wait_for.append(job);
if (!job->is_suspended())
jobs_to_wait_for.append(job);
} else {
if (command.is_pipe_source) {
jobs_to_wait_for.append(job);
} else if (command.should_notify_if_in_background) {
if (job)
job->set_running_in_background(true);
job->set_running_in_background(true);
restore_stdin();
}
}
Expand Down
4 changes: 3 additions & 1 deletion Shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ int main(int argc, char** argv)
Core::EventLoop::register_signal(SIGTSTP, [](auto) {
auto job = s_shell->current_job();
s_shell->kill_job(job, SIGTSTP);
if (job)
if (job) {
job->set_is_suspended(true);
job->unblock();
}
});

#ifndef __serenity__
Expand Down

0 comments on commit dc62371

Please sign in to comment.