diff --git a/crates/pet/Cargo.toml b/crates/pet/Cargo.toml index d114ab86..b8e8bfa1 100644 --- a/crates/pet/Cargo.toml +++ b/crates/pet/Cargo.toml @@ -38,10 +38,10 @@ clap = { version = "4.5.4", features = ["derive", "cargo"] } serde = { version = "1.0.152", features = ["derive"] } serde_json = "1.0.93" env_logger = "0.10.2" +lazy_static = "1.4.0" [dev-dependencies] regex = "1.10.4" -lazy_static = "1.4.0" [features] ci = [] diff --git a/crates/pet/src/jsonrpc.rs b/crates/pet/src/jsonrpc.rs index 121076ad..851c30da 100644 --- a/crates/pet/src/jsonrpc.rs +++ b/crates/pet/src/jsonrpc.rs @@ -1,6 +1,12 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +use crate::find::find_and_report_envs; +use crate::find::find_python_environments_in_workspace_folder_recursive; +use crate::find::identify_python_executables_using_locators; +use crate::find::SearchScope; +use crate::locators::create_locators; +use lazy_static::lazy_static; use log::{error, info, trace}; use pet::resolve::resolve_environment; use pet_conda::Conda; @@ -29,6 +35,7 @@ use serde_json::json; use serde_json::{self, Value}; use std::collections::BTreeMap; use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Mutex; use std::time::Duration; use std::{ ops::Deref, @@ -38,11 +45,10 @@ use std::{ time::SystemTime, }; -use crate::find::find_and_report_envs; -use crate::find::find_python_environments_in_workspace_folder_recursive; -use crate::find::identify_python_executables_using_locators; -use crate::find::SearchScope; -use crate::locators::create_locators; +lazy_static! { + /// Used to ensure we can have only one refreh at a time. + static ref REFRESH_LOCK: Arc> = Arc::new(Mutex::new(())); +} pub struct Context { configuration: RwLock, @@ -154,6 +160,9 @@ pub fn handle_refresh(context: Arc, id: u32, params: Value) { Ok(refres_options) => { // Start in a new thread, we can have multiple requests. thread::spawn(move || { + // Ensure we can have only one refresh at a time. + let lock = REFRESH_LOCK.lock().unwrap(); + let config = context.configuration.read().unwrap().clone(); let reporter = Arc::new(CacheReporter::new(Arc::new(jsonrpc::create_reporter()))); @@ -233,6 +242,8 @@ pub fn handle_refresh(context: Arc, id: u32, params: Value) { Some(()) }); } + + drop(lock); }); } Err(e) => {