← Back to Pull requests

fix(remote): clean up connections and goroutines on Factory.Create error paths #2069

Closed
unknown closed this pull request without merging on Oct 16, 2026 13:53 UTC
View on GitHub
unknown unknown commented on Oct 10, 2026 14:54 UTC
Author

Which issue(s) this PR fixes:


Issue longhorn/longhorn#14231

What this PR does / why we need it:

Factory.Create() in pkg/backend/remote leaks resources on two error paths:

1. If a later connect() call fails, the TCP connections already established by earlier iterations are never closed.
2. If open() fails (e.g., the remote replica cannot be opened), the dataconn.Client created from the connections is never closed. Each dataconn.Client holds NumberOfConnections TCP connections and 5 goroutines (1 loop + 2 wire senders + 2 wire receivers), so every failed Create() leaks 2 fds and 5 goroutines.

In an HA storage system, engine startup with degraded/unreachable replicas performs repeated failed Create() calls (e.g., via retries or replica rebuilding), which accumulates leaked fds and goroutines in the engine process and can eventually exhaust resources.

This PR closes the already-established connections on connect() failure, and calls dataConnClient.Close() on open() failure to tear down the connections and goroutines.

Special notes for your reviewer:

The regression test TestFactoryCreateNoLeakOnOpenFailure uses a fake gRPC replica server that fails ReplicaOpen, then quantifies fd and goroutine leaks by comparing counts before and after 10 failed Create() calls. The assertions use a tolerance threshold to avoid flakiness from unrelated background activity.

Additional documentation or context

Leak reproduction evidence is available in longhorn/longhorn#14231: before the fix, 10 failed Create() calls leaked 20 fds and 50 goroutines; after the fix, 0 fds and 0 goroutines.

``release-note
Fixed a resource leak in remote backend Factory.Create error paths that leaked TCP connections and goroutines on failed replica creation

Diff: 229 lines (+171 / -58) across 2 files Reviewer: guolxingxing Review duration: 8579m
All Vouch review confidence checks passed
Residual risk 0.15 = Change Risk (0.37) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 229 lines diff across 2 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.325
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2069"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 37.0% (MEDIUM) · Higher = larger blast radius
37.0%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 14.6% · Safe to merge
14.6%