ssh icon indicating copy to clipboard operation
ssh copied to clipboard

fix: handle Close() before Serve()

Open kian99 opened this issue 8 months ago • 1 comments

This change prevents race conditions in programs that call the server's Close() or Shutdown() methods before Serve(). Consider a program that runs both Serve() and Close() in separate go routines, if a signal comes in early telling the program to shutdown, the Close() method may be called before Serve() has completed its initialisation and begun tracking the listener.

To fix this I've simply copied the latest approach from the Go standard library HTTP server and applied them. Since the SSH server was based on Go's stdlib implementation, bringing the latest stdlib changes was easy. For reference, we can see that in e.g. Go 1.10, there is very little difference between the two. Go 1.11 then removed the logic in trackListener that allowed the server to be reused and Go 1.20 made several changes like the removal of the doneChan in favour of an atomic boolean.

While technically the change/inability to reuse a previously closed SSH server is a breaking one, the same issue was raised for Go's stdlib here and here where it was fixed.

kian99 avatar Apr 16 '25 11:04 kian99

Hi @gustavosbarreto or @belak, would either of you be able to take a look at this?

kian99 avatar Apr 22 '25 12:04 kian99