@@ -829,46 +829,100 @@ export class Remote {
829
829
// findSSHProcessID returns the currently active SSH process ID that is
830
830
// powering the remote SSH connection.
831
831
private async findSSHProcessID ( timeout = 15000 ) : Promise < number | undefined > {
832
+ const logger = getMemoryLogger ( )
833
+ logger . info ( `Starting SSH process ID search with timeout: ${ timeout } ms` )
834
+
835
+ let attempts = 0
836
+ let lastFilePath : string | undefined
837
+
832
838
const search = async ( logPath : string ) : Promise < number | undefined > => {
833
- // This searches for the socksPort that Remote SSH is connecting to. We do
834
- // this to find the SSH process that is powering this connection. That SSH
835
- // process will be logging network information periodically to a file.
836
- const text = await fs . readFile ( logPath , "utf8" )
837
- const matches = text . match ( / - > s o c k s P o r t ( \d + ) - > / )
838
- if ( ! matches ) {
839
- return
840
- }
841
- if ( matches . length < 2 ) {
842
- return
843
- }
844
- const port = Number . parseInt ( matches [ 1 ] )
845
- if ( ! port ) {
846
- return
847
- }
848
- const processes = await find ( "port" , port )
849
- if ( processes . length < 1 ) {
850
- return
839
+ try {
840
+ // This searches for the socksPort that Remote SSH is connecting to. We do
841
+ // this to find the SSH process that is powering this connection. That SSH
842
+ // process will be logging network information periodically to a file.
843
+ const text = await fs . readFile ( logPath , "utf8" )
844
+
845
+ if ( attempts % 5 === 0 ) {
846
+ logger . debug ( `SSH log file size: ${ text . length } bytes` )
847
+ }
848
+
849
+ const matches = text . match ( / - > s o c k s P o r t ( \d + ) - > / )
850
+ if ( ! matches ) {
851
+ return
852
+ }
853
+ if ( matches . length < 2 ) {
854
+ return
855
+ }
856
+ const port = Number . parseInt ( matches [ 1 ] )
857
+ if ( ! port ) {
858
+ return
859
+ }
860
+
861
+ logger . info ( `Found SSH socks port: ${ port } , searching for process` )
862
+ const processes = await find ( "port" , port )
863
+
864
+ if ( processes . length < 1 ) {
865
+ logger . debug ( `No processes found using port: ${ port } ` )
866
+ return
867
+ }
868
+
869
+ const process = processes [ 0 ]
870
+ logger . info ( `Found SSH process: PID=${ process . pid } , CMD=${ process . cmd } ` )
871
+ return process . pid
872
+ } catch ( error ) {
873
+ logger . error ( `Error searching for SSH process in log: ${ logPath } ` , error )
874
+ return undefined
851
875
}
852
- const process = processes [ 0 ]
853
- return process . pid
854
876
}
877
+
855
878
const start = Date . now ( )
879
+
856
880
const loop = async ( ) : Promise < number | undefined > => {
857
- if ( Date . now ( ) - start > timeout ) {
881
+ attempts ++
882
+
883
+ const elapsed = Date . now ( ) - start
884
+ if ( elapsed > timeout ) {
885
+ logger . info ( `SSH process ID search timed out after ${ attempts } attempts, elapsed: ${ elapsed } ms` )
858
886
return undefined
859
887
}
888
+
889
+ // Log progress periodically
890
+ if ( attempts % 5 === 0 ) {
891
+ logger . info ( `SSH process ID search attempt #${ attempts } , elapsed: ${ elapsed } ms` )
892
+ logger . logMemoryUsage ( "SSH_PROCESS_SEARCH" )
893
+ }
894
+
860
895
// Loop until we find the remote SSH log for this window.
861
896
const filePath = await this . storage . getRemoteSSHLogPath ( )
897
+
862
898
if ( ! filePath ) {
863
- return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( loop ( ) ) , 500 ) )
899
+ if ( lastFilePath !== filePath ) {
900
+ lastFilePath = filePath
901
+ logger . debug ( `SSH log file not found, will retry` )
902
+ }
903
+
904
+ return new Promise ( ( resolve ) => {
905
+ setTimeout ( ( ) => resolve ( loop ( ) ) , 500 )
906
+ } )
907
+ }
908
+
909
+ if ( lastFilePath !== filePath ) {
910
+ lastFilePath = filePath
911
+ logger . info ( `Found SSH log file: ${ filePath } ` )
864
912
}
913
+
865
914
// Then we search the remote SSH log until we find the port.
866
915
const result = await search ( filePath )
867
916
if ( ! result ) {
868
- return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( loop ( ) ) , 500 ) )
917
+ return new Promise ( ( resolve ) => {
918
+ setTimeout ( ( ) => resolve ( loop ( ) ) , 500 )
919
+ } )
869
920
}
921
+
922
+ logger . info ( `SSH process ID search completed successfully after ${ attempts } attempts, elapsed: ${ elapsed } ms` )
870
923
return result
871
924
}
925
+
872
926
return loop ( )
873
927
}
874
928
0 commit comments