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

CLI flag for disabling interrupts #186

Merged
merged 8 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ pub struct Opts {
/// Options to modify logging and error-handling behavior.
#[command(flatten)]
pub logging: LoggingOpts,

/// By default, ghciwatch will interrupt reloads if a file changes. If you want ghciwatch to
/// avoid interrupting reloads, set this flag.
#[arg(long = "no-interrupt-reloads")]
pub no_interrupt_reloads: bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super happy with a --no-interrupt-reloads flag - feels kind of awkward. But --interrupt-reloads is the default behavior.

We could switch the default back to no-interrupt, and then this flag can be --interrupt-reloads.

Made a slack convo to ask about this:
https://mercurytechnologies.slack.com/archives/CUJ89R895/p1702483630925389

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an open issue for automatic negation flags (i.e. adding --interrupt-reloads and --no-interrupt-reloads at once) here:

clap-rs/clap#815

}

/// Options for watching files.
Expand Down
29 changes: 16 additions & 13 deletions src/ghci/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub async fn run_ghci(
// This function is pretty tricky! We need to handle shutdowns at each stage, and the process
// is a little different each time, so the `select!`s can't be consolidated.

let no_interrupt_reloads = opts.no_interrupt_reloads;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely feels awkward. I tried passing the opts directly in to the relevant function, but the opts are moved into Ghci::new call just below. I can get the bool out here no problem, though, so I do that instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this is fine.

let mut ghci = Ghci::new(handle.clone(), opts)
.await
.wrap_err("Failed to start `ghci`")?;
Expand Down Expand Up @@ -113,19 +114,21 @@ pub async fn run_ghci(
}
Some(new_event) = receiver.recv() => {
tracing::debug!(?new_event, "Received ghci event from watcher while reloading");
if should_interrupt(reload_receiver).await {
// Merge the events together so we don't lose progress.
// Then, the next iteration of the loop will pick up the `maybe_event` value
// and respond immediately.
event.merge(new_event);
maybe_event = Some(event);

// Cancel the in-progress reload. This releases the `ghci` lock to prevent a deadlock.
task.abort();

// Send a SIGINT to interrupt the reload.
// NB: This may take a couple seconds to register.
ghci.lock().await.send_sigint().await?;
if !no_interrupt_reloads {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's our behavior change: if we set this flag, then we do not go into that block

if should_interrupt(reload_receiver).await {
// Merge the events together so we don't lose progress.
// Then, the next iteration of the loop will pick up the `maybe_event` value
// and respond immediately.
event.merge(new_event);
maybe_event = Some(event);

// Cancel the in-progress reload. This releases the `ghci` lock to prevent a deadlock.
task.abort();

// Send a SIGINT to interrupt the reload.
// NB: This may take a couple seconds to register.
ghci.lock().await.send_sigint().await?;
}
}
}
ret = &mut task => {
Expand Down
3 changes: 3 additions & 0 deletions src/ghci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub struct GhciOpts {
pub restart_globs: GlobMatcher,
/// Reload the `ghci` session when paths matching these globs are changed.
pub reload_globs: GlobMatcher,
/// Determines whether we should interrupt a reload in progress or not.
pub no_interrupt_reloads: bool,
/// Where to write what `ghci` emits to `stdout`. Inherits parent's `stdout` by default.
pub stdout_writer: GhciWriter,
/// Where to write what `ghci` emits to `stderr`. Inherits parent's `stderr` by default.
Expand All @@ -128,6 +130,7 @@ impl GhciOpts {
hooks: opts.hooks.clone(),
restart_globs: opts.watch.restart_globs()?,
reload_globs: opts.watch.reload_globs()?,
no_interrupt_reloads: opts.no_interrupt_reloads,
stdout_writer: GhciWriter::stdout(),
stderr_writer: GhciWriter::stderr(),
})
Expand Down