@@ -51,6 +51,8 @@ pub(crate) struct SearchIndexStats {
5151struct IndexKey {
5252 root : PathBuf ,
5353 host : PathBuf ,
54+ /// The remote session the index reads through; `None` for this machine.
55+ session : Option < String > ,
5456 hidden_directories : Vec < String > ,
5557 hidden_file_patterns : Vec < String > ,
5658 names_only : bool ,
@@ -70,13 +72,29 @@ type IndexSlot = Arc<std::sync::Mutex<Option<SharedIndex>>>;
7072static INDEX_CACHE : OnceLock < std:: sync:: Mutex < HashMap < IndexKey , IndexSlot > > > = OnceLock :: new ( ) ;
7173
7274fn cache ( ) -> & ' static std:: sync:: Mutex < HashMap < IndexKey , IndexSlot > > {
73- INDEX_CACHE . get_or_init ( || std:: sync:: Mutex :: new ( HashMap :: new ( ) ) )
75+ INDEX_CACHE . get_or_init ( || {
76+ // Every session ends with a new id, so without this a process that
77+ // reconnects or reopens remote workspaces keeps one content index and
78+ // one name index per session until it exits (1lck/Lithe-IDEA#2277).
79+ workspace:: sessions:: on_session_end ( release_session) ;
80+ std:: sync:: Mutex :: new ( HashMap :: new ( ) )
81+ } )
82+ }
83+
84+ /// Drops every index built through `session`, including in-flight builds,
85+ /// which can then no longer publish into the cache.
86+ fn release_session ( session : & str ) {
87+ let Ok ( mut indexes) = cache ( ) . lock ( ) else {
88+ return ;
89+ } ;
90+ indexes. retain ( |key, _| key. session . as_deref ( ) != Some ( session) ) ;
7491}
7592
7693fn key ( root : & Path , rules : & VisibilityRules ) -> IndexKey {
7794 IndexKey {
7895 root : root. to_path_buf ( ) ,
7996 host : workspace:: identity_key ( root) ,
97+ session : workspace:: current ( ) . map ( |host| host. session_id ( ) . to_owned ( ) ) ,
8098 hidden_directories : rules. hidden_directories . clone ( ) ,
8199 hidden_file_patterns : rules. hidden_file_patterns . clone ( ) ,
82100 names_only : false ,
@@ -120,10 +138,11 @@ fn get_index(
120138 rules : & VisibilityRules ,
121139 names_only : bool ,
122140) -> Result < SharedIndex , CoreError > {
141+ let mut cache_key = key ( root, rules) ;
142+ cache_key. names_only = names_only;
143+ let session = cache_key. session . clone ( ) ;
123144 let slot = {
124145 let mut indexes = cache ( ) . lock ( ) . map_err ( |_| index_lock_error ( ) ) ?;
125- let mut cache_key = key ( root, rules) ;
126- cache_key. names_only = names_only;
127146 // Keep one visibility variant per workspace/source, including in-flight builds.
128147 indexes. retain ( |existing, _| {
129148 existing. host != cache_key. host
@@ -136,6 +155,12 @@ fn get_index(
136155 . or_insert_with ( || Arc :: new ( std:: sync:: Mutex :: new ( None ) ) ) ,
137156 )
138157 } ;
158+ // The session may have ended while this request still held its host.
159+ // Checking after the insert closes the gap: either the check sees the
160+ // session gone, or its end hook runs after the insert and removes it.
161+ if let Some ( session) = session. filter ( |session| !workspace:: sessions:: is_registered ( session) ) {
162+ release_session ( & session) ;
163+ }
139164 let mut index = slot. lock ( ) . map_err ( |_| index_lock_error ( ) ) ?;
140165 if let Some ( index) = index. as_ref ( ) {
141166 return Ok ( Arc :: clone ( index) ) ;
@@ -151,6 +176,18 @@ fn index_lock_error() -> CoreError {
151176 CoreError :: new ( ErrorCode :: Unknown , "Search index lock was poisoned" )
152177}
153178
179+ /// Counts cached indexes, built or still building, that read through
180+ /// `session`, so lithe-core's tests can prove that ending a session releases them.
181+ #[ cfg( feature = "test-support" ) ]
182+ pub fn cached_indexes_for_session ( session : & str ) -> usize {
183+ cache ( ) . lock ( ) . map_or ( 0 , |indexes| {
184+ indexes
185+ . keys ( )
186+ . filter ( |key| key. session . as_deref ( ) == Some ( session) )
187+ . count ( )
188+ } )
189+ }
190+
154191pub ( crate ) fn invalidate_root ( root : & Path ) {
155192 let Ok ( mut indexes) = cache ( ) . lock ( ) else {
156193 return ;
0 commit comments