yo icon indicating copy to clipboard operation
yo copied to clipboard

Provide a reconnect option to `yo ssh`?

Open darrenkenny opened this issue 8 months ago • 4 comments

A common thing to do when working with VMs is to reboot them, it would be useful to have a reconnect option with yo ssh, to match the wait option, to be able to do something like:

yo ssh -w --reconnect myvm-1

or using shot name:

yo ssh -wr myvm-1

darrenkenny avatar Jun 12 '25 17:06 darrenkenny

I'm not sure how this would be implemented. How would yo know when to reconnect? By the exit status of ssh? Or by the runtime of the command? As far as I can tell, yo would need to distinguish between at least three cases:

  1. Successful ssh session which is terminated by the user intentionally, with no desire to reconnect. In fact, there are two possibilities even here: (a) the user exits via the exit command or ctrl-D, both of which result in ssh exiting with status 0. But case (b) is where the user intentionally exits via the escape sequence <RET>~., and in this case, ssh exits with status 255.
  2. Successful ssh session which is terminated due to a reboot, and the user would like ssh to reconnect. Since the connection typically drops here, I would assume ssh will terminate with status 255, but I have yet to test it.
  3. An ssh session which failed to connect, either due to a permission issue, or a connection issue. This would also result in a status 255, but you could detect it via the runtime (permission failures usually result in very brief, sub-second runtimes). However, some connection issues could be timeouts, so you can't rely on runtime to detect this case.

As a result, I'm not sure I can come up with an approach that could separate out the case (2), where we'd like to have a reconnect, from the other cases, where we definitely wouldn't want Yo to enter an infinite loop.

brenns10 avatar Jun 12 '25 17:06 brenns10

Almost certainly any reconnect would be entering a sleep pattern after the ssh connection drops - so it can always be interrupted by the user.

Personally, I don't see that as an issue in attempting to reconnect - as long as it isn't bombarding the server to try to reconnect, it seems reasonable.

The flow I'm be hoping to go with is quite simplistic, along the lines of:

  • attempt to connect, waiting if needed
    • connected, an in-use
    • on disconnect, if in reconnect mode
      • wait a short while (5 or 10 sec?)
      • loop back to attempt to connect

I don't believe it needs to be any more complicated than that.

darrenkenny avatar Jun 12 '25 18:06 darrenkenny

So the idea would be that if the user wants to exit, they ctrl-d (or whatever) and then Yo gives them a few seconds of grace period to interrupt before it reconnects?

That seems reasonable.

brenns10 avatar Jun 12 '25 18:06 brenns10

Well, I'd suggest Ctrl-C during the wait periods, but that's the general idea.

Given that it is an opt-in, i.e. not the default operation, it shouldn't be a surprise to people.

Maybe, it could be made more obvious to someone selecting it that they can Ctr-C while waiting, maybe via a message.

darrenkenny avatar Jun 12 '25 18:06 darrenkenny