Repository navigation
fix: context propagation in service lifecycle, ExecInteractive and Windows detection - #351
Merged
Merged
Conversation
kke
force-pushed
the
context-tweaks
branch
2 times, most recently
from
May 26, 2026 12:51
15a2e78 to
dcd3446
Compare
There was a problem hiding this comment.
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
ctxhas no deadline. - Change
ExecInteractiveacross client/protocols to acceptcontext.Contextand 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
falsedue 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.
There was a problem hiding this comment.
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
_ = inFline 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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds fallbacks when passed in context does not have a timeout so that service functions don't block indefinetly.
Also adds context to
ExecInteractiveand makes theIsWindows()use a 10 second timeout.gomodguardv1 is disabled in.golangci.ymlbecause it nags about it being deprecated over v2 (which I assume is enabled via theall)Part of rig v2 final stretch, breaking API is not a problem as rig v2 has not been published yet.