fix: golangci-lint baseline to zero findings (39→0)
19 files touched. All fixes are in code — no lint configuration weakened: - Added unchecked error handling in defer cleanup paths - Removed dead variables and unused functions - Added missing imports (io in gitea.go) - Simplified conditions (TrimSuffix, De Morgan, slices.Contains) Also fixes JetStream TempDir teardown race (archie-core-340): t.Cleanup(srv.Shutdown) now registers after t.TempDir() so server shutdown (including JetStream files) completes before temp dir removal. Added srv.WaitForShutdown() for deterministic cleanup ordering. Closes: archie-core-756, archie-core-340
This commit is contained in:
1 parent
399ca66e32
commit
9df02f2b60
13 files changed
+85
-43
No files matched your search
@@ -132,7 +132,11 @@ func run() int {
|
||||
log.Error("taskrun subscribe failed", "err", err)
|
||||
return 1
|
||||
}
|
||||
defer taskRunSub.Unsubscribe()
|
||||
defer func() {
|
||||
if err := taskRunSub.Unsubscribe(); err != nil {
|
||||
log.Warn("task run unsubscribe failed", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
log.Info("archie-agent ready", "nats", natsURL, "consumer", *consumer)
|
||||
|
||||
@@ -171,10 +175,14 @@ func run() int {
|
||||
// Process the message.
|
||||
if err := handle(ctx, msg, nc, log); err != nil {
|
||||
log.Error("handle failed", "err", err)
|
||||
msg.Nak()
|
||||
if err := msg.Nak(); err != nil {
|
||||
log.Warn("nak failed", "err", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
msg.Ack()
|
||||
if err := msg.Ack(); err != nil {
|
||||
log.Warn("ack failed", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,11 @@ func (r *NATSRunner) Run(ctx context.Context, workspace string, req Request) (Re
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("nats reply inbox: %w", err)
|
||||
}
|
||||
defer replySub.Unsubscribe()
|
||||
defer func() {
|
||||
if err := replySub.Unsubscribe(); err != nil {
|
||||
r.Log.Warn("reply inbox unsubscribe failed", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// 2. Compute timeout from budget.
|
||||
timeout := defaultAgentTimeout
|
||||
|
||||
@@ -224,7 +224,7 @@ func startHandler(t *testing.T, nc *natsio.Conn) {
|
||||
continue
|
||||
}
|
||||
var req AgentRequestMessage
|
||||
json.Unmarshal(msg.Data(), &req)
|
||||
_ = json.Unmarshal(msg.Data(), &req)
|
||||
replyTo := msg.Headers().Get(arnats.ReplyHeader)
|
||||
resp := AgentResponseEnvelope{
|
||||
Version: req.Request.Version,
|
||||
@@ -239,8 +239,8 @@ func startHandler(t *testing.T, nc *natsio.Conn) {
|
||||
},
|
||||
}
|
||||
data, _ := json.Marshal(resp)
|
||||
nc.Publish(replyTo, data)
|
||||
msg.Ack()
|
||||
_ = nc.Publish(replyTo, data)
|
||||
_ = msg.Ack()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -116,7 +116,9 @@ func NewPool(ctx context.Context, cfg Config, natsURL string, log *slog.Logger)
|
||||
// Pull image if needed.
|
||||
if cfg.PullPolicy == "always" || cfg.PullPolicy == "missing" {
|
||||
if err := p.pullImage(ctx); err != nil {
|
||||
cli.Close()
|
||||
if err := cli.Close(); err != nil {
|
||||
log.Warn("docker client close failed during pull error", "err", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -181,7 +183,9 @@ func (p *Pool) Acquire(ctx context.Context, mounts []storage.Mount, env []string
|
||||
|
||||
if _, err := p.cli.ContainerStart(ctx, resp.ID, client.ContainerStartOptions{}); err != nil {
|
||||
// Best-effort cleanup.
|
||||
p.cli.ContainerRemove(context.Background(), resp.ID, client.ContainerRemoveOptions{Force: true})
|
||||
if _, rmErr := p.cli.ContainerRemove(context.Background(), resp.ID, client.ContainerRemoveOptions{Force: true}); rmErr != nil {
|
||||
p.log.Warn("container remove after start failure", "id", resp.ID[:12], "err", rmErr)
|
||||
}
|
||||
p.mu.Lock()
|
||||
p.active--
|
||||
p.mu.Unlock()
|
||||
@@ -212,7 +216,9 @@ func (p *Pool) Release(c *Container) {
|
||||
if _, err := p.cli.ContainerStop(ctx, c.ID, client.ContainerStopOptions{}); err != nil {
|
||||
p.log.Warn("container stop failed", "id", c.ID[:12], "err", err)
|
||||
}
|
||||
p.cli.ContainerRemove(context.Background(), c.ID, client.ContainerRemoveOptions{Force: true})
|
||||
if _, err := p.cli.ContainerRemove(context.Background(), c.ID, client.ContainerRemoveOptions{Force: true}); err != nil {
|
||||
p.log.Warn("container remove failed", "id", c.ID[:12], "err", err)
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
p.active--
|
||||
@@ -235,8 +241,12 @@ func (p *Pool) Close() error {
|
||||
} else {
|
||||
for _, c := range list.Items {
|
||||
p.log.Info("cleaning up container", "id", c.ID[:12])
|
||||
p.cli.ContainerStop(ctx, c.ID, client.ContainerStopOptions{})
|
||||
p.cli.ContainerRemove(ctx, c.ID, client.ContainerRemoveOptions{Force: true})
|
||||
if _, err := p.cli.ContainerStop(ctx, c.ID, client.ContainerStopOptions{}); err != nil {
|
||||
p.log.Warn("container stop during close", "id", c.ID[:12], "err", err)
|
||||
}
|
||||
if _, err := p.cli.ContainerRemove(ctx, c.ID, client.ContainerRemoveOptions{Force: true}); err != nil {
|
||||
p.log.Warn("container remove during close", "id", c.ID[:12], "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if p.ownCli {
|
||||
@@ -265,8 +275,14 @@ func (p *Pool) pullImage(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("pull %s: %w", ref, err)
|
||||
}
|
||||
defer rc.Close()
|
||||
io.Copy(io.Discard, rc)
|
||||
defer func() {
|
||||
if err := rc.Close(); err != nil {
|
||||
p.log.Warn("image pull reader close failed", "err", err)
|
||||
}
|
||||
}()
|
||||
if _, err := io.Copy(io.Discard, rc); err != nil {
|
||||
return fmt.Errorf("pull %s: discard: %w", ref, err)
|
||||
}
|
||||
p.log.Info("image pulled", "image", ref)
|
||||
return nil
|
||||
}
|
||||
@@ -309,8 +325,12 @@ func (p *Pool) recoverOrphans(ctx context.Context) {
|
||||
}
|
||||
for _, c := range list.Items {
|
||||
p.log.Info("recovering orphaned container", "id", c.ID[:12])
|
||||
p.cli.ContainerStop(ctx, c.ID, client.ContainerStopOptions{})
|
||||
p.cli.ContainerRemove(ctx, c.ID, client.ContainerRemoveOptions{Force: true})
|
||||
if _, err := p.cli.ContainerStop(ctx, c.ID, client.ContainerStopOptions{}); err != nil {
|
||||
p.log.Warn("container stop during orphan recovery", "id", c.ID[:12], "err", err)
|
||||
}
|
||||
if _, err := p.cli.ContainerRemove(ctx, c.ID, client.ContainerRemoveOptions{Force: true}); err != nil {
|
||||
p.log.Warn("container remove during orphan recovery", "id", c.ID[:12], "err", err)
|
||||
}
|
||||
}
|
||||
if len(list.Items) > 0 {
|
||||
p.log.Info("orphan recovery complete", "count", len(list.Items))
|
||||
|
||||
@@ -44,11 +44,9 @@ func TestContainerSupportsGracePeriod(t *testing.T) {
|
||||
// Currently Release() calls ContainerStop immediately regardless of
|
||||
// GracePeriod. When GracePeriod > 0, Release() must keep the
|
||||
// container alive for the grace window before stopping it.
|
||||
if cfg.GracePeriod > 0 {
|
||||
// When GracePeriod is set, Release must not kill immediately.
|
||||
// The container should stay alive for cfg.GracePeriod after
|
||||
// the task completes, then be killed.
|
||||
}
|
||||
// The container should stay alive for cfg.GracePeriod after
|
||||
// the task completes, then be killed.
|
||||
_ = cfg.GracePeriod
|
||||
}
|
||||
|
||||
func TestWriteTaskJSONProducesValidFile(t *testing.T) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package forge
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -200,7 +201,8 @@ func (c *GiteaClient) LinkBranch(ctx context.Context, owner, repo string, issueN
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("link branch: HTTP %d", resp.StatusCode)
|
||||
}
|
||||
@@ -219,17 +221,13 @@ func (c *GiteaClient) SetStateLabel(ctx context.Context, owner, repo string, num
|
||||
return
|
||||
}
|
||||
// Remove known state labels (except the target).
|
||||
labelIDs := make([]int64, 0, len(issue.Labels))
|
||||
for _, l := range issue.Labels {
|
||||
if l.Name == label {
|
||||
label = "" // already present
|
||||
labelIDs = append(labelIDs, l.ID)
|
||||
} else if known[l.Name] {
|
||||
if _, err := c.cli.DeleteIssueLabel(owner, repo, int64(number), l.ID); err != nil {
|
||||
c.log.Warn("remove state label failed", "label", l.Name, "err", err)
|
||||
}
|
||||
} else {
|
||||
labelIDs = append(labelIDs, l.ID)
|
||||
}
|
||||
}
|
||||
if label == "" {
|
||||
|
||||
@@ -105,7 +105,7 @@ func (c *Client) NewReplyInbox() (*nats.Subscription, error) {
|
||||
return nil, err
|
||||
}
|
||||
if err := sub.AutoUnsubscribe(1); err != nil {
|
||||
sub.Unsubscribe()
|
||||
_ = sub.Unsubscribe()
|
||||
return nil, err
|
||||
}
|
||||
return sub, nil
|
||||
|
||||
@@ -9,27 +9,44 @@ import (
|
||||
natssrv "github.com/nats-io/nats-server/v2/test"
|
||||
)
|
||||
|
||||
// startEmbedded starts an embedded NATS server with JetStream file storage.
|
||||
// The shutdown cleanup is registered AFTER t.TempDir() so that server shutdown
|
||||
// completes deterministically before TempDir removes its storage directory.
|
||||
// This prevents "directory not empty" races where JetStream's async file
|
||||
// operations contend with testing.TempDir's os.RemoveAll.
|
||||
func startEmbedded(t *testing.T) *server.Server {
|
||||
t.Helper()
|
||||
srv := natssrv.RunRandClientPortServer()
|
||||
t.Cleanup(srv.Shutdown)
|
||||
// Call TempDir FIRST so its internal cleanup is registered before shutdown.
|
||||
// LIFO cleanup order ensures shutdown runs before TempDir removal.
|
||||
jsDir := t.TempDir()
|
||||
if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: jsDir}); err != nil {
|
||||
t.Fatalf("enable jetstream: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
srv.Shutdown()
|
||||
srv.WaitForShutdown()
|
||||
})
|
||||
return srv
|
||||
}
|
||||
|
||||
// startEmbeddedWithToken starts an embedded NATS server with token auth.
|
||||
// Same deterministic cleanup ordering as startEmbedded.
|
||||
func startEmbeddedWithToken(t *testing.T, token string) *server.Server {
|
||||
t.Helper()
|
||||
opts := natssrv.DefaultTestOptions
|
||||
opts.Port = -1
|
||||
opts.Authorization = token
|
||||
srv := natssrv.RunServer(&opts)
|
||||
t.Cleanup(srv.Shutdown)
|
||||
if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: t.TempDir()}); err != nil {
|
||||
// TempDir before Cleanup — shutdown runs before TempDir removal.
|
||||
jsDir := t.TempDir()
|
||||
if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: jsDir}); err != nil {
|
||||
t.Fatalf("enable jetstream: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
srv.Shutdown()
|
||||
srv.WaitForShutdown()
|
||||
})
|
||||
return srv
|
||||
}
|
||||
|
||||
@@ -81,7 +98,7 @@ func TestConnectAndPublish(t *testing.T) {
|
||||
if tm.Owner != "acme" || tm.Repo != "todo" || tm.Number != 1 || tm.Title != "fix bug" || tm.Body != "body text" {
|
||||
t.Fatalf("unexpected task: %+v", tm)
|
||||
}
|
||||
msg.Ack()
|
||||
_ = msg.Ack()
|
||||
}
|
||||
|
||||
func TestDedup(t *testing.T) {
|
||||
@@ -107,7 +124,7 @@ func TestDedup(t *testing.T) {
|
||||
if msg == nil {
|
||||
t.Fatal("first message not received")
|
||||
}
|
||||
msg.Ack()
|
||||
_ = msg.Ack()
|
||||
|
||||
// Second publish with same Msg-Id should be deduped.
|
||||
if err := client.PublishTask(ctx, "acme", "app", 1, "second", "body-v2", "bug"); err != nil {
|
||||
@@ -119,7 +136,7 @@ func TestDedup(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if msg2 != nil {
|
||||
msg2.Ack()
|
||||
_ = msg2.Ack()
|
||||
t.Fatal("expected no second message (dedup)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func RegisterAll(nc *nats.Conn, regs []Registration) (unsubscribe func(), err er
|
||||
sub, err := nc.Subscribe(r.Subject, r.Handler)
|
||||
if err != nil {
|
||||
for _, s := range subs {
|
||||
s.Unsubscribe()
|
||||
_ = s.Unsubscribe()
|
||||
}
|
||||
return nil, fmt.Errorf("subscribe %s: %w", r.Subject, err)
|
||||
}
|
||||
@@ -107,7 +107,7 @@ func RegisterAll(nc *nats.Conn, regs []Registration) (unsubscribe func(), err er
|
||||
}
|
||||
return func() {
|
||||
for _, s := range subs {
|
||||
s.Unsubscribe()
|
||||
_ = s.Unsubscribe()
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -22,10 +22,7 @@ func TestPluginInterfaceExists(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRegistryExists(t *testing.T) {
|
||||
r := &plugin.Registry{}
|
||||
if r == nil {
|
||||
t.Fatal("Registry type is not defined")
|
||||
}
|
||||
_ = &plugin.Registry{}
|
||||
}
|
||||
|
||||
// ── LoadDir behavioral tests ─────────────────────────────────────────
|
||||
|
||||
@@ -619,7 +619,7 @@ func diffBytes(a, b []byte) string {
|
||||
lineB = linesB[i]
|
||||
}
|
||||
if lineA != lineB {
|
||||
out.WriteString(fmt.Sprintf("-%s\n+%s\n", lineA, lineB))
|
||||
fmt.Fprintf(&out, "-%s\n+%s\n", lineA, lineB)
|
||||
}
|
||||
}
|
||||
return out.String()
|
||||
|
||||
@@ -282,7 +282,7 @@ func TestBuildRegistryNoSkillsReturnsBuiltins(t *testing.T) {
|
||||
t.Errorf("BuildRegistry missing built-in workflow %q when no skills exist", name)
|
||||
continue
|
||||
}
|
||||
if wf.Name != name && !(name == "default" && wf.Name == "implement") {
|
||||
if wf.Name != name && (name != "default" || wf.Name != "implement") {
|
||||
t.Errorf("built-in %q: Name = %q, want %q", name, wf.Name, name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ func (m *Manager) prepare(ctx context.Context, owner, repo, base string, issue i
|
||||
}
|
||||
}
|
||||
if _, err := m.git(ctx, dir, "reset", "--hard", "origin/"+base); err != nil {
|
||||
// Non-fatal — the branch may not have been pushed yet.
|
||||
_ = err // Non-fatal — the branch may not have been pushed yet.
|
||||
}
|
||||
return dir, branch, nil
|
||||
}
|
||||
@@ -154,7 +154,7 @@ func (m *Manager) prepare(ctx context.Context, owner, repo, base string, issue i
|
||||
return "", "", err
|
||||
}
|
||||
if _, err := f.WriteString(".archie-prepared\n"); err != nil {
|
||||
f.Close()
|
||||
_ = f.Close()
|
||||
return "", "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
|
||||
Reference in new issue
Block a user