Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f413db

Browse files
committed
use safer atomic pointer for containerapi
1 parent 4a5842b commit 1f413db

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

agent/agent.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ type Options struct {
9090
BlockFileTransfer bool
9191
Execer agentexec.Execer
9292

93-
ContainerAPIOptions []agentcontainers.Option
9493
ExperimentalDevcontainersEnabled bool
94+
ContainerAPIOptions []agentcontainers.Option // Enable ExperimentalDevcontainersEnabled for these to be effective.
9595
}
9696

9797
type Client interface {
@@ -190,8 +190,8 @@ func New(options Options) Agent {
190190
metrics: newAgentMetrics(prometheusRegistry),
191191
execer: options.Execer,
192192

193-
containerAPIOptions: options.ContainerAPIOptions,
194193
experimentalDevcontainersEnabled: options.ExperimentalDevcontainersEnabled,
194+
containerAPIOptions: options.ContainerAPIOptions,
195195
}
196196
// Initially, we have a closed channel, reflecting the fact that we are not initially connected.
197197
// Each time we connect we replace the channel (while holding the closeMutex) with a new one
@@ -274,7 +274,7 @@ type agent struct {
274274

275275
experimentalDevcontainersEnabled bool
276276
containerAPIOptions []agentcontainers.Option
277-
containerAPI *agentcontainers.API
277+
containerAPI atomic.Pointer[agentcontainers.API] // Set by apiHandler.
278278
}
279279

280280
func (a *agent) TailnetConn() *tailnet.Conn {
@@ -1168,11 +1168,11 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11681168
}
11691169
a.metrics.startupScriptSeconds.WithLabelValues(label).Set(dur)
11701170
a.scriptRunner.StartCron()
1171-
if a.containerAPI != nil {
1171+
if containerAPI := a.containerAPI.Load(); containerAPI != nil {
11721172
// Inform the container API that the agent is ready.
11731173
// This allows us to start watching for changes to
11741174
// the devcontainer configuration files.
1175-
a.containerAPI.SignalReady()
1175+
containerAPI.SignalReady()
11761176
}
11771177
})
11781178
if err != nil {

agent/api.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
5252
// Append after to allow the agent options to override the default options.
5353
containerAPIOpts = append(containerAPIOpts, a.containerAPIOptions...)
5454

55-
a.containerAPI = agentcontainers.NewAPI(a.logger.Named("containers"), containerAPIOpts...)
56-
r.Mount("/api/v0/containers", a.containerAPI.Routes())
55+
containerAPI := agentcontainers.NewAPI(a.logger.Named("containers"), containerAPIOpts...)
56+
r.Mount("/api/v0/containers", containerAPI.Routes())
57+
a.containerAPI.Store(containerAPI)
5758
} else {
5859
r.HandleFunc("/api/v0/containers", func(w http.ResponseWriter, r *http.Request) {
5960
httpapi.Write(r.Context(), w, http.StatusForbidden, codersdk.Response{
@@ -75,8 +76,8 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
7576
r.Get("/debug/prometheus", promHandler.ServeHTTP)
7677

7778
return r, func() error {
78-
if a.containerAPI != nil {
79-
return a.containerAPI.Close()
79+
if containerAPI := a.containerAPI.Load(); containerAPI != nil {
80+
return containerAPI.Close()
8081
}
8182
return nil
8283
}

0 commit comments

Comments
 (0)