8000 fix(agent/agentcontainers): prevent reassigning proc.agent until succ… · coder/coder@4f44dd0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f44dd0

Browse files
authored
fix(agent/agentcontainers): prevent reassigning proc.agent until successful (#18609)
1 parent 09cc906 commit 4f44dd0

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

agent/agentcontainers/api.go

Lines changed: 3 additions & 2 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -1484,15 +1484,16 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
14841484
originalName := subAgentConfig.Name
14851485

14861486
for attempt := 1; attempt <= maxAttemptsToNameAgent; attempt++ {
1487-
if proc.agent, err = client.Create(ctx, subAgentConfig); err == nil {
1487+
agent, err := client.Create(ctx, subAgentConfig)
1488+
if err == nil {
1489+
proc.agent = agent // Only reassign on success.
14881490
if api.usingWorkspaceFolderName[dc.WorkspaceFolder] {
14891491
api.devcontainerNames[dc.Name] = true
14901492
delete(api.usingWorkspaceFolderName, dc.WorkspaceFolder)
14911493
}
14921494

14931495
break
14941496
}
1495-
14961497
// NOTE(DanielleMaywood):
14971498
// Ordinarily we'd use `errors.As` here, but it didn't appear to work. Not
14981499
// sure if this is because of the communication protocol? Instead I've opted

agent/agentcontainers/subagent.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (a *subAgentAPIClient) List(ctx context.Context) ([]SubAgent, error) {
188188
return agents, nil
189189
}
190190

191-
func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (SubAgent, error) {
191+
func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (_ SubAgent, err error) {
192192
a.logger.Debug(ctx, "creating sub agent", slog.F("name", agent.Name), slog.F("directory", agent.Directory))
193193

194194
displayApps := make([]agentproto.< 8000 span class=pl-smi>CreateSubAgentRequest_DisplayApp, 0, len(agent.DisplayApps))
@@ -233,19 +233,27 @@ func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (SubAgen
233233
if err != nil {
234234
return SubAgent{}, err
235235
}
236+
defer func() {
237+
if err != nil {
238+
// Best effort.
239+
_, _ = a.api.DeleteSubAgent(ctx, &agentproto.DeleteSubAgentRequest{
240+
Id: resp.GetAgent().GetId(),
241+
})
242+
}
243+
}()
236244

237-
agent.Name = resp.Agent.Name
238-
agent.ID, err = uuid.FromBytes(resp.Agent.Id)
245+
agent.Name = resp.GetAgent().GetName()
246+
agent.ID, err = uuid.FromBytes(resp.GetAgent().GetId())
239247
if err != nil {
240-
return agent, err
248+
return SubAgent{}, err
241249
}
242-
agent.AuthToken, err = uuid.FromBytes(resp.Agent.AuthToken)
250+
agent.AuthToken, err = uuid.FromBytes(resp.GetAgent().GetAuthToken())
243251
if err != nil {
244-
return agent, err
252+
return SubAgent{}, err
245253
}
246254

247-
for _, appError := range resp.AppCreationErrors {
248-
app := apps[appError.Index]
255+
for _, appError := range resp.GetAppCreationErrors() {
256+
app := apps[appError.GetIndex()]
249257

250258
a.logger.Warn(ctx, "unable to create app",
251259
slog.F("agent_name", agent.Name),

0 commit comments

Comments
 (0)
0