Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f6a29e

Browse files
committed
address comments
1 parent c9d3b2c commit 6f6a29e

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

agent/agentcontainers/containers_dockercli.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -311,31 +311,31 @@ func (dcli *dockerCLI) List(ctx context.Context) (codersdk.WorkspaceAgentListCon
311311
// container IDs and returns the parsed output.
312312
// The stderr output is also returned for logging purposes.
313313
func runDockerInspect(ctx context.Context, execer agentexec.Execer, ids ...string) (stdout, stderr []byte, err error) {
314-
select {
315-
case <-ctx.Done():
314+
if ctx.Err() != nil {
316315
// If the context is done, we don't want to run the command.
317316
return []byte{}, []byte{}, ctx.Err()
318-
default:
319-
var stdoutBuf, stderrBuf bytes.Buffer
320-
cmd := execer.CommandContext(ctx, "docker", append([]string{"inspect"}, ids...)...)
321-
cmd.Stdout = &stdoutBuf
322-
cmd.Stderr = &stderrBuf
323-
err = cmd.Run()
324-
stdout = bytes.TrimSpace(stdoutBuf.Bytes())
325-
stderr = bytes.TrimSpace(stderrBuf.Bytes())
326-
if err != nil {
327-
if ctx.Err() != nil {
328-
// If the context was canceled, we don't want to return an error.
329-
return stdout, stderr, ctx.Err()
330-
}
331-
if bytes.Contains(stderr, []byte("No such object:")) {
332-
// This can happen if a container is deleted between the time we check for its existence and the time we inspect it.
333-
return stdout, stderr, nil
334-
}
335-
return stdout, stderr, err
317+
}
318+
var stdoutBuf, stderrBuf bytes.Buffer
319+
cmd := execer.CommandContext(ctx, "docker", append([]string{"inspect"}, ids...)...)
320+
cmd.Stdout = &stdoutBuf
321+
cmd.Stderr = &stderrBuf
322+
err = cmd.Run()
323+
stdout = bytes.TrimSpace(stdoutBuf.Bytes())
324+
stderr = bytes.TrimSpace(stderrBuf.Bytes())
325+
if err != nil {
326+
if ctx.Err() != nil {
327+
// If the context was canceled while running the command,
328+
// return the context error instead of the command error,
329+
// which is likely to be "signal: killed".
330+
return stdout, stderr, ctx.Err()
331+
}
332+
if bytes.Contains(stderr, []byte("No such object:")) {
333+
// This can happen if a container is deleted between the time we check for its existence and the time we inspect it.
334+
return stdout, stderr, nil
336335
}
337-
return stdout, stderr, nil
336+
return stdout, stderr, err
338337
}
338+
return stdout, stderr, nil
339339
}
340340

341341
// To avoid a direct dependency on the Docker API, we use the docker CLI

0 commit comments

Comments
 (0)