Skip to content

Add CLI runner goroutine leak test - #7582

Merged
Mzack9999 merged 1 commit into
devfrom
5121-cli-leaktest
Jul 26, 2026
Merged

Add CLI runner goroutine leak test#7582
Mzack9999 merged 1 commit into
devfrom
5121-cli-leaktest

Conversation

@Mzack9999

@Mzack9999 Mzack9999 commented Jul 24, 2026

Copy link
Copy Markdown
Member

Summary

  • Add subprocess-isolated goleak coverage for the CLI runner path (ParseOptionsNewRunEnumerationClose)
  • Gate the shared benchmark httptest on -test.bench so unit leak checks are not polluted

Closes #5121

Summary by CodeRabbit

  • Bug Fixes
    • Improved test isolation by preventing benchmark-only services from affecting regular test runs.
    • Added coverage to detect and prevent unexpected goroutine leaks during CLI execution.
    • Ensured benchmark resources are cleaned up only when applicable.

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The test harness now starts its shared HTTP server only for benchmarks and adds a forked CLI runner test that executes a local template, closes the runner, and verifies that no unexpected goroutines remain.

Changes

CLI test reliability

Layer / File(s) Summary
Benchmark-only server lifecycle
cmd/nuclei/main_benchmark_test.go
TestMain detects benchmark execution before creating dummyServer and targetURL, and conditionally closes the server during teardown.
Forked CLI leak test
cmd/nuclei/main_leak_test.go
The test re-executes itself in child mode, runs the nuclei runner against a local HTTP server and template, then uses goleak to verify goroutine cleanup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

A rabbit hops through tests at night,
Bench servers sleep when runs aren’t right.
The runner dashes, then shuts down,
While goleak checks each thread around.
“No loose loops!” we cheer and crown.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a CLI runner goroutine leak test.
Linked Issues check ✅ Passed The PR adds a subprocess-isolated goleak test for the CLI runner path, matching issue #5121's request.
Out of Scope Changes check ✅ Passed The benchmark server guard supports the new leak test and is part of the stated PR objective, not unrelated scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 5121-cli-leaktest

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmd/nuclei/main_benchmark_test.go`:
- Line 64: Update the argument check around the benchmark-detection logic to
match only the exact -test.bench flag or arguments beginning with -test.bench=.
Do not treat -test.benchmem or -test.benchtime as benchmark-selection flags, so
they do not start the shared server.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cafa4fc4-81fb-49f8-9e1f-adf797ade358

📥 Commits

Reviewing files that changed from the base of the PR and between bcf2089 and 35a87ba.

⛔ Files ignored due to path filters (1)
  • cmd/nuclei/testdata/leaktest/basic-http.yaml is excluded by !**/*.yaml
📒 Files selected for processing (2)
  • cmd/nuclei/main_benchmark_test.go
  • cmd/nuclei/main_leak_test.go


func isBenchmarkRun() bool {
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-test.bench") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Match only the benchmark-selection flag.

This also matches -test.benchmem and -test.benchtime, so unit runs using either flag still start the shared server. Restrict the match to -test.bench or -test.bench=....

Proposed fix
-		if strings.HasPrefix(arg, "-test.bench") {
+		if arg == "-test.bench" || strings.HasPrefix(arg, "-test.bench=") {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if strings.HasPrefix(arg, "-test.bench") {
if arg == "-test.bench" || strings.HasPrefix(arg, "-test.bench=") {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/nuclei/main_benchmark_test.go` at line 64, Update the argument check
around the benchmark-detection logic to match only the exact -test.bench flag or
arguments beginning with -test.bench=. Do not treat -test.benchmem or
-test.benchtime as benchmark-selection flags, so they do not start the shared
server.

@Mzack9999
Mzack9999 merged commit 3163621 into dev Jul 26, 2026
19 checks passed
@Mzack9999
Mzack9999 deleted the 5121-cli-leaktest branch July 26, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add LeakTest for Nuclei CLI

1 participant