fix(daemon): reown worktrees a killed container left root-owned

This commit is contained in:
sam committed 2026-10-11 04:01:19 +11:00
1 parent 43769e902b
commit 7ca1896b16
2 files changed
+88 -3

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
package container
import (
"context"
"fmt"
"strconv"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/mount"
"github.com/moby/moby/client"
)
// Reown hands dir back to uid:gid with a throwaway root container. A task
// container that was SIGKILLed never restores ownership of the files it
// created as root, and the non-root daemon has no privilege to do it itself.
func (p *Pool) Reown(ctx context.Context, dir string, uid, gid int) error {
cfg := p.current()
if err := p.ensureImage(ctx, cfg, cfg.Image); err != nil {
return err
}
resp, err := p.cli.ContainerCreate(ctx, client.ContainerCreateOptions{
Config: &container.Config{
Image: cfg.Image,
Entrypoint: []string{"chown", "-R", strconv.Itoa(uid) + ":" + strconv.Itoa(gid), "/w"},
Labels: map[string]string{"archie-daemon": "true"},
},
HostConfig: &container.HostConfig{
NetworkMode: "none",
Mounts: []mount.Mount{{Type: mount.TypeBind, Source: dir, Target: "/w"}},
},
})
if err != nil {
return fmt.Errorf("create reown container: %w", err)
}
defer func() {
_, _ = p.cli.ContainerRemove(context.WithoutCancel(ctx), resp.ID, client.ContainerRemoveOptions{Force: true})
}()
wait := p.cli.ContainerWait(ctx, resp.ID, client.ContainerWaitOptions{Condition: container.WaitConditionNextExit})
if _, err := p.cli.ContainerStart(ctx, resp.ID, client.ContainerStartOptions{}); err != nil {
return fmt.Errorf("start reown container: %w", err)
}
select {
case res := <-wait.Result:
if res.StatusCode != 0 {
return fmt.Errorf("reown %s: chown exited %d", dir, res.StatusCode)
}
return nil
case err := <-wait.Error:
return fmt.Errorf("wait for reown container: %w", err)
case <-ctx.Done():
return ctx.Err()
}
}
+35 -3
View File
@@ -1694,7 +1694,13 @@ func (d *Daemon) prepareWorkspace(ctx context.Context, task *workflow.Task, tree
target = worktree.Target(task.Branch)
}
// Every task gets an independent full clone.
dir, branch, err := trees.Prepare(ctx, task.Owner, task.Repo, repo.BaseBranch(), task.IssueNumber, task.Title, task.Body, task.Labels, target)
prepare := func() (string, string, error) {
return trees.Prepare(ctx, task.Owner, task.Repo, repo.BaseBranch(), task.IssueNumber, task.Title, task.Body, task.Labels, target)
}
dir, branch, err := prepare()
if d.reownDenied(ctx, trees.Dir(task.Owner, task.Repo, task.IssueNumber), err) {
dir, branch, err = prepare()
}
if err != nil {
reason := "worktree prepare failed: " + err.Error()
if target != worktree.Fresh {
@@ -1737,7 +1743,7 @@ func (d *Daemon) cleanupTerminalTaskWorktree(ctx context.Context, task *workflow
if d.worktreeHoldsUncapturedWork(cleanupCtx, trees, task) {
return
}
if err := trees.Cleanup(task.Owner, task.Repo, task.IssueNumber); err != nil {
if err := d.cleanupWorktree(cleanupCtx, trees, task.Owner, task.Repo, task.IssueNumber); err != nil {
d.Log.Warn("terminal worktree cleanup failed", "task", task.ID, "err", err)
}
}
@@ -2503,7 +2509,33 @@ func (d *Daemon) RemoveWorktree(owner, repo, identity string, issue int) error {
if trees == nil {
return nil
}
return trees.Cleanup(owner, repo, issue)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
return d.cleanupWorktree(ctx, trees, owner, repo, issue)
}
// cleanupWorktree removes a task's clone, first handing back any root-owned
// files a killed container left in it.
func (d *Daemon) cleanupWorktree(ctx context.Context, trees *worktree.Manager, owner, repo string, issue int) error {
err := trees.Cleanup(owner, repo, issue)
if d.reownDenied(ctx, trees.Dir(owner, repo, issue), err) {
return trees.Cleanup(owner, repo, issue)
}
return err
}
// reownDenied repairs dir when err says the daemon was refused access to it,
// and reports whether the caller should retry. A SIGKILLed task container
// leaves root-owned files that only a container can chown back.
func (d *Daemon) reownDenied(ctx context.Context, dir string, err error) bool {
if err == nil || !errors.Is(err, fs.ErrPermission) || d.ContainerPool == nil {
return false
}
if rerr := d.ContainerPool.Reown(ctx, dir, os.Getuid(), os.Getgid()); rerr != nil {
d.Log.Warn("worktree ownership repair failed", "dir", dir, "err", rerr)
return false
}
return true
}
// InFlight counts the same submitted work that graceful shutdown waits for,