Skip to content

Commit 23a6833

Browse files
committed
Fix another race
1 parent a1b68e5 commit 23a6833

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

cli/ssh.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
117117
// the logger is created but before any goroutines or wind-down
118118
// defers (e.g. context cancels) are declared.
119119
var wg sync.WaitGroup
120-
defer func() {
121-
cancel()
122-
wg.Wait()
123-
}()
120+
defer wg.Wait()
124121

125122
workspace, workspaceAgent, err := getWorkspaceAndAgent(ctx, inv, client, codersdk.Me, inv.Args[0])
126123
if err != nil {
@@ -192,7 +189,15 @@ func (r *RootCmd) ssh() *clibase.Cmd {
192189
wg.Add(1)
193190
go func() {
194191
defer wg.Done()
195-
watchAndClose(ctx, rawSSH.Close, logger, client, workspace)
192+
watchAndClose(ctx, func() error {
193+
rawSSH.Close()
194+
// If we don't close Stdin, the io.Copy below may
195+
// block indefinitely on Stdin Read.
196+
if rc, ok := inv.Stdin.(io.Closer); ok {
197+
rc.Close()
198+
}
199+
return nil
200+
}, logger, client, workspace)
196201
}()
197202

198203
wg.Add(1)

0 commit comments

Comments
 (0)