@@ -21,22 +21,25 @@ import (
21
21
"time"
22
22
23
23
"github.com/armon/circbuf"
24
+ "github.com/gliderlabs/ssh"
24
25
"github.com/google/uuid"
25
-
26
+ "github.com/pkg/sftp"
26
27
"go.uber.org/atomic"
28
+ gossh "golang.org/x/crypto/ssh"
29
+ "golang.org/x/xerrors"
27
30
28
31
"cdr.dev/slog"
29
32
"github.com/coder/coder/agent/usershell"
30
33
"github.com/coder/coder/peer"
31
34
"github.com/coder/coder/peerbroker"
32
35
"github.com/coder/coder/pty"
33
36
"github.com/coder/retry"
37
+ )
34
38
35
- "github.com/pkg/sftp"
36
-
37
- "github.com/gliderlabs/ssh"
38
- gossh "golang.org/x/crypto/ssh"
39
- "golang.org/x/xerrors"
39
+ const (
40
+ ProtocolReconnectingPTY = "reconnecting-pty"
41
+ ProtocolSSH = "ssh"
42
+ ProtocolDial = "dial"
40
43
)
41
44
42
45
type Options struct {
@@ -174,17 +177,25 @@ func (*agent) runStartupScript(ctx context.Context, script string) error {
174
177
defer func () {
175
178
_ = writer .Close ()
176
179
}()
180
+
177
181
caller := "-c"
178
182
if runtime .GOOS == "windows" {
179
183
caller = "/c"
180
184
}
185
+
181
186
cmd := exec .CommandContext (ctx , shell , caller , script )
182
187
cmd .Stdout = writer
183
188
cmd .Stderr = writer
184
189
err = cmd .Run ()
185
190
if err != nil {
191
+ // cmd.Run does not return a context canceled error, it returns "signal: killed".
192
+ if ctx .Err () != nil {
193
+ return ctx .Err ()
194
+ }
195
+
186
196
return xerrors .Errorf ("run: %w" , err )
187
197
}
198
+
188
199
return nil
189
200
}
190
201
@@ -208,11 +219,11 @@ func (a *agent) handlePeerConn(ctx context.Context, conn *peer.Conn) {
208
219
}
209
220
210
221
switch channel .Protocol () {
211
- case "ssh" :
222
+ case ProtocolSSH :
212
223
go a .sshServer .HandleConn (channel .NetConn ())
213
- case "reconnecting-pty" :
224
+ case ProtocolReconnectingPTY :
214
225
go a .handleReconnectingPTY (ctx , channel .Label (), channel .NetConn ())
215
- case "dial" :
226
+ case ProtocolDial :
216
227
go a .handleDial (ctx , channel .Label (), channel .NetConn ())
217
228
default :
218
229
a .logger .Warn (ctx , "unhandled protocol from channel" ,
@@ -478,8 +489,8 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne
478
489
a .logger .Warn (ctx , "start reconnecting pty command" , slog .F ("id" , id ))
479
490
}
480
491
481
- // Default to buffer 64KB .
482
- circularBuffer , err := circbuf .NewBuffer (64 * 1024 )
492
+ // Default to buffer 64KiB .
493
+ circularBuffer , err := circbuf .NewBuffer (64 << 10 )
483
494
if err != nil {
484
495
a .logger .Warn (ctx , "create circular buffer" , slog .Error (err ))
485
496
return
0 commit comments