@@ -182,6 +182,9 @@ func cmdRun(fl Flags) (int, error) {
182182
183183 undoMaxProcs := setResourceLimits (logger )
184184 defer undoMaxProcs ()
185+ // release the local reference to the undo function so it can be GC'd;
186+ // the deferred call above has already captured the actual function value.
187+ undoMaxProcs = nil //nolint:ineffassign,wastedassign
185188
186189 configFlag := fl .String ("config" )
187190 configAdapterFlag := fl .String ("adapter" )
@@ -252,12 +255,16 @@ func cmdRun(fl Flags) (int, error) {
252255 logBuffer .FlushTo (defaultLogger )
253256 return caddy .ExitCodeFailedStartup , fmt .Errorf ("loading initial config: %v" , err )
254257 }
258+ // release the reference to the config so it can be GC'd
259+ config = nil //nolint:ineffassign,wastedassign
255260
256- // at this stage the config will have replaced
257- // the default logger to the configured one, so
258- // we can now flush the buffered logs, then log
259- // that the config is running.
260- logBuffer .FlushTo (caddy .Log ())
261+ // at this stage the config will have replaced the
262+ // default logger to the configured one, so we can
263+ // log normally, now that the config is running.
264+ // also clear our ref to the buffer so it can get GC'd
265+ logger = caddy .Log ()
266+ defaultLogger = nil //nolint:ineffassign,wastedassign
267+ logBuffer = nil //nolint:wastedassign,ineffassign
261268 logger .Info ("serving initial configuration" )
262269
263270 // if we are to report to another process the successful start
@@ -273,12 +280,16 @@ func cmdRun(fl Flags) (int, error) {
273280 return caddy .ExitCodeFailedStartup ,
274281 fmt .Errorf ("dialing confirmation address: %v" , err )
275282 }
276- defer conn .Close ()
277283 _ , err = conn .Write (confirmationBytes )
278284 if err != nil {
279285 return caddy .ExitCodeFailedStartup ,
280286 fmt .Errorf ("writing confirmation bytes to %s: %v" , pingbackFlag , err )
281287 }
288+ // close (non-defer because we `select {}` below)
289+ // and release references so they can be GC'd
290+ conn .Close ()
291+ confirmationBytes = nil //nolint:ineffassign,wastedassign
292+ conn = nil //nolint:wastedassign,ineffassign
282293 }
283294
284295 // if enabled, reload config file automatically on changes
@@ -306,6 +317,9 @@ func cmdRun(fl Flags) (int, error) {
306317 }
307318 }
308319
320+ // release the last local logger reference
321+ logger = nil //nolint:wastedassign,ineffassign
322+
309323 select {}
310324}
311325
0 commit comments