Skip to content

fix: context propagation in service lifecycle, ExecInteractive and Windows detection - #351

Merged
kke merged 5 commits into
mainfrom
context-tweaks
May 26, 2026
Merged

kke merged 5 commits into
mainfrom
context-tweaks

Conversation

@kke

@kke kke commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Adds fallbacks when passed in context does not have a timeout so that service functions don't block indefinetly.

Also adds context to ExecInteractive and makes the IsWindows() use a 10 second timeout.

gomodguard v1 is disabled in .golangci.yml because it nags about it being deprecated over v2 (which I assume is enabled via the all)

Part of rig v2 final stretch, breaking API is not a problem as rig v2 has not been published yet.

@kke
kke requested a review from Copilot May 26, 2026 12:47
@kke
kke force-pushed the context-tweaks branch 2 times, most recently from 15a2e78 to dcd3446 Compare May 26, 2026 12: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.

Pull request overview

This PR introduces timeout-aware fallbacks for operations that previously could block indefinitely when passed a context without a deadline, and updates interactive command execution to accept a context for cancellation.

Changes:

  • Add a default 2-minute timeout fallback for service lifecycle operations when ctx has no deadline.
  • Change ExecInteractive across client/protocols to accept context.Context and terminate sessions/processes on cancellation.
  • Adjust SSH/OpenSSH Windows detection to use a 10-second bounded probe and pre-warm the cache during Connect, plus add a timeout policy document and tests.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
TIMEOUT_POLICY.md New documentation describing timeout behavior and guidance for consumers.
service.go Adds withServiceTimeout fallback and applies it to several service operations.
service_test.go Adds tests for withServiceTimeout behavior.
protocol/winrm/connection.go Adds context-aware ExecInteractive and returns ctx.Err() on cancellation.
protocol/ssh/connection.go Refactors Windows detection to support context propagation and adds ExecInteractive(ctx, ...).
protocol/openssh/connection.go Adds Windows detection caching/probing and ExecInteractive(ctx, ...), and pre-warms detection during Connect.
protocol/localhost/connection.go Adds context-aware ExecInteractive and cancels/kills local process on ctx.Done().
protocol/connection.go Updates InteractiveExecer interface to require context.Context.
client.go Updates Client.ExecInteractive signature to accept context.Context and forward it.
Comments suppressed due to low confidence (1)

protocol/openssh/connection.go:183

  • Connect pre-warms the Windows cache via detectWindows(ctx). If ctx is canceled/expired (or expires during StartProcess), detectWindows will cache false due to the error path, potentially making IsWindows return the wrong value permanently. Consider skipping pre-warm when ctx.Err()!=nil and/or only caching on a successful probe (don’t update the cache on context cancellation/deadline).
		c.isConnected = true
		c.controlMutex.Unlock()
		// Pre-warm Windows detection using the connect context. This avoids
		// context.Background() probes on every first IsWindows() call.
		c.detectWindows(ctx)
		return nil

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

Comment thread protocol/openssh/connection.go Resolved
Comment thread protocol/ssh/connection.go Resolved

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Comments suppressed due to low confidence (2)

protocol/openssh/connection.go:235

  • Same as above: this Connect-time detectWindows(ctx) pre-warm uses the unbounded connect context, which can make Connect block longer than expected if the probe stalls. Consider running the probe with a small derived timeout (while still respecting ctx cancellation).
	// Pre-warm Windows detection using the connect context. This avoids
	// context.Background() probes on every first IsWindows() call.
	c.detectWindows(ctx)

protocol/ssh/connection.go:667

  • The _ = inF line is effectively a no-op and makes it unclear whether inF is intentionally unused or accidentally ignored. Prefer removing the parameter if not needed, or using it directly (e.g. for the FD) so the signature reflects actual behavior.
	_ = inF // kept for signature clarity; caller passes it as input
	return func() { _ = term.Restore(stdinFD, old) }, nil

