From e9b78dc1e712bef017fef1c5c61e59279515a5c9 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 26 Jun 2025 19:51:52 +0000 Subject: [PATCH] fix(agent/agentcontainers): refresh containers before status change The previous method of refreshing before we change the devcontainer status introduced an intermediary state where the devcontainer might not yet have been assigned a container and will flicker as stopped before going into running. --- agent/agentcontainers/api.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/agent/agentcontainers/api.go b/agent/agentcontainers/api.go index 26ebafd660fb1..b853c865c8813 100644 --- a/agent/agentcontainers/api.go +++ b/agent/agentcontainers/api.go @@ -1002,6 +1002,15 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D logger.Info(ctx, "devcontainer created successfully") + // Ensure the container list is updated immediately after creation. + // This makes sure that dc.Container is populated before we acquire + // the lock avoiding a temporary inconsistency in the API state + // where status is running, but the container is nil. + if err := api.RefreshContainers(ctx); err != nil { + logger.Error(ctx, "failed to trigger immediate refresh after devcontainer creation", slog.Error(err)) + return xerrors.Errorf("refresh containers: %w", err) + } + api.mu.Lock() dc = api.knownDevcontainers[dc.WorkspaceFolder] // Update the devcontainer status to Running or Stopped based on the @@ -1020,13 +1029,6 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D api.knownDevcontainers[dc.WorkspaceFolder] = dc api.mu.Unlock() - // Ensure an immediate refresh to accurately reflect the - // devcontainer state after recreation. - if err := api.RefreshContainers(ctx); err != nil { - logger.Error(ctx, "failed to trigger immediate refresh after devcontainer creation", slog.Error(err)) - return xerrors.Errorf("refresh containers: %w", err) - } - return nil }