Skip to content

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

@Elvand-Lie

Description

@Elvand-Lie

Bug Description

In pkg/ssh/ssh_dialer.go, the DialContext implementation has two issues:

1. Goroutine leak

A background goroutine monitors ctx.Done() to force-close the connection on context cancellation. If the caller closes the connection manually while the context is still alive (common with context.Background() or long-lived contexts), the goroutine blocks forever on <-ctx.Done(), leaking 1 goroutine per connection.

2. Violates net.Dialer.DialContext contract

Per the Go standard library docs:

"Once successfully connected, any expiration of the context will not affect the connection."

The current implementation does the opposite it kills established connections when the context expires. The k8s dialer in this same repo already handles this correctly (pkg/k8s/dialer.go L80-84).

Additionally, the context is not used during the actual dial phase (d.Dial() ignores context entirely), so the goroutine provides no value.

Proposed Fix

The golang.org/x/crypto/ssh library already provides Client.DialContext which handles context correctly cancellation during dial, no effect after connection. Simply delegate to it and remove the broken goroutine.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions