Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/goose-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tower-http = { version = "0.5", features = ["cors", "fs", "auth"] }
http = "1.0"
webbrowser = "1.0"
indicatif = "0.17.11"
tokio-util = { version = "0.7.15", features = ["compat"] }
tokio-util = { version = "0.7.15", features = ["compat", "rt"] }
is-terminal = "0.4.16"
anstream = "0.6.18"
url = "2.5.7"
Expand Down
1 change: 1 addition & 0 deletions crates/goose-cli/src/session/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn get_input(
Ok(text) => text,
Err(e) => match e {
rustyline::error::ReadlineError::Interrupted => return Ok(InputResult::Exit),
rustyline::error::ReadlineError::Eof => return Ok(InputResult::Exit),
_ => return Err(e.into()),
},
};
Expand Down
17 changes: 13 additions & 4 deletions crates/goose-cli/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::session::task_execution_display::{
use goose::conversation::Conversation;
use std::io::Write;
use std::str::FromStr;
use tokio::signal::ctrl_c;
use tokio_util::task::AbortOnDropHandle;

pub use self::export::message_to_markdown;
pub use builder::{build_session, SessionBuilderConfig, SessionSettings};
Expand Down Expand Up @@ -775,8 +777,6 @@ impl CliSession {
interactive: bool,
cancel_token: CancellationToken,
) -> Result<()> {
let cancel_token_clone = cancel_token.clone();

// Cache the output format check to avoid repeated string comparisons in the hot loop
let is_json_mode = self.output_format == "json";

Expand All @@ -790,6 +790,15 @@ impl CliSession {
.messages
.last()
.ok_or_else(|| anyhow::anyhow!("No user message"))?;

let cancel_token_interrupt = cancel_token.clone();
let handle = tokio::spawn(async move {
if ctrl_c().await.is_ok() {
cancel_token_interrupt.cancel();
}
});
Comment on lines 794 to 799
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

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

The spawned task is not tracked or joined, which could lead to it outliving the function scope. If process_agent_response returns before Ctrl+C is pressed, the spawned task will continue running and may trigger cancellation on an already-consumed token. Consider storing the task handle and aborting it before the function returns, or using tokio::select! with a scoped spawn pattern to ensure proper cleanup.

Copilot uses AI. Check for mistakes.
let _drop_handle = AbortOnDropHandle::new(handle);

let mut stream = self
.agent
.reply(
Expand All @@ -800,6 +809,7 @@ impl CliSession {
.await?;

let mut progress_bars = output::McpSpinners::new();
let cancel_token_clone = cancel_token.clone();

use futures::StreamExt;
loop {
Expand Down Expand Up @@ -1075,8 +1085,7 @@ impl CliSession {
None => break,
}
}
_ = tokio::signal::ctrl_c() => {
cancel_token_clone.cancel();
_ = cancel_token_clone.cancelled() => {
drop(stream);
if let Err(e) = self.handle_interrupted_messages(true).await {
eprintln!("Error handling interruption: {}", e);
Expand Down