Skip to content

Commit 62d3265

Browse files
committed
fix nerdctl ps show nothing when timeout
1 parent d13fb45 commit 62d3265

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

cmd/nerdctl/compose/compose_ps.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"strings"
2323
"text/tabwriter"
24-
"time"
2524

2625
"github.com/spf13/cobra"
2726
"golang.org/x/sync/errgroup"
@@ -345,28 +344,30 @@ func formatPublishers(labelMap map[string]string) []PortPublisher {
345344

346345
// statusForFilter returns the status value to be matched with the 'status' filter
347346
func statusForFilter(ctx context.Context, c containerd.Container) string {
348-
// Just in case, there is something wrong in server.
349-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
350-
defer cancel()
351-
352-
task, err := c.Task(ctx, nil)
353-
if err != nil {
347+
var errorStatus error
348+
defer func() {
349+
if errorStatus != nil && ctx.Err() == context.DeadlineExceeded {
350+
log.L.WithError(errorStatus).Warnf("failed to get container %s status", c.ID())
351+
}
352+
}()
353+
task, errorStatus := c.Task(ctx, nil)
354+
if errorStatus != nil {
354355
// NOTE: NotFound doesn't mean that container hasn't started.
355356
// In docker/CRI-containerd plugin, the task will be deleted
356357
// when it exits. So, the status will be "created" for this
357358
// case.
358-
if errdefs.IsNotFound(err) {
359+
if errdefs.IsNotFound(errorStatus) {
359360
return string(containerd.Created)
360361
}
361362
return string(containerd.Unknown)
362363
}
363364

364-
status, err := task.Status(ctx)
365-
if err != nil {
365+
status, errorStatus := task.Status(ctx)
366+
if errorStatus != nil {
366367
return string(containerd.Unknown)
367368
}
368-
labels, err := c.Labels(ctx)
369-
if err != nil {
369+
labels, errorStatus := c.Labels(ctx)
370+
if errorStatus != nil {
370371
return string(containerd.Unknown)
371372
}
372373

pkg/containerutil/containerutil.go

-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ func PrintHostPort(ctx context.Context, writer io.Writer, container containerd.C
8686

8787
// ContainerStatus returns the container's status from its task.
8888
func ContainerStatus(ctx context.Context, c containerd.Container) (containerd.Status, error) {
89-
// Just in case, there is something wrong in server.
90-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
91-
defer cancel()
92-
9389
task, err := c.Task(ctx, nil)
9490
if err != nil {
9591
return containerd.Status{}, err

pkg/formatter/formatter.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,31 @@ import (
3939
)
4040

4141
func ContainerStatus(ctx context.Context, c containerd.Container) string {
42-
// Just in case, there is something wrong in server.
43-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
44-
defer cancel()
4542
titleCaser := cases.Title(language.English)
46-
47-
task, err := c.Task(ctx, nil)
48-
if err != nil {
43+
var errorStatus error
44+
defer func() {
45+
if errorStatus != nil && ctx.Err() == context.DeadlineExceeded {
46+
log.L.WithError(errorStatus).Warnf("failed to get container %s status", c.ID())
47+
}
48+
}()
49+
task, errorStatus := c.Task(ctx, nil)
50+
if errorStatus != nil {
4951
// NOTE: NotFound doesn't mean that container hasn't started.
5052
// In docker/CRI-containerd plugin, the task will be deleted
5153
// when it exits. So, the status will be "created" for this
5254
// case.
53-
if errdefs.IsNotFound(err) {
55+
if errdefs.IsNotFound(errorStatus) {
5456
return titleCaser.String(string(containerd.Created))
5557
}
5658
return titleCaser.String(string(containerd.Unknown))
5759
}
5860

59-
status, err := task.Status(ctx)
60-
if err != nil {
61+
status, errorStatus := task.Status(ctx)
62+
if errorStatus != nil {
6163
return titleCaser.String(string(containerd.Unknown))
6264
}
63-
labels, err := c.Labels(ctx)
64-
if err != nil {
65+
labels, errorStatus := c.Labels(ctx)
66+
if errorStatus != nil {
6567
return titleCaser.String(string(containerd.Unknown))
6668
}
6769

0 commit comments

Comments
 (0)