fix: isolate detached command context from parent cancellation#179
fix: isolate detached command context from parent cancellation#179mason5052 wants to merge 3 commits intovxcontrol:masterfrom
Conversation
Detached terminal commands (detach=true) inherit the parent context. When the parent context is canceled (e.g., agent delegation timeout), the detached goroutine's ctx.Done() fires and kills the background command, even though it has its own timeout. Use context.WithoutCancel(ctx) for the detached goroutine. This preserves context values (tracing, logging) but prevents parent cancellation from propagating. The command's own timeout via context.WithTimeout in getExecResult continues to work. Non-detached commands are unchanged and still respect parent cancellation. Closes vxcontrol#176 Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
There was a problem hiding this comment.
Pull request overview
Fixes detached terminal commands (detach=true) being prematurely canceled when the parent context is canceled (e.g., agent delegation timeout), by isolating the detached goroutine’s context from parent cancellation while preserving context values.
Changes:
- Create a detached context via
context.WithoutCancel(ctx)for background execution. - Use the detached context when waiting for exec results in the detached goroutine.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| resultChan := make(chan execResult, 1) | ||
| detachedCtx := context.WithoutCancel(ctx) | ||
|
|
||
| go func() { | ||
| output, err := t.getExecResult(ctx, createResp.ID, timeout) | ||
| output, err := t.getExecResult(detachedCtx, createResp.ID, timeout) |
There was a problem hiding this comment.
The new detached execution behavior (using context.WithoutCancel) isn’t covered by tests. Since other tools in this package have unit tests, consider adding a terminal ExecCommand test that cancels the parent ctx and asserts the detached goroutine continues (e.g., via a mock dockerClient that would otherwise abort when ctx.Done() closes), while the command still stops on its own timeout.
There was a problem hiding this comment.
Valid suggestion. However, writing a meaningful test for this requires mocking the Docker client (ContainerExecCreate, ContainerExecAttach, etc.) and verifying goroutine behavior under cancellation -- which is fairly involved and would require test infrastructure that does not exist yet in this package. For a 2-line bugfix that uses an idiomatic Go pattern (context.WithoutCancel), I think the current verification (manual code review + go vet + build) is proportionate. Happy to add tests in a follow-up PR if the maintainers prefer.
Validates the core fix: detached goroutine must survive parent context cancellation (context.WithoutCancel behavior). TestExecCommandDetachSurvivesParentCancel: - Starts detach=true command, cancels parent ctx after quick return - Asserts goroutine does NOT see cancellation (ctxWasCanceled=false) - This test would FAIL without context.WithoutCancel TestExecCommandNonDetachRespectsParentCancel: - Starts detach=false command, cancels parent ctx after 200ms - Asserts command DOES fail with context error - Ensures WithoutCancel was NOT applied to non-detach path Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
Rename mockTermLogProvider to contextTestTermLogProvider in terminal_context_test.go to prevent redeclaration error when both PR vxcontrol#179 and PR vxcontrol#181 are merged into the same package. Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
Description of the Change
Problem
Detached terminal commands (
detach=true) inherit the parent context. When the parent context is canceled (e.g., agent delegation timeout after ~2.5 minutes), the detached goroutine'sctx.Done()fires ingetExecResultand kills the background command, even though the command has its own timeout (300-1200 seconds).This makes agent delegation effectively unusable for commands that take more than ~2.5 minutes (which is most penetration testing tasks like
nmapscans).Closes #176
Solution
Use
context.WithoutCancel(ctx)(Go 1.21+) for the detached goroutine. This:context.WithTimeoutingetExecResult)Non-detached commands are unchanged -- they still use the original
ctxand respect parent cancellation as before.Type of Change
Areas Affected
Testing and Verification
Test Configuration
Test Steps
context.WithoutCancelis available in Go 1.24 (introduced in Go 1.21)ExecCommand) still uses originalctxt.tlp.PutMsg) are preserved throughWithoutCancelgetExecResult'sdefer cancel()still prevents goroutine leakSecurity Considerations
No security impact. The change only affects context propagation for background commands. Commands still terminate when their own timeout expires.
Checklist
go fmtandgo vet