diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 747177ba5b2b..83ed0728f545 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -4091,6 +4091,7 @@ output: # Options for analysis running. run: # Timeout for analysis, e.g. 30s, 5m. + # If the value is lower or equal to 0, the timeout is disabled. # Default: 1m timeout: 5m diff --git a/pkg/commands/flagsets.go b/pkg/commands/flagsets.go index 608f6b9de581..5e7944acf08d 100644 --- a/pkg/commands/flagsets.go +++ b/pkg/commands/flagsets.go @@ -49,7 +49,8 @@ func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) { internal.AddFlagAndBind(v, fs, fs.String, "go", "run.go", "", color.GreenString("Targeted Go version")) internal.AddHackedStringSlice(fs, "build-tags", color.GreenString("Build tags")) - internal.AddFlagAndBind(v, fs, fs.Duration, "timeout", "run.timeout", defaultTimeout, color.GreenString("Timeout for total work")) + internal.AddFlagAndBind(v, fs, fs.Duration, "timeout", "run.timeout", defaultTimeout, + color.GreenString("Timeout for total work. If <= 0, the timeout is disabled")) internal.AddFlagAndBind(v, fs, fs.Bool, "tests", "run.tests", true, color.GreenString("Analyze tests (*_test.go)")) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index ff7c5e467b67..d9aa7578cd70 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -238,14 +238,21 @@ func (c *runCommand) execute(_ *cobra.Command, args []string) { needTrackResources := logutils.IsVerbose() || c.opts.PrintResourcesUsage trackResourcesEndCh := make(chan struct{}) - defer func() { // XXX: this defer must be before ctx.cancel defer - if needTrackResources { // wait until resource tracking finished to print properly + + // Note: this defer must be before ctx.cancel defer + defer func() { + // wait until resource tracking finished to print properly + if needTrackResources { <-trackResourcesEndCh } }() - ctx, cancel := context.WithTimeout(context.Background(), c.cfg.Run.Timeout) - defer cancel() + ctx := context.Background() + if c.cfg.Run.Timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, c.cfg.Run.Timeout) + defer cancel() + } if needTrackResources { go watchResources(ctx, trackResourcesEndCh, c.log, c.debugf)