Comment thread service.go Resolved
Comment thread protocol/ssh/connection.go Resolved
Comment thread protocol/ssh/connection.go Resolved
Comment thread protocol/openssh/connection.go Resolved
Comment thread protocol/ssh/connection.go Resolved
Comment thread client.go Resolved
@kke
kke force-pushed the context-tweaks branch from 94506ee to c5f22d2 Compare May 26, 2026 13:35
@kke
kke requested a review from Copilot May 26, 2026 13:35

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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Comment thread protocol/ssh/connection.go Resolved
Comment thread .golangci.yml Resolved
Comment thread client_test.go Resolved
@kke
kke requested a review from Copilot May 26, 2026 13:53
@kke
kke force-pushed the context-tweaks branch from c5f22d2 to c96d8c8 Compare May 26, 2026 13:53
@kke
kke marked this pull request as ready for review May 26, 2026 13:56

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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Comment thread protocol/openssh/connection.go Resolved

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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Comment thread protocol/ssh/connection.go Resolved

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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Comment thread service.go Resolved
@kke kke changed the title Context timeout fallbacks fix: context propagation in service lifecycle, ExecInteractive and Windows detection May 26, 2026
@kke
kke force-pushed the context-tweaks branch from b17e3f4 to e2d14c7 Compare May 26, 2026 17:14
@kke
kke requested a review from Copilot May 26, 2026 17:15

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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Comment thread protocol/ssh/connection.go Resolved
Comment thread protocol/localhost/connection.go Resolved
kke added 5 commits May 26, 2026 22:47
Service.Start/Stop/Restart/Enable/Disable/DaemonReload previously had no
default timeout, so passing context.Background() could block indefinitely.
Add withServiceTimeout() that applies a 2-minute fallback when the caller
provides no deadline.  Existing deadlines are preserved unchanged.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Change InteractiveExecer.ExecInteractive to accept context.Context as
its first parameter. All four protocol implementations now honour
cancellation:
- SSH: goroutine closes the session when ctx is done
- OpenSSH: passes ctx directly to StartProcess
- WinRM: passes ctx to RunWithContextWithInput
- Localhost: goroutine kills the process when ctx is done

Client.ExecInteractive updated to match. v2 is unreleased so the
interface break is acceptable.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
SSH and OpenSSH connections previously triggered the Windows probe lazily
on the first IsWindows() call, which used context.Background()+10s
regardless of the caller's context.

Refactor:
- Extract detectWindows(ctx) from IsWindows() in both SSH and OpenSSH
- Call detectWindows(ctx) at the end of Connect() so the probe uses the
  connect context for cancellation
- IsWindows() retains the context.Background()+10s fallback for callers
  that check without calling Connect() first
- OpenSSH Connect: replace defer-unlock with explicit unlocks to avoid
  deadlock (StartProcess acquires the same mutex)

Also fix goroutine leak in ExecInteractive (SSH and Localhost):
watchDone channel now stops the ctx-watcher goroutine when the
session/process finishes normally, preventing it from leaking.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
- Apply withServiceTimeout to SetEnvironment, ScriptPath, IsRunning, Logs
- Bound the detectWindows pre-warm probe to 10s via prewarmWindows helper
- Pre-warm Windows detection in connectViaBastion path too
- Fix setupInteractivePTY to use inF.Fd() instead of os.Stdin.Fd()
- Rename GetSize result vars to width/height for clarity in RequestPty call
- Synchronize c.isWindows access in openssh with controlMutex
- Skip caching detectWindows result when probe ctx is cancelled
- Add ExecInteractive context-forwarding tests to client_test.go

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
- Apply withServiceTimeout to SetEnvironment, ScriptPath, IsRunning, Logs
- Bound the detectWindows pre-warm probe to 10s via prewarmWindows helper
- Pre-warm Windows detection in connectViaBastion path too
- Fix setupInteractivePTY to use inF.Fd() instead of os.Stdin.Fd()
- Rename GetSize result vars to width/height for clarity in RequestPty call
- Synchronize c.isWindows access in openssh with controlMutex
- Only cache detectWindows result when probe actually started (err == nil)
- Skip caching detectWindows result when probe ctx is cancelled
- Add ExecInteractive context-forwarding tests to client_test.go

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>

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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.

@kke
kke merged commit f10084b into main May 26, 2026
15 checks passed
@kke
kke deleted the context-tweaks branch May 26, 2026 20:05
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