I develop a vscode extension with TypeScript,which provides a function for remote connection test. But occasionally this connection test command gets stuck and never returns。
My code is like this:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
export class ConnectService {
async testConnectionByCmd(instanceInfo: any) {
//some code to get connection info
……
// test connection by cmd
let cmd = `ssh -tt -o StrictHostKeyChecking=no -i ${IdentityFile} ${User}@${HostName} -p ${Port} echo $? "exit 0"`;
Logger.info('check connection cmd : ' + cmd);
return new Promise(async resolve => {
let stdout, stderr;
try {
({stdout, stderr} = await exec(cmd)); // hang at here sometime
Logger.info('exec(cmd).stdout: ' + stdout); // and this log is not printed
Logger.info('exec(cmd).stderr: ' + stderr);
} catch (e: any) {
({stdout, stderr} = e);
}
return resolve({stdout, stderr, HostName});
});
}
When the code is hang here, I open a terminal and execute the command in terminal, it returns normally and quickly, which means the network connection of the remote host is normal。 But the ts code still hangs here and never return.
What is the reason for this? Thank you!