From 15a2e2816197efa7d229bf25feb31addbeded32f Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Sat, 25 Feb 2023 11:22:58 +0200 Subject: [PATCH] Fix usage report block of k6 execution if it takes longer than 3s --- cmd/run.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index c22a1b9a8ce..c0f1c1bbca7 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -307,22 +307,17 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { // Init has passed successfully, so unless disabled, make sure we send a // usage report after the context is done. if !conf.NoUsageReport.Bool { - backgroundProcesses.Add(2) - reportCtx, reportCancel := context.WithCancel(globalCtx) - reportDone := make(chan error) + backgroundProcesses.Add(1) go func() { + defer backgroundProcesses.Done() + reportCtx, reportCancel := context.WithTimeout(globalCtx, 3*time.Second) + defer reportCancel() logger.Debug("Sending usage report...") - reportDone <- reportUsage(reportCtx, execScheduler) - close(reportDone) - backgroundProcesses.Done() - }() - go func() { - select { - case <-reportDone: - case <-time.After(3 * time.Second): + if rerr := reportUsage(reportCtx, execScheduler); rerr != nil { + logger.WithError(rerr).Debug("Error sending usage report") + } else { + logger.Debug("Usage report sent successfully") } - reportCancel() - backgroundProcesses.Done() }() }