@@ -39,7 +39,7 @@ export interface SshProcessMonitorOptions {
3939 remoteSshExtensionId : string ;
4040}
4141
42- // Cleanup threshold for old network info files (1 hour)
42+ // Cleanup threshold for old network info files
4343const CLEANUP_MAX_AGE_MS = 60 * 60 * 1000 ;
4444
4545/**
@@ -81,11 +81,11 @@ export class SshProcessMonitor implements vscode.Disposable {
8181 maxAgeMs : number ,
8282 logger : Logger ,
8383 ) : Promise < void > {
84- const deletedFiles : string [ ] = [ ] ;
8584 try {
8685 const files = await fs . readdir ( networkInfoPath ) ;
8786 const now = Date . now ( ) ;
8887
88+ const deletedFiles : string [ ] = [ ] ;
8989 for ( const file of files ) {
9090 if ( ! file . endsWith ( ".json" ) ) {
9191 continue ;
@@ -104,14 +104,14 @@ export class SshProcessMonitor implements vscode.Disposable {
104104 // File may have been deleted by another process or doesn't exist, ignore
105105 }
106106 }
107- } catch {
108- // Directory may not exist yet, that's fine
109- }
110107
111- if ( deletedFiles . length > 0 ) {
112- logger . debug (
113- `Cleaned up ${ deletedFiles . length } old network info file(s): ${ deletedFiles . join ( ", " ) } ` ,
114- ) ;
108+ if ( deletedFiles . length > 0 ) {
109+ logger . debug (
110+ `Cleaned up ${ deletedFiles . length } old network info file(s): ${ deletedFiles . join ( ", " ) } ` ,
111+ ) ;
112+ }
113+ } catch {
114+ // Directory may not exist yet, ignore
115115 }
116116 }
117117
@@ -339,57 +339,58 @@ export class SshProcessMonitor implements vscode.Disposable {
339339
340340 /**
341341 * Monitors network info and updates the status bar.
342- * Checks file mtime to detect stale connections and trigger reconnection search.
343- * After consecutive failures to read the file, searches for a new process.
342+ * Searches for a new process if the file is stale or unreadable.
344343 */
345344 private async monitorNetwork ( ) : Promise < void > {
346345 const { networkInfoPath, networkPollInterval, logger } = this . options ;
347346 const staleThreshold = networkPollInterval * 5 ;
348347 const maxReadFailures = 5 ;
349- let consecutiveReadFailures = 0 ;
348+ let readFailures = 0 ;
350349
351350 while ( ! this . disposed && this . currentPid !== undefined ) {
352- const networkInfoFile = path . join (
353- networkInfoPath ,
354- `${ this . currentPid } .json` ,
355- ) ;
356-
357- let searchReason = "" ;
351+ const filePath = path . join ( networkInfoPath , `${ this . currentPid } .json` ) ;
352+ let search : { needed : true ; reason : string } | { needed : false } = {
353+ needed : false ,
354+ } ;
358355
359356 try {
360- const stats = await fs . stat ( networkInfoFile ) ;
357+ const stats = await fs . stat ( filePath ) ;
361358 const ageMs = Date . now ( ) - stats . mtime . getTime ( ) ;
362-
363- consecutiveReadFailures = 0 ;
359+ readFailures = 0 ;
364360
365361 if ( ageMs > staleThreshold ) {
366- searchReason = `Network info stale (${ Math . round ( ageMs / 1000 ) } s old)` ;
362+ search = {
363+ needed : true ,
364+ reason : `Network info stale (${ Math . round ( ageMs / 1000 ) } s old)` ,
365+ } ;
367366 } else {
368- const content = await fs . readFile ( networkInfoFile , "utf8" ) ;
367+ const content = await fs . readFile ( filePath , "utf8" ) ;
369368 const network = JSON . parse ( content ) as NetworkInfo ;
370369 const isStale = ageMs > networkPollInterval * 2 ;
371370 this . updateStatusBar ( network , isStale ) ;
372371 }
373372 } catch ( error ) {
374- consecutiveReadFailures ++ ;
373+ readFailures ++ ;
375374 logger . debug (
376- `Failed to read network info (attempt ${ consecutiveReadFailures } ): ${ ( error as Error ) . message } ` ,
375+ `Failed to read network info (attempt ${ readFailures } ): ${ ( error as Error ) . message } ` ,
377376 ) ;
378-
379- if ( consecutiveReadFailures >= maxReadFailures ) {
380- searchReason = `Network info missing for ${ consecutiveReadFailures } attempts` ;
377+ if ( readFailures >= maxReadFailures ) {
378+ search = {
379+ needed : true ,
380+ reason : `Network info missing for ${ readFailures } attempts` ,
381+ } ;
381382 }
382383 }
383384
384- if ( searchReason ) {
385- // Throttle: don't search too frequently
385+ // Search for new process if needed (with throttling)
386+ if ( search . needed ) {
386387 const timeSinceLastSearch = Date . now ( ) - this . lastStaleSearchTime ;
387388 if ( timeSinceLastSearch < staleThreshold ) {
388389 await this . delay ( staleThreshold - timeSinceLastSearch ) ;
389390 continue ;
390391 }
391392
392- logger . debug ( `${ searchReason } , searching for new SSH process` ) ;
393+ logger . debug ( `${ search . reason } , searching for new SSH process` ) ;
393394 // searchForProcess will update PID if a different process is found
394395 this . lastStaleSearchTime = Date . now ( ) ;
395396 await this . searchForProcess ( ) ;
0 commit comments