@@ -168,13 +168,15 @@ impl CondaLocator for Conda {
168168 self . managers . insert ( conda_dir. clone ( ) , manager. clone ( ) ) ;
169169
170170 // Also check for a mamba/micromamba manager in the same directory and report it.
171- if !self . mamba_managers . contains_key ( & conda_dir) {
172- if let Some ( mamba_mgr) = get_mamba_manager ( & conda_dir) {
173- self . mamba_managers
174- . insert ( conda_dir. clone ( ) , mamba_mgr. clone ( ) ) ;
175- reporter. report_manager ( & mamba_mgr. to_manager ( ) ) ;
176- }
177- }
171+ let _ = self
172+ . mamba_managers
173+ . get_or_insert_with ( conda_dir. clone ( ) , || {
174+ let mgr = get_mamba_manager ( & conda_dir) ;
175+ if let Some ( ref m) = mgr {
176+ reporter. report_manager ( & m. to_manager ( ) ) ;
177+ }
178+ mgr
179+ } ) ;
178180
179181 // Find all the environments in the conda install folder. (under `envs` folder)
180182 for conda_env in
@@ -349,14 +351,16 @@ impl Locator for Conda {
349351 reporter. report_environment ( & env) ;
350352
351353 // Also check for a mamba/micromamba manager in the same directory and report it.
352- let is_new_mamba = !self . mamba_managers . contains_key ( conda_dir) ;
353- if let Some ( mamba_mgr) = self . mamba_managers . get_or_insert_with ( conda_dir. clone ( ) , || {
354- get_mamba_manager ( conda_dir)
355- } ) {
356- if is_new_mamba {
357- reporter. report_manager ( & mamba_mgr. to_manager ( ) ) ;
354+ // Reporting inside the closure minimizes the TOCTOU window compared to a
355+ // separate contains_key check, though concurrent threads may still
356+ // briefly both invoke the closure before the write-lock double-check.
357+ let _ = self . mamba_managers . get_or_insert_with ( conda_dir. clone ( ) , || {
358+ let mgr = get_mamba_manager ( conda_dir) ;
359+ if let Some ( ref m) = mgr {
360+ reporter. report_manager ( & m. to_manager ( ) ) ;
358361 }
359- }
362+ mgr
363+ } ) ;
360364 } else {
361365 // We will still return the conda env even though we do not have the manager.
362366 // This might seem incorrect, however the tool is about discovering environments.
0 commit comments