@@ -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