fix(container): keep agent output out of the exit reason

This commit is contained in:
sam committed 2026-10-24 02:28:01 +11:00
1 parent 2847c684b0
commit 61cbfd8271
1 file changed
+9 -26
+9 -26
View File
@@ -15,11 +15,9 @@ import (
"maps"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/moby/moby/api/pkg/stdcopy"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/registry"
"github.com/moby/moby/client"
@@ -41,9 +39,8 @@ type Container struct {
// not start returns nil, which never closes.
func (c *Container) Exited() <-chan struct{} { return c.exited }
// ExitDetail says how the container ended: its exit code and the last lines it
// wrote. It is empty until Exited closes, and for a container the pool did not
// start.
// ExitDetail says how the container ended: its exit code. It is empty until
// Exited closes, and for a container the pool did not start.
func (c *Container) ExitDetail() string {
if c.detail == nil {
return ""
@@ -377,7 +374,7 @@ func (p *Pool) watchExit(ctx context.Context, id string, detail *string) <-chan
go func() {
select {
case result := <-wait.Result:
*detail = p.exitDetail(context.WithoutCancel(ctx), id, result.StatusCode)
*detail = exitDetail(result.StatusCode)
close(exited)
case <-wait.Error:
}
@@ -385,28 +382,14 @@ func (p *Pool) watchExit(ctx context.Context, id string, detail *string) <-chan
return exited
}
// exitDetail describes a finished container for the operator. The container is
// auto-removed, so its output is read best effort: a lost race leaves the code.
func (p *Pool) exitDetail(ctx context.Context, id string, code int64) string {
detail := fmt.Sprintf("exit code %d", code)
// exitDetail describes a finished container for the operator. Its output is
// left out: the reason reaches the dashboard, logs and notifications, and an
// agent's last lines can carry a credential.
func exitDetail(code int64) string {
if code == 137 {
detail += " (killed; often out of memory)"
return "exit code 137 (killed; often out of memory)"
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
logs, err := p.cli.ContainerLogs(ctx, id, client.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Tail: "5"})
if err != nil {
return detail
}
defer logs.Close()
var out strings.Builder
if _, err := stdcopy.StdCopy(&out, &out, io.LimitReader(logs, 4096)); err != nil && out.Len() == 0 {
return detail
}
if tail := strings.TrimSpace(out.String()); tail != "" {
detail += "; last output: " + strings.Join(strings.Fields(strings.ReplaceAll(tail, "\n", " | ")), " ")
}
return detail
return fmt.Sprintf("exit code %d", code)
}
// containerTeardown is a live container's max-uptime timer and whether its