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
16 changes: 8 additions & 8 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "3"
members = ["crates/*", "xtask"]

[workspace.package]
version = "1.2.5"
version = "1.2.6"
edition = "2024"
license = "GPL-3.0-only"
publish = false
Expand Down
15 changes: 12 additions & 3 deletions crates/barsql-app/src/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,24 @@ impl Server {
}
}

async fn call(self: Arc<Self>, id: Value, params: Value, tab: String, cancelled: watch::Receiver<bool>) {
async fn call(self: Arc<Self>, id: Value, params: Value, tab: String, mut cancelled: watch::Receiver<bool>) {
let tool = params["name"].as_str().unwrap_or_default();
let args = &params["arguments"];
let started = Instant::now();
let result = if *cancelled.borrow() {
Err(CANCELLED.into())
} else {
let run = AssertUnwindSafe(self.run(tool, args, &tab, cancelled.clone()));
run.catch_unwind().await.unwrap_or_else(|panic| Err(failed(QueryError::from_panic(panic))))
let run = AssertUnwindSafe(self.run(tool, args, &tab, cancelled.clone()))
.catch_unwind()
.map(|result| result.unwrap_or_else(|panic| Err(failed(QueryError::from_panic(panic)))));
// Anything but a change, which rolls back by itself, stops at once, even before its query starts.
// Err is a newer call taking the id, not a cancel.
tokio::select! {
result = run => result,
Ok(_) = cancelled.wait_for(|stop| *stop), if tool != "run_change" => {
Err(failed(QueryError::cancelled()))
}
}
};
// Gives the connection back at once: SQLite has only one.
self.bar.cleanup_tab(&tab).await;
Expand Down
13 changes: 13 additions & 0 deletions crates/barsql-app/src/mcp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,19 @@ fn a_reused_request_id_leaves_the_newer_call_cancellable() {
assert!(s.settled(), "the cancel reached it");
}

// Holding the engines lock parks the call in reload_connections, before its query registers.
#[test]
fn a_cancel_before_the_query_registers_still_stops_it() {
let s = setup();
let engines = lock(&s.server.bar.inner.engines);
s.query(4, ENDLESS);
std::thread::sleep(Duration::from_millis(200));
s.cancel(4);
drop(engines);
assert!(s.settled(), "the cancel was lost");
assert!(s.replies.try_recv().is_err(), "a cancelled call answered");
}

#[test]
fn cancelling_a_change_takes_its_question_down_and_runs_nothing() {
let s = setup();
Expand Down
Loading