Skip to content

Commit 356e959

Browse files
Jai Raj ChoudharyJai Raj Choudhary
authored andcommitted
fix: proper context lifecycle management in run loop
- Replace defer cancel() with explicit cancel() calls - Cancel context immediately after each test iteration - Prevent context accumulation in tight loops - Address SonarQube reliability rating C -> A
1 parent 68cb347 commit 356e959

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

cmd/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,11 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
403403
o.limiter.Accept()
404404

405405
ctxWithTimeout, cancel := context.WithTimeout(ctx, o.requestTimeout)
406-
defer cancel()
407406
ctxWithTimeout = context.WithValue(ctxWithTimeout, runner.ContextKey("").ParentDir(), loader.GetContext())
408407

409408
output, err = suiteRunner.RunTestCase(&testCase, dataContext, ctxWithTimeout)
410409
if err = util.ErrorWrap(err, "failed to run '%s', %v", testCase.Name, err); err != nil {
410+
cancel() // Cancel context before handling error
411411
if o.requestIgnoreError {
412412
errs = append(errs, err)
413413
} else {
@@ -419,10 +419,12 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
419419
reverseRunner.WithTestReporter(runner.NewDiscardTestReporter())
420420
if _, err = reverseRunner.RunTestCase(
421421
&testCase, dataContext, ctxWithTimeout); err != nil {
422+
cancel() // Cancel context before returning error
422423
err = fmt.Errorf("got error in reverse test: %w", err)
423424
return
424425
}
425426
suiteRunner.WithTestReporter(o.reporter)
427+
cancel() // Cancel context after successful completion
426428
}
427429
dataContext[testCase.Name] = output
428430
}

0 commit comments

Comments
 (0)