fix: ensure ssh cleanup happens on cmd error

I noticed in my logs that sometimes `coder ssh` doesn't gracefully disconnect from the coordinator.

The cause is the `closerStack` construct we use in that function.  It has two paths to start closing things down:

1. explicit `close()` which we do in `defer`
2. context cancellation, which happens if the cli function returns an error

sometimes the ssh remote command returns an error, and this triggers context cancellation of the `closerStack`.  That is fine in and of itself, but we still want the explicit `close()` to wait until everything is closed before returning, since that's where we do cleanup, including the graceful disconnect.  Prior to this fix the `close()` just immediately exits if another goroutine is closing the stack.  Here we add a wait until everything is done.
This commit is contained in:
Spike Curtis
2024-03-07 17:26:49 +04:00
committed by GitHub
parent c8aa99a5b8
commit b96f6b48a4
2 changed files with 60 additions and 0 deletions
+4
View File
@@ -876,6 +876,7 @@ type closerStack struct {
closed bool
logger slog.Logger
err error
wg sync.WaitGroup
}
func newCloserStack(ctx context.Context, logger slog.Logger) *closerStack {
@@ -893,10 +894,13 @@ func (c *closerStack) close(err error) {
c.Lock()
if c.closed {
c.Unlock()
c.wg.Wait()
return
}
c.closed = true
c.err = err
c.wg.Add(1)
defer c.wg.Done()
c.Unlock()
for i := len(c.closers) - 1; i >= 0; i-- {