Skip to content

Commit 99e9652

Browse files
committed
fix: keep search cancellation within safety guardrails (refs #1932)
1 parent dbb8682 commit 99e9652

5 files changed

Lines changed: 27 additions & 21 deletions

File tree

‎crates/jcode-app-core/src/tool/agentgrep.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,11 @@ async fn await_or_background_search(
319319
session_id: String,
320320
) -> Result<ToolOutput> {
321321
let mut owner = SearchTaskOwner(Some(work_handle));
322-
match tokio::time::timeout(foreground_budget, owner.0.as_mut().unwrap()).await {
322+
let work_handle = owner
323+
.0
324+
.as_mut()
325+
.ok_or_else(|| anyhow::anyhow!("agentgrep task owner is missing"))?;
326+
match tokio::time::timeout(foreground_budget, work_handle).await {
323327
Ok(joined) => {
324328
joined.map_err(|err| anyhow::anyhow!("agentgrep task failed to join: {err}"))?
325329
}
@@ -333,7 +337,10 @@ async fn await_or_background_search(
333337
false,
334338
// Transfer, do not cancel: the background manager becomes
335339
// the sole owner, including while adoption is awaiting.
336-
owner.0.take().unwrap(),
340+
owner
341+
.0
342+
.take()
343+
.ok_or_else(|| anyhow::anyhow!("agentgrep task owner is missing"))?,
337344
)
338345
.await;
339346
Ok(ToolOutput::new(format!(

‎crates/jcode-app-core/src/tool/agentgrep_tests.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async fn assert_search_stopped(stopped: tokio::sync::oneshot::Receiver<String>)
129129
}
130130

131131
#[tokio::test]
132-
async fn cancelling_foreground_future_stops_linked_search() {
132+
async fn search_cancellation_foreground_future_stops_linked_search() {
133133
let root = tempfile::tempdir().unwrap();
134134
fs::write(root.path().join("input.rs"), "fn needle() {}\n").unwrap();
135135
let (handle, started, stopped) = repeating_search(root.path().to_path_buf());
@@ -149,7 +149,7 @@ async fn cancelling_foreground_future_stops_linked_search() {
149149
}
150150

151151
#[tokio::test]
152-
async fn dropping_search_owner_stops_linked_search() {
152+
async fn search_cancellation_dropping_owner_stops_linked_search() {
153153
let root = tempfile::tempdir().unwrap();
154154
fs::write(root.path().join("input.rs"), "fn needle() {}\n").unwrap();
155155
let (handle, started, stopped) = repeating_search(root.path().to_path_buf());
@@ -163,7 +163,7 @@ async fn dropping_search_owner_stops_linked_search() {
163163
}
164164

165165
#[tokio::test]
166-
async fn promoted_search_lives_until_background_cancel() {
166+
async fn search_cancellation_promoted_search_lives_until_background_cancel() {
167167
let root = tempfile::tempdir().unwrap();
168168
fs::write(root.path().join("input.rs"), "fn needle() {}\n").unwrap();
169169
let (handle, started, mut stopped) = repeating_search(root.path().to_path_buf());
@@ -193,7 +193,7 @@ async fn promoted_search_lives_until_background_cancel() {
193193
}
194194

195195
#[tokio::test]
196-
async fn background_reload_stops_linked_search() {
196+
async fn search_cancellation_background_reload_stops_linked_search() {
197197
let root = tempfile::tempdir().unwrap();
198198
fs::write(root.path().join("input.rs"), "fn needle() {}\n").unwrap();
199199
let (handle, started, stopped) = repeating_search(root.path().to_path_buf());

‎crates/jcode-base/src/background.rs‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ use model::{
3232
progress_event_record, progress_wait_reason, push_task_event, task_dir, terminal_event_record,
3333
};
3434

35-
/// Own an adopted task rather than detaching it when its wrapper is aborted.
36-
/// Construct before the first await, including before spawning the wrapper, so
37-
/// cancellation during registration or before its first poll is also safe.
38-
struct AbortAdoptedTask<T>(JoinHandle<T>);
39-
40-
impl<T> Drop for AbortAdoptedTask<T> {
41-
fn drop(&mut self) {
42-
self.0.abort();
43-
}
44-
}
45-
4635
/// Manages background task execution
4736
pub struct BackgroundTaskManager {
4837
tasks: Arc<RwLock<HashMap<String, RunningTask>>>,
@@ -659,7 +648,7 @@ impl BackgroundTaskManager {
659648
wake: bool,
660649
handle: JoinHandle<Result<jcode_tool_types::ToolOutput>>,
661650
) -> BackgroundTaskInfo {
662-
let mut handle = AbortAdoptedTask(handle);
651+
let mut handle = model::AbortAdoptedTask(handle);
663652
// Reserve before the foreground caller can release its turn/tool guard.
664653
let runtime_activity = crate::runtime_activity::global().enter().await;
665654
let (notify, wake) = normalize_delivery(notify, wake);
@@ -716,7 +705,6 @@ impl BackgroundTaskManager {
716705
let (delivery_flags_tx, delivery_flags_rx) = watch::channel((notify, wake));
717706
let tasks_for_prune = Arc::clone(&self.tasks);
718707
let (registered_tx, registered_rx) = tokio::sync::oneshot::channel::<()>();
719-
720708
let wrapper_handle = tokio::spawn(async move {
721709
let _runtime_activity = runtime_activity;
722710
let tool_result = (&mut handle.0).await;

‎crates/jcode-base/src/background/model.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ use std::time::Instant;
77
use tokio::sync::watch;
88
use tokio::task::JoinHandle;
99

10+
/// Own an adopted task rather than detaching it when its wrapper is aborted.
11+
/// Construct before the first await, including before spawning the wrapper, so
12+
/// cancellation during registration or before its first poll is also safe.
13+
pub(super) struct AbortAdoptedTask<T>(pub(super) JoinHandle<T>);
14+
15+
impl<T> Drop for AbortAdoptedTask<T> {
16+
fn drop(&mut self) {
17+
self.0.abort();
18+
}
19+
}
20+
1021
/// Directory for background task output files
1122
pub(super) fn task_dir() -> PathBuf {
1223
std::env::temp_dir().join("jcode-bg-tasks")

‎crates/jcode-base/src/background/tests.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ async fn assert_adopted_owner_cancelled(reload: bool) -> Result<()> {
3838
}
3939

4040
#[tokio::test]
41-
async fn cancelling_adopted_task_drops_actual_owner() -> Result<()> {
41+
async fn adopted_task_cancellation_drops_actual_owner() -> Result<()> {
4242
assert_adopted_owner_cancelled(false).await
4343
}
4444

4545
#[tokio::test]
46-
async fn reload_drops_adopted_task_owner() -> Result<()> {
46+
async fn adopted_task_reload_drops_owner() -> Result<()> {
4747
assert_adopted_owner_cancelled(true).await
4848
}
4949

0 commit comments

Comments
 (0)