Skip to content

Commit f201d1a

Browse files
committed
chore: improve logging for findSSHProcessID
1 parent 1e56a3f commit f201d1a

File tree

1 file changed

+77
-23
lines changed

1 file changed

+77
-23
lines changed

src/remote.ts

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -829,46 +829,100 @@ export class Remote {
829829
// findSSHProcessID returns the currently active SSH process ID that is
830830
// powering the remote SSH connection.
831831
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+
832838
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(/-> socksPort (\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(/-> socksPort (\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
851875
}
852-
const process = processes[0]
853-
return process.pid
854876
}
877+
855878
const start = Date.now()
879+
856880
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`)
858886
return undefined
859887
}
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+
860895
// Loop until we find the remote SSH log for this window.
861896
const filePath = await this.storage.getRemoteSSHLogPath()
897+
862898
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}`)
864912
}
913+
865914
// Then we search the remote SSH log until we find the port.
866915
const result = await search(filePath)
867916
if (!result) {
868-
return new Promise((resolve) => setTimeout(() => resolve(loop()), 500))
917+
return new Promise((resolve) => {
918+
setTimeout(() => resolve(loop()), 500)
919+
})
869920
}
921+
922+
logger.info(`SSH process ID search completed successfully after ${attempts} attempts, elapsed: ${elapsed}ms`)
870923
return result
871924
}
925+
872926
return loop()
873927
}
874928

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy