Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent-tools): async test guards so locks do not span awaits

thinhlpg7e482ec3584b
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs
index d5560f6eb..31fe39ce7 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs
@@ -2563,12 +2563,14 @@ mod tests {
let d = crate::tools::gate::resolve_decision(
lookup("write").unwrap(),
&json!({"path": "/tmp/x.txt", "content": "y"}),
- &root,
- Some(&scratch),
- &[],
+ &crate::tools::gate::GateContext {
+ project_root: &root,
+ scratch: Some(&scratch),
+ read_roots: &[],
+ hide_jan: true,
+ },
&crate::permissions::ToolPermissions::default(),
&crate::tools::gate::SessionGrants::default(),
- true,
);
assert_eq!(
d,
@@ -2592,12 +2594,14 @@ mod tests {
let d = crate::tools::gate::resolve_decision(
lookup("write").unwrap(),
&json!({"path": "/tmp/esc/x.txt", "content": "y"}),
- &root,
- Some(&scratch),
- &[],
+ &crate::tools::gate::GateContext {
+ project_root: &root,
+ scratch: Some(&scratch),
+ read_roots: &[],
+ hide_jan: true,
+ },
&crate::permissions::ToolPermissions::default(),
&crate::tools::gate::SessionGrants::default(),
- true,
);
assert_eq!(
d,
@@ -3487,8 +3491,10 @@ mod tests {
// ---- screenshot ---------------------------------------------------------
/// Two headless Chromes racing for the same profile dir collide, so the
- /// tests that actually launch one are serialised.
- static CHROME_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
+ /// tests that actually launch one are serialised. An async mutex so the test
+ /// can hold it across the awaited screenshot without `await_holding_lock`.
+ static CHROME_TEST_LOCK: std::sync::LazyLock<tokio::sync::Mutex<()>> =
+ std::sync::LazyLock::new(|| tokio::sync::Mutex::new(()));
#[tokio::test]
async fn screenshot_rejects_a_non_html_file() {
@@ -3542,7 +3548,7 @@ mod tests {
if chrome_binary().is_none() {
return;
}
- let _guard = CHROME_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
+ let _guard = CHROME_TEST_LOCK.lock().await;
let root = unique_root();
std::fs::write(
root.join("page.html"),
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs
index 36075ae33..0736d2c51 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs
@@ -464,15 +464,16 @@ pub fn workspace_filename(name: &str) -> Result<String, String> {
/// the sweep deletes the directory the other test is mid-way through using. Any
/// test that either sweeps or depends on a live scratch takes this first.
#[cfg(test)]
-pub(crate) static SCRATCH_NAMESPACE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
+pub(crate) static SCRATCH_NAMESPACE_LOCK: std::sync::LazyLock<tokio::sync::Mutex<()>> =
+ std::sync::LazyLock::new(|| tokio::sync::Mutex::new(()));
-/// Take [`SCRATCH_NAMESPACE_LOCK`], ignoring poisoning: a panic in one test must
-/// not cascade into unrelated failures in every other one.
+/// Take [`SCRATCH_NAMESPACE_LOCK`], awaiting the mutex like the tests that use
+/// it. The guard is held across the async body on purpose: these tests serialise
+/// against the sweep that otherwise races them, and [`tokio::sync::Mutex`] drops
+/// the guard if a task is cancelled, so it cannot deadlock.
#[cfg(test)]
-pub(crate) fn lock_scratch_namespace() -> std::sync::MutexGuard<'static, ()> {
- SCRATCH_NAMESPACE_LOCK
- .lock()
- .unwrap_or_else(|e| e.into_inner())
+pub(crate) async fn lock_scratch_namespace() -> tokio::sync::MutexGuard<'static, ()> {
+ SCRATCH_NAMESPACE_LOCK.lock().await
}
#[cfg(test)]
@@ -547,7 +548,7 @@ mod tests {
#[allow(clippy::await_holding_lock)]
async fn stale_scratch_dirs_are_swept_and_fresh_ones_are_not() {
let session = format!("sweep-stale-{}", std::process::id());
- let _guard = lock_scratch_namespace();
+ let _guard = lock_scratch_namespace().await;
let orphan = ensure_scratch_dir(&session).await.unwrap();
std::fs::write(orphan.join("spill.txt"), b"leaked").unwrap();
let bystander = std::env::temp_dir().join(format!("not-a-scratch-{session}"));