MCPcopy Index your code
hub / github.com/coder/coder / Close

Method Close

agent/agentcontainers/api.go:2173–2234  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2171}
2172
2173func (api *API) Close() error {
2174 api.mu.Lock()
2175 if api.closed {
2176 api.mu.Unlock()
2177 return nil
2178 }
2179 api.logger.Debug(api.ctx, "closing API")
2180 api.closed = true
2181
2182 // Stop all running subagent processes and clean up.
2183 subAgentIDs := make([]uuid.UUID, 0, len(api.injectedSubAgentProcs))
2184 for workspaceFolder, proc := range api.injectedSubAgentProcs {
2185 api.logger.Debug(api.ctx, "canceling subagent process",
2186 slog.F("agent_name", proc.agent.Name),
2187 slog.F("agent_id", proc.agent.ID),
2188 slog.F("container_id", proc.containerID),
2189 slog.F("workspace_folder", workspaceFolder),
2190 )
2191 proc.stop()
2192 if proc.agent.ID != uuid.Nil {
2193 subAgentIDs = append(subAgentIDs, proc.agent.ID)
2194 }
2195 }
2196 api.injectedSubAgentProcs = make(map[string]subAgentProcess)
2197
2198 api.cancel() // Interrupt all routines.
2199 api.mu.Unlock() // Release lock before waiting for goroutines.
2200
2201 // Note: We can't use api.ctx here because it's canceled.
2202 deleteCtx, deleteCancel := context.WithTimeout(context.Background(), defaultOperationTimeout)
2203 defer deleteCancel()
2204 client := *api.subAgentClient.Load()
2205 for _, id := range subAgentIDs {
2206 err := client.Delete(deleteCtx, id)
2207 if err != nil {
2208 api.logger.Error(api.ctx, "delete subagent record during shutdown failed",
2209 slog.Error(err),
2210 slog.F("agent_id", id),
2211 )
2212 }
2213 }
2214
2215 // Close the watcher to ensure its loop finishes.
2216 err := api.watcher.Close()
2217
2218 // Wait for loops to finish.
2219 if api.watcherDone != nil {
2220 <-api.watcherDone
2221 }
2222 if api.updaterDone != nil {
2223 <-api.updaterDone
2224 }
2225 if api.discoverDone != nil {
2226 <-api.discoverDone
2227 }
2228
2229 // Wait for all async tasks to complete.
2230 api.asyncWg.Wait()

Callers 5

TestAPIFunction · 0.95

Calls 9

stopMethod · 0.65
DeleteMethod · 0.65
CloseMethod · 0.65
WaitMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45
cancelMethod · 0.45
LoadMethod · 0.45
ErrorMethod · 0.45

Tested by 5

TestAPIFunction · 0.76