Skip to content

[release-0.x] fix: make Disconnect safe to call concurrently - #508

Merged
kke merged 3 commits into
release-0.xfrom
fix/v0-connection-client-race
Oct 29, 2026
Merged

kke merged 3 commits into
release-0.xfrom
fix/v0-connection-client-race

Conversation

@kke

@kke kke commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Connection now holds its client in an atomic.Value (a mutex would make the by-value embedded Connection uncopyable), and each method loads the client once, so Disconnect from another goroutine no longer races IsConnected/Exec.

Fixes #498

πŸ€– Generated with Claude Code

Connection.client was read and written without synchronization, so
calling Disconnect from another goroutine raced IsConnected and the
Exec methods, and could hand them a nil client.

The client is now held in an atomic.Value, which unlike a mutex keeps
Connection copyable for the types embedding it by value, and each method
loads it once.

Fixes #498

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke
kke requested a balanced review from Copilot October 29, 2026 10:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟑 Changes recommended

The atomic client swap leaves WinRM and OpenSSH transport state vulnerable to races with Disconnect, including a potential nil dereference during execution.

3 open issues
What changed in this PR

This PR addresses #498 on release-0.x by synchronizing Connection’s client reference and retaining a client snapshot during command execution.

Changes:

  • Replace direct client access with atomic.Value helpers.
  • Update connection and execution methods to use saved client references, and atomically clear the reference on disconnect.
  • Adapt mock setup and add concurrent-disconnect regression coverage.
File Description
connection.go Adds atomic client storage and updates client access and disconnect handling.
connection_test.go Updates client initialization in tests and adds concurrent-disconnect coverage.

Review effort: Balanced

Comment thread connection.go Resolved
Comment thread connection.go Resolved
Comment thread connection_test.go Resolved
WinRM cleared its client and OpenSSH its connected flag without synchronization while commands read them, so those are now atomic and WinRM commands use the client they loaded. Connection keeps its client behind a shared, mutex guarded pointer instead of an atomic.Value, which must not be copied after first use either, and the concurrency test now disconnects deterministically between the connection check and the command.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke
kke requested a balanced review from Copilot October 29, 2026 10:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟑 Changes recommended

The regression test orders client reads around Disconnect and would pass even without the holder’s locks, leaving concurrent-access synchronization untested.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Low severity Qualify Disconnect interruption behavior by transport

connection.go:422

The claim that Disconnect makes running commands fail does not hold for every transport: WinRM only clears its client pointer, and Localhost's disconnect is a no-op. In-flight commands can therefore continue running or blocking after Disconnect returns. Qualify interruption as transport-dependent rather than promising cancellation for all connections.

3 resolved since last review

Review effort: Balanced

Comment thread connection_test.go Resolved
The ordered test only covers the nil-client interleaving. Also qualify Disconnect's doc: whether a running command is interrupted depends on the transport.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke
kke requested a balanced review from Copilot October 29, 2026 11:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟒 Approval recommended

No active review issues remain in the changes synchronizing connection state during concurrent disconnection.

No open issues πŸŽ‰

1 resolved since last review

Review effort: Balanced

@kke
kke merged commit 3ceab1b into release-0.x Oct 29, 2026
8 checks passed
@kke
kke deleted the fix/v0-connection-client-race branch October 29, 2026 11:18
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.

2 participants