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

Skip to content

Commit c515085

Browse files
authored
fix: Unify context usage for agent cmd and logs (#5059)
1 parent cbb1e91 commit c515085

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

agent/agent.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (a *agent) run(ctx context.Context) error {
174174
if err != nil {
175175
return xerrors.Errorf("fetch metadata: %w", err)
176176
}
177-
a.logger.Info(context.Background(), "fetched metadata")
177+
a.logger.Info(ctx, "fetched metadata")
178178
oldMetadata := a.metadata.Swap(metadata)
179179

180180
// The startup script should only execute on the first run!
@@ -208,7 +208,7 @@ func (a *agent) run(ctx context.Context) error {
208208
a.closeMutex.Lock()
209209
network := a.network
210210
a.closeMutex.Unlock()
211-
if a.network == nil {
211+
if network == nil {
212212
a.logger.Debug(ctx, "creating tailnet")
213213
network, err = a.createTailnet(ctx, metadata.DERPMap)
214214
if err != nil {
@@ -365,7 +365,7 @@ func (a *agent) runCoordinator(ctx context.Context, network *tailnet.Conn) error
365365
return err
366366
}
367367
defer coordinator.Close()
368-
a.logger.Info(context.Background(), "connected to coordination server")
368+
a.logger.Info(ctx, "connected to coordination server")
369369
sendNodes, errChan := tailnet.ServeCoordinator(coordinator, network.UpdateNodes)
370370
network.SetNodeCallback(sendNodes)
371371
select {

cli/agent.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func workspaceAgent() *cobra.Command {
3636
// This command isn't useful to manually execute.
3737
Hidden: true,
3838
RunE: func(cmd *cobra.Command, args []string) error {
39+
ctx, cancel := context.WithCancel(cmd.Context())
40+
defer cancel()
41+
3942
rawURL, err := cmd.Flags().GetString(varAgentURL)
4043
if err != nil {
4144
return xerrors.Errorf("CODER_AGENT_URL must be set: %w", err)
@@ -57,22 +60,22 @@ func workspaceAgent() *cobra.Command {
5760
// Spawn a reaper so that we don't accumulate a ton
5861
// of zombie processes.
5962
if reaper.IsInitProcess() && !noReap && isLinux {
60-
logger.Info(cmd.Context(), "spawning reaper process")
63+
logger.Info(ctx, "spawning reaper process")
6164
// Do not start a reaper on the child process. It's important
6265
// to do this else we fork bomb ourselves.
6366
args := append(os.Args, "--no-reap")
6467
err := reaper.ForkReap(reaper.WithExecArgs(args...))
6568
if err != nil {
66-
logger.Error(cmd.Context(), "failed to reap", slog.Error(err))
69+
logger.Error(ctx, "failed to reap", slog.Error(err))
6770
return xerrors.Errorf("fork reap: %w", err)
6871
}
6972

70-
logger.Info(cmd.Context(), "reaper process exiting")
73+
logger.Info(ctx, "reaper process exiting")
7174
return nil
7275
}
7376

7477
version := buildinfo.Version()
75-
logger.Info(cmd.Context(), "starting agent",
78+
logger.Info(ctx, "starting agent",
7679
slog.F("url", coderURL),
7780
slog.F("auth", auth),
7881
slog.F("version", version),
@@ -84,7 +87,7 @@ func workspaceAgent() *cobra.Command {
8487
// Enable pprof handler
8588
// This prevents the pprof import from being accidentally deleted.
8689
_ = pprof.Handler
87-
pprofSrvClose := serveHandler(cmd.Context(), logger, nil, pprofAddress, "pprof")
90+
pprofSrvClose := serveHandler(ctx, logger, nil, pprofAddress, "pprof")
8891
defer pprofSrvClose()
8992

9093
// exchangeToken returns a session token.
@@ -102,7 +105,7 @@ func workspaceAgent() *cobra.Command {
102105
// This is *only* done for testing to mock client authentication.
103106
// This will never be set in a production scenario.
104107
var gcpClient *metadata.Client
105-
gcpClientRaw := cmd.Context().Value("gcp-client")
108+
gcpClientRaw := ctx.Value("gcp-client")
106109
if gcpClientRaw != nil {
107110
gcpClient, _ = gcpClientRaw.(*metadata.Client)
108111
}
@@ -113,7 +116,7 @@ func workspaceAgent() *cobra.Command {
113116
// This is *only* done for testing to mock client authentication.
114117
// This will never be set in a production scenario.
115118
var awsClient *http.Client
116-
awsClientRaw := cmd.Context().Value("aws-client")
119+
awsClientRaw := ctx.Value("aws-client")
117120
if awsClientRaw != nil {
118121
awsClient, _ = awsClientRaw.(*http.Client)
119122
if awsClient != nil {
@@ -127,7 +130,7 @@ func workspaceAgent() *cobra.Command {
127130
// This is *only* done for testing to mock client authentication.
128131
// This will never be set in a production scenario.
129132
var azureClient *http.Client
130-
azureClientRaw := cmd.Context().Value("azure-client")
133+
azureClientRaw := ctx.Value("azure-client")
131134
if azureClientRaw != nil {
132135
azureClient, _ = azureClientRaw.(*http.Client)
133136
if azureClient != nil {
@@ -166,7 +169,7 @@ func workspaceAgent() *cobra.Command {
166169
"GIT_ASKPASS": executablePath,
167170
},
168171
})
169-
<-cmd.Context().Done()
172+
<-ctx.Done()
170173
return closer.Close()
171174
},
172175
}

0 commit comments

Comments
 (0)