bug: devcontainer associates with a stale stopped container, leaving the running one permanently without a sub-agent

Ouverte
#29,766 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

  • #29882 par @pujitha24 — fermée sans fusion

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
65/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
docker, go
Domaine
backend, devtools

Piste de recherche

The bug is in agent/agentcontainers/containers_dockercli.go and api.go. Start by reading the container listing logic at line 247 and the association loop around line 982. The fix likely involves filtering for running containers or matching on both devcontainer labels. Run the existing tests for agentcontainers and add a test that reproduces the stale container scenario. Verify the fix by building the agent and testing with the steps in the issue.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Is there an existing issue for this?
  • I have searched the existing issues
Current Behavior

A devcontainer can be left permanently without a sub-agent while its container runs
normally, with Coder reporting the devcontainer as stopped and naming a container that
exited hours earlier.

When two containers carry the same devcontainer.local_folder label -- e.g. a stale one
left behind beside a freshly created one -- the agent associates the devcontainer with
the oldest match, regardless of state. coder show then displays that dead
container's name and a stopped status, and because injection is gated on Running,
no sub-agent is ever injected into the container that is actually running. That
container has no agent process at all.

It never recovers on its own: the refresh loop recomputes the same association every
interval. It is also silent -- no error, no warning, and no container_id in the agent
log, because the injection function is never entered. Removing the stale container is
the only fix; the next refresh then injects normally.

Three pieces compose into it:

  1. agent/agentcontainers/containers_dockercli.go:247 lists containers with
    docker ps --all --quiet --no-trunc -- stopped containers included -- and appends ids
    in output order. docker ps sorts newest-first.

  2. agent/agentcontainers/api.go:~982, in the container -> devcontainer association loop:

     if dc, ok := api.knownDevcontainers[workspaceFolder]; ok {
         ...
         dc.Container = container          // last write wins; no check on Running
         api.knownDevcontainers[dc.WorkspaceFolder] = dc
         continue
     }
    

    Newest-first input plus last-write-wins means the oldest matching container ends
    up in dc.Container.

  3. Injection is then gated on the status derived from that container:

    if dc.Status == codersdk.WorkspaceAgentDevcontainerStatusRunning {
    err := api.maybeInjectSubAgentIntoContainerLocked(ctx, dc)

Note the asymmetry that makes this reachable: @devcontainers/cli decides whether to
reuse a container by matching both id labels (devcontainer.local_folder and
devcontainer.config_file), but the association loop above keys on
devcontainer.local_folder alone. A container the CLI correctly declines to
reuse is still adopted by the agent.

Relevant Log Output

coder show -- the devcontainer is bound to affectionate_euclid, which exited the
previous day, and is reported stopped:

└─ main (linux, amd64)          ⦿ connected  ✔ healthy  v2.36.4+10fd510
   └─ Devcontainers
      └─ myproject [affectionate_euclid]          ⏹ stopped

docker ps -a on the workspace pod -- the bound container is long dead, while the real
devcontainer has been up for 21 minutes (unrelated containers trimmed):

CONTAINER ID   IMAGE                          CREATED          STATUS                       NAMES
c96d64aede24   vsc-myproject-<hash>-uid       23 minutes ago   Up 21 minutes                hungry_albattani
931085ca94fc   <untagged older image>         20 hours ago     Exited (255) 20 hours ago    affectionate_euclid

Both carry devcontainer.local_folder=/workspace/myproject.

The running container had no agent process and no agent logs at all.

Agent log for the same period. The agent started at 18:57:21 with the real container
already running since 18:56:17, and created no sub-agent for 24 minutes. There is no
error, and no container_id field appears anywhere in that window, because the
injection function is never entered:

2026-09-22 18:57:21.075 [info]  agent is starting now
... 24 minutes: no devcontainer errors or warnings, no container_id logged ...

The stale container was then removed by hand, and the next refresh recovered
immediately:

# docker rm affectionate_euclid

2026-09-22 19:21:19.146 [info]  containers: detected container architecture
2026-09-22 19:21:19.513 [info]  containers: copied agent binary to container
2026-09-22 19:21:19.655 [debu]  containers: checking if sub agent should be deleted
2026-09-22 19:21:19.655 [debu]  containers: creating new subagent
2026-09-22 19:21:19.674 [info]  containers: starting subagent in devcontainer

# docker exec hungry_albattani ps aux | grep coder
node  3789  /.coder-agent/coder agent        # started 19:21

The only container id that ever appears in the agent log is the surviving one, first
mentioned at 19:21:19.

Expected Behavior

When several containers match a devcontainer's workspace folder, the agent should
associate the one the devcontainer CLI would actually use -- ideally by matching on the
same pair of id labels the CLI matches on, rather than on devcontainer.local_folder
alone. Preferring a running container, or the newest, would also avoid this particular
failure.

Steps to Reproduce

These steps are a way to recreate the scenario by launching a container, bringing up one and
stopping it, and then relaunching the original one in order to make the stopped one "older".
Coder will then not be able to inject the agent into the newer running container, and we will
be in a silently stuck state until the "older" container is deleted, leading to recovery.

  1. Create a workspace whose template declares a coder_devcontainer resource. Let the
    agent bring the devcontainer up and confirm a sub-agent is injected.

  2. On the workspace host, create a decoy container carrying the same workspace-folder
    label, and stop it. We are going to make this one the "older" container:
    docker run -d --name decoy
    --label devcontainer.local_folder=/workspace/myproject
    alpine sleep infinity
    docker stop decoy

  3. Recreate the real devcontainer so that it is newer than the decoy -- the Rebuild
    action in the UI, or devcontainer up --remove-existing-container.

  4. Wait one refresh interval (10s default).

    coder show reports the devcontainer as stopped and names decoy, although the
    real container is running. No sub-agent is injected into it, and nothing is logged.

  5. docker rm decoy

    Within one refresh interval the sub-agent is created and injected.

The ordering in step 3 matters: the decoy must be the older of the two, since the oldest
matching container is the one that wins.

Environment
  • Host OS: Ubuntu (workspace container), Kubernetes-hosted workspaces
  • Coder version: v2.36.0 (server), agent v2.36.4

Not tested on the latest release, but the relevant code is unchanged on main as of
this writing: containers_dockercli.go:246 still lists with --all, and api.go:982
still assigns dc.Container with no Running check.

Additional Context

The issue occurs consistently

Langage dominant
Go
Étoiles
17.5k
Forks
1.7k
Merge moyen
2 j 23 h
PR mergées (30 j)
575

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de coder/coder

Toutes les issues de coder/coder

Issues similaires

Plus d'issues Go

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.