Skip to content

Fix goroutine leak in SSH DialContext - #3882

Merged
knative-prow[bot] merged 2 commits into
knative:mainfrom
Elvand-Lie:fix-ssh-dialer-leak-3881
Jun 8, 2026
Merged

Fix goroutine leak in SSH DialContext#3882
knative-prow[bot] merged 2 commits into
knative:mainfrom
Elvand-Lie:fix-ssh-dialer-leak-3881

Conversation

@Elvand-Lie

@Elvand-Lie Elvand-Lie commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Changes

Fixes #3881

Why is this needed?

The previous DialContext implementation spawned a background goroutine that watched ctx.Done() and force-closed the connection when the context expired. This had two problems:

  1. Goroutine leak: If the caller closed the connection manually while the context was still alive (e.g., context.Background()), the goroutine blocked forever on <-ctx.Done(), leaking 1 goroutine per connection.

  2. Violated net.Dialer.DialContext semantics: Per the Go docs: "Once successfully connected, any expiration of the context will not affect the connection." The old code did the opposite it killed established connections when the context expired. The k8s dialer in this same repo already documents this contract correctly (pkg/k8s/dialer.go L80-84).

The fix

The golang.org/x/crypto/ssh library already provides Client.DialContext which handles context-cancellable dialing correctly:

  • Context cancellation during the dial phase → returns error
  • Context expiration after connection is established → does not affect the connection

So the fix simply delegates to d.sshClient.DialContext(ctx, d.network, d.addr) instead of calling d.Dial() + spawning a broken monitoring goroutine. The connWithDone wrapper and sync import are removed as dead code.

Testing performed

  • go test ./pkg/ssh/... passes.
  • The change is a net deletion of 30 lines removing incorrect behavior, not adding new logic.
Fixed a goroutine leak and incorrect context handling in the SSH dialer. DialContext now correctly delegates to ssh.Client.DialContext, respecting standard Go context semantics.
NONE

@knative-prow knative-prow Bot added the kind/bug Bugs label Jun 6, 2026
@knative-prow
knative-prow Bot requested review from dsimansk and jrangelramos June 6, 2026 08:29
@knative-prow knative-prow Bot added size/S 🤖 PR changes 10-29 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels Jun 6, 2026
@knative-prow

knative-prow Bot commented Jun 6, 2026

Copy link
Copy Markdown

Hi @Elvand-Lie. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@codecov

codecov Bot commented Jun 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.01%. Comparing base (165ccda) to head (57ed8b5).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3882      +/-   ##
==========================================
+ Coverage   53.09%   54.01%   +0.91%     
==========================================
  Files         200      200              
  Lines       23567    23558       -9     
==========================================
+ Hits        12514    12725     +211     
+ Misses       9832     9605     -227     
- Partials     1221     1228       +7     
Flag Coverage Δ
e2e 33.50% <0.00%> (+11.87%) ⬆️
e2e go 29.42% <0.00%> (+0.01%) ⬆️
e2e node 25.74% <0.00%> (+0.01%) ⬆️
e2e python 29.76% <0.00%> (+0.01%) ⬆️
e2e quarkus 25.87% <0.00%> (+0.01%) ⬆️
e2e rust 25.34% <0.00%> (+0.01%) ⬆️
e2e springboot 24.01% <0.00%> (+<0.01%) ⬆️
e2e typescript 25.85% <0.00%> (+0.01%) ⬆️
e2e-config-ci 26.96% <0.00%> (+0.01%) ⬆️
integration 15.74% <0.00%> (+0.04%) ⬆️
unit macos-14 42.89% <100.00%> (-0.04%) ⬇️
unit macos-latest 42.89% <100.00%> (-0.04%) ⬇️
unit ubuntu-24.04-arm 43.21% <100.00%> (-0.04%) ⬇️
unit ubuntu-latest 43.75% <100.00%> (-0.04%) ⬇️
unit windows-latest 42.95% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@matejvasek

Copy link
Copy Markdown
Contributor

Looking at the code I am not sure whether even the old version is correct:

https://pkg.go.dev/net#Dialer.DialContext

The provided Context must be non-nil. If the context expires before the connection is complete, an error is returned. Once successfully connected, any expiration of the context will not affect the connection.

I guess we should simply ignore the ctx and spawn no goroutine at all.

Copilot AI 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.

Pull request overview

Fixes a goroutine leak in pkg/ssh’s DialContext by ensuring the background context-monitoring goroutine can also exit when the returned connection is manually closed (not only when the context is cancelled). This aligns the implementation with the lifecycle patterns described in Issue #3881 for long-lived contexts (e.g., context.Background()).

Changes:

  • Introduced a connWithDone wrapper that signals a done channel on Close().
  • Updated the monitoring goroutine to select on both ctx.Done() and the connection’s done signal.
  • Returned the wrapped connection from DialContext to ensure caller-initiated Close() triggers goroutine cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/ssh/ssh_dialer.go Outdated
Comment on lines +151 to +155
select {
case <-ctx.Done():
conn.Close()
case <-wrapped.done:
// Connection was closed by the caller; exit cleanly.
Comment thread pkg/ssh/ssh_dialer.go Outdated
Comment on lines +149 to +153
go func() {
if ctx != nil {
<-ctx.Done()
conn.Close()
select {
case <-ctx.Done():
conn.Close()
@Elvand-Lie

Elvand-Lie commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

@matejvasek I've removed the goroutine entirely instead of patching it since the goroutine is a bit redundant. x/crypto/ssh already has Client.DialContext that handles context cancellation during dial correctly, so I'm just delegating to it now. Net -30 lines. Should we have a regression test for this, or is the deletion self-evident enough?

@matejvasek

Copy link
Copy Markdown
Contributor

/approve
/lgtm

@knative-prow knative-prow Bot added lgtm 🤖 PR is ready to be merged. approved 🤖 PR has been approved by an approver from all required OWNERS files. labels Jun 7, 2026
@matejvasek

Copy link
Copy Markdown
Contributor

/ok-to-test

@knative-prow knative-prow Bot added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels Jun 7, 2026
@matejvasek

Copy link
Copy Markdown
Contributor

Please rebase on the main branch to fix the CI issues.

The monitoring goroutine in DialContext violated net.Dialer.DialContext semantics:
context should only govern connection establishment, not connection lifetime.
The goroutine also leaked when connections were closed manually while the
context remained open.

Remove the goroutine and connWithDone wrapper entirely. Delegate to
ssh.Client.DialContext which already handles context-cancellable dialing
correctly per the Go contract.
@Elvand-Lie
Elvand-Lie force-pushed the fix-ssh-dialer-leak-3881 branch from 1e0117a to 57ed8b5 Compare June 8, 2026 16:36
@knative-prow knative-prow Bot removed the lgtm 🤖 PR is ready to be merged. label Jun 8, 2026
@Elvand-Lie

Copy link
Copy Markdown
Contributor Author

Rebased with the same changes @matejvasek

@matejvasek

Copy link
Copy Markdown
Contributor

/approve
/lgtm

@knative-prow knative-prow Bot added the lgtm 🤖 PR is ready to be merged. label Jun 8, 2026
@knative-prow

knative-prow Bot commented Jun 8, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Elvand-Lie, matejvasek

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow
knative-prow Bot merged commit 213fa33 into knative:main Jun 8, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved 🤖 PR has been approved by an approver from all required OWNERS files. kind/bug Bugs lgtm 🤖 PR is ready to be merged. ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/S 🤖 PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkg/ssh: DialContext leaks goroutines if context is never cancelled

3 participants