Run: Don't wait for child process to end when adding it to history

When we try to start a long-running child process (e.g. a GUI app)
using a combination of the POSIX spawn and waitpid API, the Run
process ends up waiting for it to end before making any changes
to the path history. This leads to some confusion when trying to
fire up another Run process only to see that it did not save the
path to this program.

This PR resolves this by saving the path after it was created using
the POSIX spawn API.
This commit is contained in:
Kemal Zebari 2024-03-03 12:49:03 -08:00 committed by Tim Schumacher
parent 05731f93b6
commit 8893836b60
Notes: sideshowbarker 2024-07-17 02:38:39 +09:00
2 changed files with 18 additions and 7 deletions

View file

@ -96,12 +96,6 @@ void RunWindow::do_run()
hide();
if (run_via_launch(run_input) || run_as_command(run_input)) {
// Remove any existing history entry, prepend the successful run string to history and save.
m_path_history.remove_all_matching([&](ByteString v) { return v == run_input; });
m_path_history.prepend(run_input);
// FIXME: Handle failure to save history somehow.
(void)save_history();
close();
return;
}
@ -120,7 +114,11 @@ bool RunWindow::run_as_command(ByteString const& run_input)
pid_t child_pid = maybe_child_pid.release_value();
// Command spawned in child shell. Hide and wait for exit code.
// The child shell was able to start. Let's save it to the history immediately so users can see it as the first entry the next time they run this program.
prepend_history(run_input);
// FIXME: Handle failure to save history somehow.
(void)save_history();
int status;
if (waitpid(child_pid, &status, 0) < 0)
return false;
@ -130,6 +128,8 @@ bool RunWindow::run_as_command(ByteString const& run_input)
// 127 is typically the shell indicating command not found. 126 for all other errors.
if (child_error == 126 || child_error == 127) {
// There's an opportunity to remove the history entry here since it failed during its runtime, but other implementations (e.g. Windows 11) don't bother removing the entry.
// This makes sense, especially for cases where a user is debugging a failing program.
return false;
}
@ -157,6 +157,10 @@ bool RunWindow::run_via_launch(ByteString const& run_input)
return false;
}
prepend_history(run_input);
// FIXME: Handle failure to save history somehow.
(void)save_history();
dbgln("Ran via URL launch.");
return true;
@ -182,6 +186,12 @@ ErrorOr<void> RunWindow::load_history()
return {};
}
void RunWindow::prepend_history(ByteString const& input)
{
m_path_history.remove_all_matching([&](ByteString const& entry) { return input == entry; });
m_path_history.prepend(input);
}
ErrorOr<void> RunWindow::save_history()
{
auto file = TRY(Core::File::open(history_file_path(), Core::File::OpenMode::Write));

View file

@ -32,6 +32,7 @@ private:
ByteString history_file_path();
ErrorOr<void> load_history();
ErrorOr<void> save_history();
void prepend_history(ByteString const& input);
Vector<ByteString> m_path_history;
NonnullRefPtr<GUI::ItemListModel<ByteString>> m_path_history_model;