mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
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:
@@ -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-- {
|
||||
|
||||
Reference in New Issue
Block a user