ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

Early close from the ssh server cause the connection to end without error nor ready event

Open Congelli501 opened this issue 5 years ago • 0 comments

I used this simple code to establish an ssh connection:

ssh.connect(sshOptions);
await new Promise((resolve, reject) => {
	ssh.on('ready', resolve);
	ssh.on('error', reject);
});

I expect the connection to either be ready or fail with an error.

But, if the server close the connection after the greeting message (server ssh version) due to beeing too busy, then the connection end without any ready nor error event.

This code helped me catch this edge case & make sure my promise resolves:

ssh.connect(sshOptions);
await new Promise((resolve, reject) => {
	const onEarlyEnd = reject.bind(null, new Error('SSH early close / end'));
	ssh.on('ready', () => {
		ssh.off('close', onEarlyEnd);
		ssh.off('end', onEarlyEnd);
		resolve();
	});
	ssh.on('error', reject);
	ssh.on('close', onEarlyEnd);
	ssh.on('end', onEarlyEnd);
});

To prevent this tricky issue from happening to other users, could you make sure a ssh.connect will either result in a ready or an error event? If an end event occurs before the connection is ready, then an error event should be emitted.

Congelli501 avatar Sep 08 '20 16:09 Congelli501