@@ -36,6 +36,9 @@ func workspaceAgent() *cobra.Command {
36
36
// This command isn't useful to manually execute.
37
37
Hidden : true ,
38
38
RunE : func (cmd * cobra.Command , args []string ) error {
39
+ ctx , cancel := context .WithCancel (cmd .Context ())
40
+ defer cancel ()
41
+
39
42
rawURL , err := cmd .Flags ().GetString (varAgentURL )
40
43
if err != nil {
41
44
return xerrors .Errorf ("CODER_AGENT_URL must be set: %w" , err )
@@ -57,22 +60,22 @@ func workspaceAgent() *cobra.Command {
57
60
// Spawn a reaper so that we don't accumulate a ton
58
61
// of zombie processes.
59
62
if reaper .IsInitProcess () && ! noReap && isLinux {
60
- logger .Info (cmd . Context () , "spawning reaper process" )
63
+ logger .Info (ctx , "spawning reaper process" )
61
64
// Do not start a reaper on the child process. It's important
62
65
// to do this else we fork bomb ourselves.
63
66
args := append (os .Args , "--no-reap" )
64
67
err := reaper .ForkReap (reaper .WithExecArgs (args ... ))
65
68
if err != nil {
66
- logger .Error (cmd . Context () , "failed to reap" , slog .Error (err ))
69
+ logger .Error (ctx , "failed to reap" , slog .Error (err ))
67
70
return xerrors .Errorf ("fork reap: %w" , err )
68
71
}
69
72
70
- logger .Info (cmd . Context () , "reaper process exiting" )
73
+ logger .Info (ctx , "reaper process exiting" )
71
74
return nil
72
75
}
73
76
74
77
version := buildinfo .Version ()
75
- logger .Info (cmd . Context () , "starting agent" ,
78
+ logger .Info (ctx , "starting agent" ,
76
79
slog .F ("url" , coderURL ),
77
80
slog .F ("auth" , auth ),
78
81
slog .F ("version" , version ),
@@ -84,7 +87,7 @@ func workspaceAgent() *cobra.Command {
84
87
// Enable pprof handler
85
88
// This prevents the pprof import from being accidentally deleted.
86
89
_ = pprof .Handler
87
- pprofSrvClose := serveHandler (cmd . Context () , logger , nil , pprofAddress , "pprof" )
90
+ pprofSrvClose := serveHandler (ctx , logger , nil , pprofAddress , "pprof" )
88
91
defer pprofSrvClose ()
89
92
90
93
// exchangeToken returns a session token.
@@ -102,7 +105,7 @@ func workspaceAgent() *cobra.Command {
102
105
// This is *only* done for testing to mock client authentication.
103
106
// This will never be set in a production scenario.
104
107
var gcpClient * metadata.Client
105
- gcpClientRaw := cmd . Context () .Value ("gcp-client" )
108
+ gcpClientRaw := ctx .Value ("gcp-client" )
106
109
if gcpClientRaw != nil {
107
110
gcpClient , _ = gcpClientRaw .(* metadata.Client )
108
111
}
@@ -113,7 +116,7 @@ func workspaceAgent() *cobra.Command {
113
116
// This is *only* done for testing to mock client authentication.
114
117
// This will never be set in a production scenario.
115
118
var awsClient * http.Client
116
- awsClientRaw := cmd . Context () .Value ("aws-client" )
119
+ awsClientRaw := ctx .Value ("aws-client" )
117
120
if awsClientRaw != nil {
118
121
awsClient , _ = awsClientRaw .(* http.Client )
119
122
if awsClient != nil {
@@ -127,7 +130,7 @@ func workspaceAgent() *cobra.Command {
127
130
// This is *only* done for testing to mock client authentication.
128
131
// This will never be set in a production scenario.
129
132
var azureClient * http.Client
130
- azureClientRaw := cmd . Context () .Value ("azure-client" )
133
+ azureClientRaw := ctx .Value ("azure-client" )
131
134
if azureClientRaw != nil {
132
135
azureClient , _ = azureClientRaw .(* http.Client )
133
136
if azureClient != nil {
@@ -166,7 +169,7 @@ func workspaceAgent() *cobra.Command {
166
169
"GIT_ASKPASS" : executablePath ,
167
170
},
168
171
})
169
- <- cmd . Context () .Done ()
172
+ <- ctx .Done ()
170
173
return closer .Close ()
171
174
},
172
175
}
0 commit comments