fix(mcp-memory,server): prevent lock contention and executor blocking in TasksTool::List

This commit is contained in:
Riz Ashraf committed 2026-10-11 18:31:26 +01:00
1 parent 0afba74b4c
commit 9e02639d0d
1 file changed
+11 -10
+11 -10
View File
@@ -256,10 +256,10 @@ impl McpTool for TasksHandler {
} }
} }
TasksTool::List { git_branch, status, summary_level, max_tokens, offset } => { TasksTool::List { git_branch, status, summary_level, max_tokens, offset } => {
let level = summary_level.as_deref().unwrap_or("detailed"); let level = summary_level.clone().unwrap_or_else(|| "detailed".to_string());
let offset = offset.unwrap_or(0); let offset = offset.unwrap_or(0);
let data = state.project.tasks.read_with(|tasks| { let filtered: Vec<serde_json::Value> = state.project.tasks.read_with(|tasks| {
let matching: Vec<_> = tasks tasks
.iter() .iter()
.filter(|t| { .filter(|t| {
let status_match = match status.as_deref() { let status_match = match status.as_deref() {
@@ -273,17 +273,17 @@ impl McpTool for TasksHandler {
}; };
status_match && branch_match status_match && branch_match
}) })
.collect();
let mut filtered: Vec<_> = matching
.into_iter()
.skip(offset) .skip(offset)
.map(|t| match level { .map(|t| match level.as_str() {
"compact" => serde_json::json!({ "id": t.id, "title": t.title, "status": t.status }), "compact" => serde_json::json!({ "id": t.id, "title": t.title, "status": t.status }),
"full" => serde_json::to_value(t).unwrap_or_default(), "full" => serde_json::to_value(t).unwrap_or_default(),
_ => serde_json::json!({ "id": t.id, "title": t.title, "status": t.status, "description": t.description, "git_branch": t.git_branch }), _ => serde_json::json!({ "id": t.id, "title": t.title, "status": t.status, "description": t.description, "git_branch": t.git_branch }),
}) })
.collect(); .collect()
});
let data = tokio::task::spawn_blocking(move || {
let mut filtered = filtered;
let mut json_str = serde_yaml::to_string(&filtered)?; let mut json_str = serde_yaml::to_string(&filtered)?;
if let Some(max_t) = max_tokens { if let Some(max_t) = max_tokens {
let char_limit = max_t * 4; let char_limit = max_t * 4;
@@ -293,7 +293,8 @@ impl McpTool for TasksHandler {
} }
} }
Ok::<String, crate::error::AppError>(json_str) Ok::<String, crate::error::AppError>(json_str)
})?; }).await.map_err(|e| crate::error::AppError::Internal(format!("Task serialization thread panicked: {}", e)))??;
Ok(data) Ok(data)
} }
TasksTool::SetCriteria { id, criteria: criteria_list } => { TasksTool::SetCriteria { id, criteria: criteria_list } => {