@@ -50,9 +50,9 @@ public class ConsoleSessionManager
5050 private readonly Queue < string > _shuffledNames = new ( ) ;
5151
5252 /// <summary>
53- /// Mapping from pwsh PID to assigned name (for recycling names when console closes)
53+ /// Set of pwsh PIDs that have been assigned a console title
5454 /// </summary>
55- private readonly Dictionary < int , string > _pidToName = new ( ) ;
55+ private readonly HashSet < int > _titledPids = new ( ) ;
5656
5757 /// <summary>
5858 /// Path to the category lock file
@@ -203,7 +203,7 @@ public HashSet<int> ConsumeKnownBusyPids()
203203 }
204204
205205 /// <summary>
206- /// Clears a dead pipe from active pipe and busy tracking, and recycles the console name
206+ /// Clears a dead pipe from active pipe and busy tracking
207207 /// </summary>
208208 public void ClearDeadPipe ( string pipeName )
209209 {
@@ -217,14 +217,7 @@ public void ClearDeadPipe(string pipeName)
217217 if ( pid . HasValue )
218218 {
219219 _knownBusyPids . Remove ( pid . Value ) ;
220-
221- // Recycle the console name back to the queue
222- if ( _pidToName . TryGetValue ( pid . Value , out var name ) )
223- {
224- _pidToName . Remove ( pid . Value ) ;
225- _shuffledNames . Enqueue ( name ) ;
226- Console . Error . WriteLine ( $ "[INFO] ConsoleSessionManager: Recycled name '{ name } ' from dead pipe '{ pipeName } '") ;
227- }
220+ _titledPids . Remove ( pid . Value ) ;
228221 }
229222 Console . Error . WriteLine ( $ "[INFO] ConsoleSessionManager: Cleared dead pipe '{ pipeName } '") ;
230223 }
@@ -324,25 +317,21 @@ public IEnumerable<string> EnumerateUnownedPipes()
324317 }
325318
326319 /// <summary>
327- /// Assigns a console name to the specified pwsh PID. Returns the same name if already assigned.
328- /// When the console is closed, call ClearDeadPipe to recycle the name.
320+ /// Assigns a console name to the specified pwsh PID. Returns null if already assigned.
329321 /// </summary>
330322 /// <param name="pwshPid">The PID of the PowerShell process</param>
331- /// <returns>Console name in format "#pwshPid name"</returns>
332- public string AssignNameToPid ( int pwshPid )
323+ /// <returns>Console name in format "#pwshPid name", or null if already titled </returns>
324+ public string ? TryAssignNameToPid ( int pwshPid )
333325 {
334326 lock ( _lock )
335327 {
336- // Return existing name if already assigned
337- if ( _pidToName . TryGetValue ( pwshPid , out var existingName ) )
338- return $ "#{ pwshPid } { existingName } ";
328+ if ( ! _titledPids . Add ( pwshPid ) )
329+ return null ;
339330
340- // Get next name from shuffled queue
341331 if ( _shuffledNames . Count == 0 )
342332 RefillShuffledNames ( ) ;
343333
344334 var name = _shuffledNames . Dequeue ( ) ;
345- _pidToName [ pwshPid ] = name ;
346335 return $ "#{ pwshPid } { name } ";
347336 }
348337 }
0 commit comments