Skip to content

Commit b117938

Browse files
authored
There can be only one refresh request at a time (#116)
1 parent 7b20a20 commit b117938

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

crates/pet/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ clap = { version = "4.5.4", features = ["derive", "cargo"] }
3838
serde = { version = "1.0.152", features = ["derive"] }
3939
serde_json = "1.0.93"
4040
env_logger = "0.10.2"
41+
lazy_static = "1.4.0"
4142

4243
[dev-dependencies]
4344
regex = "1.10.4"
44-
lazy_static = "1.4.0"
4545

4646
[features]
4747
ci = []

crates/pet/src/jsonrpc.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
use crate::find::find_and_report_envs;
5+
use crate::find::find_python_environments_in_workspace_folder_recursive;
6+
use crate::find::identify_python_executables_using_locators;
7+
use crate::find::SearchScope;
8+
use crate::locators::create_locators;
9+
use lazy_static::lazy_static;
410
use log::{error, info, trace};
511
use pet::resolve::resolve_environment;
612
use pet_conda::Conda;
@@ -29,6 +35,7 @@ use serde_json::json;
2935
use serde_json::{self, Value};
3036
use std::collections::BTreeMap;
3137
use std::sync::atomic::{AtomicBool, Ordering};
38+
use std::sync::Mutex;
3239
use std::time::Duration;
3340
use std::{
3441
ops::Deref,
@@ -38,11 +45,10 @@ use std::{
3845
time::SystemTime,
3946
};
4047

41-
use crate::find::find_and_report_envs;
42-
use crate::find::find_python_environments_in_workspace_folder_recursive;
43-
use crate::find::identify_python_executables_using_locators;
44-
use crate::find::SearchScope;
45-
use crate::locators::create_locators;
48+
lazy_static! {
49+
/// Used to ensure we can have only one refreh at a time.
50+
static ref REFRESH_LOCK: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
51+
}
4652

4753
pub struct Context {
4854
configuration: RwLock<Configuration>,
@@ -154,6 +160,9 @@ pub fn handle_refresh(context: Arc<Context>, id: u32, params: Value) {
154160
Ok(refres_options) => {
155161
// Start in a new thread, we can have multiple requests.
156162
thread::spawn(move || {
163+
// Ensure we can have only one refresh at a time.
164+
let lock = REFRESH_LOCK.lock().unwrap();
165+
157166
let config = context.configuration.read().unwrap().clone();
158167
let reporter = Arc::new(CacheReporter::new(Arc::new(jsonrpc::create_reporter())));
159168

@@ -233,6 +242,8 @@ pub fn handle_refresh(context: Arc<Context>, id: u32, params: Value) {
233242
Some(())
234243
});
235244
}
245+
246+
drop(lock);
236247
});
237248
}
238249
Err(e) => {

0 commit comments

Comments
 (0)