@@ -221,30 +221,18 @@ func ssh() *cobra.Command {
221
221
}
222
222
}
223
223
224
- // Wait for the context to be canceled, or the SSH session to end.
225
- sshErr := make (chan error )
226
- go func () {
227
- defer close (sshErr )
228
-
229
- err = sshSession .Wait ()
230
- if err != nil {
231
- // If the connection drops unexpectedly, we get an ExitMissingError but no other
232
- // error details, so try to at least give the user a better message
233
- if errors .Is (err , & gossh.ExitMissingError {}) {
234
- sshErr <- xerrors .New ("SSH connection ended unexpectedly" )
235
- return
236
- }
237
- sshErr <- err
224
+ err = sshSession .Wait ()
225
+ if err != nil {
226
+ // If the connection drops unexpectedly, we get an
227
+ // ExitMissingError but no other error details, so try to at
228
+ // least give the user a better message
229
+ if errors .Is (err , & gossh.ExitMissingError {}) {
230
+ return xerrors .New ("SSH connection ended unexpectedly" )
238
231
}
239
- }()
240
-
241
- select {
242
- case <- ctx .Done ():
243
- _ = sshSession .Close ()
244
- return ctx .Err ()
245
- case err := <- sshErr :
246
232
return err
247
233
}
234
+
235
+ return nil
248
236
},
249
237
}
250
238
cliflag .BoolVarP (cmd .Flags (), & stdio , "stdio" , "" , "CODER_SSH_STDIO" , false , "Specifies whether to emit SSH output over stdin/stdout." )
@@ -456,7 +444,12 @@ func uploadGPGKeys(ctx context.Context, sshClient *gossh.Client) error {
456
444
// Check if the agent is running in the workspace already.
457
445
// Note: we don't support windows in the workspace for GPG forwarding so
458
446
// using shell commands is fine.
459
- agentSocketBytes , err := runRemoteSSH (sshClient , nil , "set -eux; agent_socket=$(gpgconf --list-dir agent-socket); echo $agent_socket; test ! -S $agent_socket" )
447
+ agentSocketBytes , err := runRemoteSSH (sshClient , nil , `
448
+ set -eux
449
+ agent_socket=$(gpgconf --list-dir agent-socket)
450
+ echo "$agent_socket"
451
+ test ! -S "$agent_socket"
452
+ ` )
460
453
agentSocket := strings .TrimSpace (string (agentSocketBytes ))
461
454
if err != nil {
462
455
return xerrors .Errorf ("check if agent socket is running (check if %q exists): %w" , agentSocket , err )
@@ -540,24 +533,30 @@ func sshForwardRemote(ctx context.Context, stderr io.Writer, sshClient *gossh.Cl
540
533
return
541
534
}
542
535
543
- localConn , err := net .Dial (localAddr .Network (), localAddr .String ())
544
- if err != nil {
545
- _ , _ = fmt .Fprintf (stderr , "Dial local address %s: %+v\n " , localAddr .String (), err )
546
- _ = remoteConn .Close ()
547
- continue
548
- }
536
+ go func () {
537
+ defer func () {
538
+ _ = remoteConn .Close ()
539
+ }()
549
540
550
- if c , ok := localAddr .(cookieAddr ); ok {
551
- _ , err = localConn .Write (c .cookie )
541
+ localConn , err := net .Dial (localAddr .Network (), localAddr .String ())
552
542
if err != nil {
553
- _ , _ = fmt .Fprintf (stderr , "Write cookie to local connection: %+v\n " , err )
543
+ _ , _ = fmt .Fprintf (stderr , "Dial local address %s: %+v\n " , localAddr .String (), err )
544
+ return
545
+ }
546
+ defer func () {
554
547
_ = localConn .Close ()
555
- _ = remoteConn .Close ()
556
- continue
548
+ }()
549
+
550
+ if c , ok := localAddr .(cookieAddr ); ok {
551
+ _ , err = localConn .Write (c .cookie )
552
+ if err != nil {
553
+ _ , _ = fmt .Fprintf (stderr , "Write cookie to local connection: %+v\n " , err )
554
+ return
555
+ }
557
556
}
558
- }
559
557
560
- go agent .Bicopy (ctx , localConn , remoteConn )
558
+ agent .Bicopy (ctx , localConn , remoteConn )
559
+ }()
561
560
}
562
561
}()
563
562
0 commit comments