Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tui): avoid tui shutdown on unrecognized input #9289

Merged
merged 4 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions crates/turborepo-lib/src/run/summary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ impl<'a> RunSummary<'a> {
}

if let Some(spaces_client_handle) = self.spaces_client_handle.take() {
self.send_to_space(spaces_client_handle, end_time, exit_code)
self.send_to_space(spaces_client_handle, end_time, exit_code, is_watch)
.await;
}

Expand All @@ -395,26 +395,35 @@ impl<'a> RunSummary<'a> {
spaces_client_handle: SpacesClientHandle,
ended_at: DateTime<Local>,
exit_code: i32,
is_watch: bool,
) {
let spinner = tokio::spawn(async {
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
turborepo_ui::start_spinner("...sending run summary...");
let spinner = (!is_watch).then(|| {
tokio::spawn(async {
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
turborepo_ui::start_spinner("...sending run summary...");
})
});

// We log the error here but don't fail because
// failing to send the space shouldn't fail the run.
if let Err(err) = spaces_client_handle.finish_run(exit_code, ended_at).await {
warn!("Error sending to space: {}", err);
if !is_watch {
warn!("Error sending to space: {}", err);
}
};

let result = spaces_client_handle.close().await;

spinner.abort();
if let Some(spinner) = spinner {
spinner.abort();
}

Self::print_errors(&result.errors);
if !is_watch {
Self::print_errors(&result.errors);

if let Some(run) = result.run {
println!("Run: {}\n", run.url);
if let Some(run) = result.run {
println!("Run: {}\n", run.url);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/turborepo-lib/src/run/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub enum Error {
PackageChange(#[from] tonic::Status),
#[error(transparent)]
UI(#[from] turborepo_ui::Error),
#[error("could not connect to UI thread")]
#[error("could not connect to UI thread: {0}")]
UISend(String),
#[error("cannot use root turbo.json at {0} with watch mode")]
NonStandardTurboJsonPath(String),
Expand Down Expand Up @@ -317,7 +317,7 @@ impl WatchClient {
let task_names = run.engine.tasks_with_command(&run.pkg_dep_graph);
sender
.restart_tasks(task_names)
.map_err(|err| Error::UISend(err.to_string()))?;
.map_err(|err| Error::UISend(format!("some packages changed: {err}")))?;
}

let ui_sender = self.ui_sender.clone();
Expand Down Expand Up @@ -371,7 +371,7 @@ impl WatchClient {
let task_names = self.run.engine.tasks_with_command(&self.run.pkg_dep_graph);
sender
.update_tasks(task_names)
.map_err(|err| Error::UISend(err.to_string()))?;
.map_err(|err| Error::UISend(format!("all packages changed {err}")))?;
}

if self.run.has_non_interruptible_tasks() {
Expand Down
44 changes: 27 additions & 17 deletions crates/turborepo-ui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,14 @@ pub async fn run_app(tasks: Vec<String>, receiver: AppReceiver) -> Result<(), Er
let (result, callback) =
match run_app_inner(&mut terminal, &mut app, receiver, crossterm_rx).await {
Ok(callback) => (Ok(()), callback),
Err(err) => (Err(err), None),
Err(err) => {
debug!("tui shutting down: {err}");
(Err(err), None)
}
};

debug!("it's real, we're shutting down");
chris-olszewski marked this conversation as resolved.
Show resolved Hide resolved

cleanup(terminal, app, callback)?;

result
Expand Down Expand Up @@ -634,25 +639,28 @@ async fn poll<'a>(
crossterm_rx: &mut mpsc::Receiver<crossterm::event::Event>,
) -> Option<Event> {
let input_closed = crossterm_rx.is_closed();
let input_fut = async {
crossterm_rx
.recv()
.await
.and_then(|event| input_options.handle_crossterm_event(event))
};
let receiver_fut = async { receiver.recv().await };
let event_fut = async move {
if input_closed {
receiver_fut.await
} else {

if input_closed {
receiver.recv().await
} else {
// tokio::select is messing with variable read detection
#[allow(unused_assignments)]
let mut event = None;
loop {
tokio::select! {
e = input_fut => e,
e = receiver_fut => e,
e = crossterm_rx.recv() => {
event = e.and_then(|e| input_options.handle_crossterm_event(e));
}
e = receiver.recv() => {
event = e;
}
}
if event.is_some() {
break;
}
}
};

event_fut.await
event
}
}

const MIN_HEIGHT: u16 = 10;
Expand Down Expand Up @@ -729,9 +737,11 @@ fn update(
app.set_status(task, status, result)?;
}
Event::InternalStop => {
debug!("shutting down due to internal failure");
app.done = true;
}
Event::Stop(callback) => {
debug!("shutting down due to message");
app.done = true;
return Ok(Some(callback));
}
Expand Down
7 changes: 6 additions & 1 deletion crates/turborepo-ui/src/tui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ fn translate_key_event(options: InputOptions, key_event: KeyEvent) -> Option<Eve
#[cfg(unix)]
fn ctrl_c() -> Option<Event> {
use nix::sys::signal;
use tracing::debug;
match signal::raise(signal::SIGINT) {
Ok(_) => None,
// We're unable to send the signal, stop rendering to force shutdown
Err(_) => Some(Event::InternalStop),
Err(_) => {
debug!("unable to send sigint, shutting down");
Some(Event::InternalStop)
}
}
}

Expand All @@ -146,6 +150,7 @@ fn ctrl_c() -> Option<Event> {
None
} else {
// We're unable to send the Ctrl-C event, stop rendering to force shutdown
debug!("unable to send sigint, shutting down");
Some(Event::InternalStop)
}
}
Expand Down
Loading