fix(sandbox): reclassify exec races with the idle sweep as NotFound so rebinding heals (#3980)

An exec that collided with the idle sweeper's delete (#3942 mechanism A)
surfaced as a non-zero exit with an empty stderr, which the filesystem op
wrappers classified as InvalidRequest — outside CanReplaceRemoteBinding's
NotFound/Terminal allow-list, so the rebinding self-heal never ran and the
failure reached the user (empty answer, zero steps). The op wrappers now
re-inspect on failure and reclassify as NotFound when the Engine confirms
the container is gone, making the sweeper's 'deleting needs no coordination
with the binding store' premise hold for the mid-flight window too. Only a
confirmed 404 reclassifies; transport errors keep the original kind.

The shared message now carries the exit code — with an empty stderr it was
the one missing clue during the original diagnosis (#3942).

Co-authored-by: vicfei <vicfei@users.noreply.github.com>
main
Fei.Chen 2026-10-08 01:28:53 +08:00 committed by GitHub
parent b8d83eab1c
commit dad81cb8c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 88 additions and 17 deletions

View File

@ -892,7 +892,7 @@ func (c *DockerRemoteClient) WriteFile(
return dockerError("WriteFile", err)
}
if result.ExitCode != 0 {
return dockerFileOpError("WriteFile", clean, result.Stderr)
return c.dockerFileOpFailed(ctx, id, "WriteFile", clean, result.Stderr, result.ExitCode)
}
return nil
}
@ -927,7 +927,7 @@ func (c *DockerRemoteClient) ReadFile(
return nil, dockerError("ReadFile", err)
}
if result.ExitCode != 0 {
return nil, dockerFileOpError("ReadFile", clean, result.Stderr)
return nil, c.dockerFileOpFailed(ctx, id, "ReadFile", clean, result.Stderr, result.ExitCode)
}
return []byte(result.Stdout), nil
}
@ -971,7 +971,7 @@ func (c *DockerRemoteClient) Stat(
return nil, dockerError("Stat", err)
}
if result.ExitCode != 0 {
return nil, dockerFileOpError("Stat", clean, result.Stderr)
return nil, c.dockerFileOpFailed(ctx, id, "Stat", clean, result.Stderr, result.ExitCode)
}
entries := parseDockerFindOutput(result.Stdout)
if len(entries) == 0 {
@ -994,7 +994,18 @@ func (c *DockerRemoteClient) Stat(
// NotFound so callers can treat it as "nothing there"; everything else,
// permission denials included, is an invalid request carrying the tool's own
// complaint rather than a synthesised one.
func dockerFileOpError(op, clean, stderr string) error {
// dockerFileOpFailed classifies a filesystem op's non-zero exec exit. The
// message carries the exit code — with an empty stderr it used to be the
// only missing clue (issue #3942). When the exec raced the idle sweeper's
// delete (ensureRunning's inspect passes, the sweep removes the container,
// the exec dies mid-flight), the failure surfaces as exit != 0 with an
// empty stderr; the re-inspect reclassifies it as NotFound so
// CanReplaceRemoteBinding lets the rebinding self-heal take over — making
// the sweeper's "deleting needs no coordination with the binding store"
// premise hold for this window too (issue #3942, mechanism A).
func (c *DockerRemoteClient) dockerFileOpFailed(
ctx context.Context, id, op, clean, stderr string, exitCode int,
) error {
if strings.Contains(stderr, "No such file or directory") {
return &RemoteError{
Kind: RemoteErrorKindNotFound,
@ -1003,14 +1014,32 @@ func dockerFileOpError(op, clean, stderr string) error {
Message: clean + " does not exist",
}
}
if c.containerVanished(ctx, id, op) {
return &RemoteError{
Kind: RemoteErrorKindNotFound,
Provider: SandboxTypeDocker,
Op: op,
Message: fmt.Sprintf("%s %s: exit=%d: container vanished mid-op (likely idle sweep)",
op, clean, exitCode),
}
}
return &RemoteError{
Kind: RemoteErrorKindInvalidRequest,
Provider: SandboxTypeDocker,
Op: op,
Message: fmt.Sprintf("%s %s: %s", op, clean, firstNonEmptyLine(stderr)),
Message: fmt.Sprintf("%s %s: exit=%d: %s", op, clean, exitCode, firstNonEmptyLine(stderr)),
}
}
// containerVanished reports whether the Engine confirms the container is
// gone (404). A transport or daemon error returns false — only a confirmed
// disappearance may reclassify, keeping unknown errors on the safe side of
// CanReplaceRemoteBinding's allow-list.
func (c *DockerRemoteClient) containerVanished(ctx context.Context, id, op string) bool {
_, err := c.api.ContainerInspect(ctx, id, client.ContainerInspectOptions{})
return err != nil && IsRemoteNotFound(dockerError(op, err))
}
// MakeDir creates a directory (and its parents) inside the sandbox.
func (c *DockerRemoteClient) MakeDir(
ctx context.Context,
@ -1039,12 +1068,7 @@ func (c *DockerRemoteClient) makeDir(ctx context.Context, id, dir, op string) er
return dockerError(op, err)
}
if result.ExitCode != 0 {
return &RemoteError{
Kind: RemoteErrorKindInvalidRequest,
Provider: SandboxTypeDocker,
Op: op,
Message: fmt.Sprintf("mkdir -p %s: %s", dir, firstNonEmptyLine(result.Stderr)),
}
return c.dockerFileOpFailed(ctx, id, op, dir, result.Stderr, result.ExitCode)
}
return nil
}
@ -1076,12 +1100,7 @@ func (c *DockerRemoteClient) Remove(
return dockerError("Remove", err)
}
if result.ExitCode != 0 {
return &RemoteError{
Kind: RemoteErrorKindInvalidRequest,
Provider: SandboxTypeDocker,
Op: "Remove",
Message: fmt.Sprintf("rm -rf %s: %s", clean, firstNonEmptyLine(result.Stderr)),
}
return c.dockerFileOpFailed(ctx, id, "Remove", clean, result.Stderr, result.ExitCode)
}
return nil
}
@ -1125,6 +1144,15 @@ func (c *DockerRemoteClient) ListDir(
Message: clean + " does not exist",
}
}
if c.containerVanished(ctx, id, "ListDir") {
return nil, &RemoteError{
Kind: RemoteErrorKindNotFound,
Provider: SandboxTypeDocker,
Op: "ListDir",
Message: fmt.Sprintf("ListDir %s: exit=%d: container vanished mid-op (likely idle sweep)",
clean, result.ExitCode),
}
}
return nil, &RemoteError{
Kind: RemoteErrorKindInternal,
Provider: SandboxTypeDocker,

View File

@ -935,6 +935,12 @@ func TestDockerClientReadFileMapsFailures(t *testing.T) {
engine := newFakeDockerEngine()
engine.execExit = 1
engine.execStderr = tt.stderr
// The container is alive: these are genuine command failures,
// not the mid-op disappearance the vanish recheck covers.
engine.inspect["c"] = container.InspectResponse{
ID: "c",
State: &container.State{Status: "running"},
}
docker := newTestDockerClient(t, engine)
_, err := docker.ReadFile(context.Background(), testHandle("c"),
@ -1216,3 +1222,40 @@ func TestDockerExecObservesBothOutputStreamsWithoutChangingResult(t *testing.T)
require.Equal(t, result.Stdout, observed["stdout"])
require.Equal(t, result.Stderr, observed["stderr"])
}
// The idle sweeper can delete a container between ensureRunning's inspect
// gate and the exec's completion (#3942 mechanism A): the exec dies with a
// non-zero exit and an empty stderr. The filesystem ops must reclassify
// that collision as NotFound so CanReplaceRemoteBinding lets the rebinding
// self-heal run instead of surfacing InvalidRequest to the user.
func TestFileOpAgainstVanishedContainerRebinds(t *testing.T) {
engine := newFakeDockerEngine()
engine.execExit = 1
engine.execStderr = "" // the convicted signature: nothing on stderr
docker := newTestDockerClient(t, engine)
// The id never enters the inspect map: the fake's Engine 404s it,
// exactly like the daemon does after the sweeper's delete.
err := docker.MakeDir(context.Background(), testHandle("swept-away"), "/data/out")
require.True(t, IsRemoteNotFound(err), "got %+v", err)
require.True(t, CanReplaceRemoteBinding(err),
"a vanished container must be replaceable")
require.Contains(t, err.Error(), "exit=1",
"the exit code must survive in the message (#3942 diagnostic gap)")
}
// A genuine command failure with the container still alive keeps the
// InvalidRequest classification — and with it the binding.
func TestFileOpGenuineFailureKeepsBinding(t *testing.T) {
engine := newFakeDockerEngine()
engine.execExit = 1
engine.execStderr = "mkdir: cannot create directory '/data/out': File exists"
engine.inspect["container-1"] = container.InspectResponse{
ID: "container-1",
State: &container.State{Status: "running"},
}
docker := newTestDockerClient(t, engine)
err := docker.MakeDir(context.Background(), testHandle("container-1"), "/data/out")
require.True(t, IsRemoteInvalidRequest(err), "got %+v", err)
require.False(t, CanReplaceRemoteBinding(err))
}