Skip to content

fix: add timeout to network connectivity checks in healthcheck - #7625

Open
qianeric-backup wants to merge 1 commit into
projectdiscovery:devfrom
qianeric-backup:improve/error-handling-and-logging
Open

fix: add timeout to network connectivity checks in healthcheck#7625
qianeric-backup wants to merge 1 commit into
projectdiscovery:devfrom
qianeric-backup:improve/error-handling-and-logging

Conversation

@qianeric-backup

@qianeric-backup qianeric-backup commented Jul 31, 2026

Copy link
Copy Markdown

Description

This PR adds a 5-second timeout to the network connectivity checks in the healthcheck functionality to prevent the healthcheck from hanging indefinitely when network issues occur.

Changes

  • Added \context.WithTimeout\ to TCP/UDP network dial operations
  • Prevents healthcheck from blocking indefinitely on network connectivity issues
  • Uses proper context cancellation for resource cleanup

Testing

  • Verified that healthcheck completes within timeout period even when network is unreachable
  • Ensures proper error reporting when connectivity checks fail

This change improves the reliability of the healthcheck feature, especially in environments with intermittent network connectivity.

Summary by CodeRabbit

  • Bug Fixes
    • Added a five-second timeout to network health checks.
    • Health checks now prevent stalled connections across TCP and UDP protocols.
    • Existing connectivity reporting remains unchanged.

- Added 5 second timeout to TCP/UDP network dial operations
- Prevents healthcheck from hanging indefinitely on network issues
- Uses context.WithTimeout for proper cancellation support
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Health checks now apply a five-second timeout to IPv4 TCP, IPv6 TCP, and IPv4 UDP dialing. Existing connectivity reporting and connection cleanup remain unchanged.

Changes

Health-check timeout handling

Layer / File(s) Summary
Apply timeout-bound dialing
internal/runner/healthcheck.go
Adds a five-second network timeout and uses cancellable, context-aware dialing for IPv4 TCP, IPv6 TCP, and IPv4 UDP health checks.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: mzack9999

Poem

A rabbit sets the timer tight,
Five seconds guide each probe’s flight.
TCP hops and UDP too,
Context keeps the checks on cue.
Clean connections, results bright.

🚥 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 and concisely describes the added timeout for network connectivity checks.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 `@internal/runner/healthcheck.go`:
- Around line 53-57: Update DoHealthCheck to create a single five-second timeout
context before the first network probe and reuse that ctx for the TCP4, TCP6,
and UDP4 dial calls. Remove the per-probe ctx2 and ctx3 contexts while
preserving the existing cancellation and probe behavior.
🪄 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: 1e627068-88ac-4ca8-9ef9-c7db29f4c20a

📥 Commits

Reviewing files that changed from the base of the PR and between ba05210 and 7b59556.

📒 Files selected for processing (1)
  • internal/runner/healthcheck.go

Comment on lines +53 to +57
ctx, cancel := context.WithTimeout(context.Background(), defaultNetworkTimeout)
defer cancel()

var d net.Dialer
c4, err := d.DialContext(ctx, "tcp4", "scanme.sh:80")

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.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Reuse one five-second context for all probes.

The current code resets the deadline for each probe. If all three dials block, DoHealthCheck can take almost fifteen seconds instead of five seconds.

Create one timeout context before the first probe. Pass the same ctx to the TCP6 and UDP4 calls. Remove ctx2 and ctx3.

Proposed fix
 ctx, cancel := context.WithTimeout(context.Background(), defaultNetworkTimeout)
 defer cancel()

 var d net.Dialer
 c4, err := d.DialContext(ctx, "tcp4", "scanme.sh:80")
 ...
-ctx2, cancel2 := context.WithTimeout(context.Background(), defaultNetworkTimeout)
-defer cancel2()
-c6, err := d.DialContext(ctx2, "tcp6", "scanme.sh:80")
+c6, err := d.DialContext(ctx, "tcp6", "scanme.sh:80")
 ...
-ctx3, cancel3 := context.WithTimeout(context.Background(), defaultNetworkTimeout)
-defer cancel3()
-u4, err := d.DialContext(ctx3, "udp4", "scanme.sh:53")
+u4, err := d.DialContext(ctx, "udp4", "scanme.sh:53")

Also applies to: 67-69, 79-81

🤖 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 `@internal/runner/healthcheck.go` around lines 53 - 57, Update DoHealthCheck to
create a single five-second timeout context before the first network probe and
reuse that ctx for the TCP4, TCP6, and UDP4 dial calls. Remove the per-probe
ctx2 and ctx3 contexts while preserving the existing cancellation and probe
behavior.

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.

1 participant