1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
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;
4
10
use log:: { error, info, trace} ;
5
11
use pet:: resolve:: resolve_environment;
6
12
use pet_conda:: Conda ;
@@ -29,6 +35,7 @@ use serde_json::json;
29
35
use serde_json:: { self , Value } ;
30
36
use std:: collections:: BTreeMap ;
31
37
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
38
+ use std:: sync:: Mutex ;
32
39
use std:: time:: Duration ;
33
40
use std:: {
34
41
ops:: Deref ,
@@ -38,11 +45,10 @@ use std::{
38
45
time:: SystemTime ,
39
46
} ;
40
47
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
+ }
46
52
47
53
pub struct Context {
48
54
configuration : RwLock < Configuration > ,
@@ -154,6 +160,9 @@ pub fn handle_refresh(context: Arc<Context>, id: u32, params: Value) {
154
160
Ok ( refres_options) => {
155
161
// Start in a new thread, we can have multiple requests.
156
162
thread:: spawn ( move || {
163
+ // Ensure we can have only one refresh at a time.
164
+ let lock = REFRESH_LOCK . lock ( ) . unwrap ( ) ;
165
+
157
166
let config = context. configuration . read ( ) . unwrap ( ) . clone ( ) ;
158
167
let reporter = Arc :: new ( CacheReporter :: new ( Arc :: new ( jsonrpc:: create_reporter ( ) ) ) ) ;
159
168
@@ -233,6 +242,8 @@ pub fn handle_refresh(context: Arc<Context>, id: u32, params: Value) {
233
242
Some ( ( ) )
234
243
} ) ;
235
244
}
245
+
246
+ drop ( lock) ;
236
247
} ) ;
237
248
}
238
249
Err ( e) => {
0 commit comments