- Notifications
You must be signed in to change notification settings - Fork1.1k
fix: close SSH sessions bottom-up if top-down fails#14678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
72 changes: 60 additions & 12 deletionscli/ssh.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -37,6 +37,7 @@ import ( | ||
| "github.com/coder/coder/v2/codersdk/workspacesdk" | ||
| "github.com/coder/coder/v2/cryptorand" | ||
| "github.com/coder/coder/v2/pty" | ||
| "github.com/coder/quartz" | ||
| "github.com/coder/retry" | ||
| "github.com/coder/serpent" | ||
| ) | ||
| @@ -48,6 +49,8 @@ const ( | ||
| var ( | ||
| workspacePollInterval = time.Minute | ||
| autostopNotifyCountdown = []time.Duration{30 * time.Minute} | ||
| // gracefulShutdownTimeout is the timeout, per item in the stack of things to close | ||
| gracefulShutdownTimeout = 2 * time.Second | ||
| ) | ||
| func (r *RootCmd) ssh() *serpent.Command { | ||
| @@ -153,7 +156,7 @@ func (r *RootCmd) ssh() *serpent.Command { | ||
| // log HTTP requests | ||
| client.SetLogger(logger) | ||
| } | ||
| stack := newCloserStack(ctx, logger, quartz.NewReal()) | ||
| defer stack.close(nil) | ||
| for _, remoteForward := range remoteForwards { | ||
| @@ -936,11 +939,18 @@ type closerStack struct { | ||
| closed bool | ||
| logger slog.Logger | ||
| err error | ||
| allDone chan struct{} | ||
| // for testing | ||
| clock quartz.Clock | ||
| } | ||
| func newCloserStack(ctx context.Context, logger slog.Logger, clock quartz.Clock) *closerStack { | ||
| cs := &closerStack{ | ||
| logger: logger, | ||
| allDone: make(chan struct{}), | ||
| clock: clock, | ||
| } | ||
| go cs.closeAfterContext(ctx) | ||
| return cs | ||
| } | ||
| @@ -954,20 +964,58 @@ func (c *closerStack) close(err error) { | ||
| c.Lock() | ||
| if c.closed { | ||
| c.Unlock() | ||
| <-c.allDone | ||
| return | ||
| } | ||
| c.closed = true | ||
| c.err = err | ||
| c.Unlock() | ||
| defer close(c.allDone) | ||
| if len(c.closers) == 0 { | ||
| return | ||
| } | ||
| // We are going to work down the stack in order. If things close quickly, we trigger the | ||
| // closers serially, in order. `done` is a channel that indicates the nth closer is done | ||
| // closing, and we should trigger the (n-1) closer. However, if things take too long we don't | ||
| // want to wait, so we also start a ticker that works down the stack and sends on `done` as | ||
| // well. | ||
| next := len(c.closers) - 1 | ||
| // here we make the buffer 2x the number of closers because we could write once for it being | ||
| // actually done and once via the countdown for each closer | ||
| done := make(chan int, len(c.closers)*2) | ||
| startNext := func() { | ||
| go func(i int) { | ||
| defer func() { done <- i }() | ||
| cwn := c.closers[i] | ||
| cErr := cwn.closer.Close() | ||
| c.logger.Debug(context.Background(), | ||
| "closed item from stack", slog.F("name", cwn.name), slog.Error(cErr)) | ||
| }(next) | ||
| next-- | ||
| } | ||
| done <- len(c.closers) // kick us off right away | ||
| // start a ticking countdown in case we hang/don't close quickly | ||
| countdown := len(c.closers) - 1 | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| c.clock.TickerFunc(ctx, gracefulShutdownTimeout, func() error { | ||
| if countdown < 0 { | ||
| return nil | ||
| } | ||
| done <- countdown | ||
| countdown-- | ||
| return nil | ||
| }, "closerStack") | ||
| for n := range done { // the nth closer is done | ||
| if n == 0 { | ||
| return | ||
| } | ||
| if n-1 == next { | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| startNext() | ||
| } | ||
| } | ||
| } | ||
103 changes: 86 additions & 17 deletionscli/ssh_internal_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.