fix(agent): delay containerAPI init to ensure startup scripts run before (#18630)

This commit is contained in:
Mathias Fredriksson
2025-06-27 14:10:35 +03:00
committed by GitHub
parent 3cb9b20b11
commit 7e99fb7d7e
3 changed files with 14 additions and 13 deletions
+12 -10
View File
@@ -1163,13 +1163,7 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
scriptRunnerOpts []agentscripts.InitOption
devcontainerScripts map[uuid.UUID]codersdk.WorkspaceAgentScript
)
if a.devcontainers {
a.containerAPI.Init(
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
agentcontainers.WithDevcontainers(manifest.Devcontainers, scripts),
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
)
if a.containerAPI != nil {
scripts, devcontainerScripts = agentcontainers.ExtractDevcontainerScripts(manifest.Devcontainers, scripts)
}
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted, scriptRunnerOpts...)
@@ -1190,9 +1184,17 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
// autostarted devcontainer will be included in this time.
err := a.scriptRunner.Execute(a.gracefulCtx, agentscripts.ExecuteStartScripts)
for _, dc := range manifest.Devcontainers {
cErr := a.createDevcontainer(ctx, aAPI, dc, devcontainerScripts[dc.ID])
err = errors.Join(err, cErr)
if a.containerAPI != nil {
a.containerAPI.Init(
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
agentcontainers.WithDevcontainers(manifest.Devcontainers, scripts),
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
)
for _, dc := range manifest.Devcontainers {
cErr := a.createDevcontainer(ctx, aAPI, dc, devcontainerScripts[dc.ID])
err = errors.Join(err, cErr)
}
}
dur := time.Since(start).Seconds()
+1 -2
View File
@@ -530,7 +530,6 @@ func (api *API) updateContainers(ctx context.Context) error {
// will clear up on the next update.
if !errors.Is(err, context.Canceled) {
api.mu.Lock()
api.containers = codersdk.WorkspaceAgentListContainersResponse{}
api.containersErr = err
api.mu.Unlock()
}
@@ -947,7 +946,7 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
slog.F("devcontainer_id", dc.ID),
slog.F("devcontainer_name", dc.Name),
slog.F("workspace_folder", dc.WorkspaceFolder),
slog.F("config_path", configPath),
slog.F("config_path", dc.ConfigPath),
)
)
+1 -1
View File
@@ -36,7 +36,7 @@ func (a *agent) apiHandler() http.Handler {
cacheDuration: cacheDuration,
}
if a.devcontainers {
if a.containerAPI != nil {
r.Mount("/api/v0/containers", a.containerAPI.Routes())
} else {
r.HandleFunc("/api/v0/containers", func(w http.ResponseWriter, r *http.Request) {