fix: isolate detached command context from parent cancellation#179
Merged
asdek merged 3 commits intovxcontrol:feature/next_releasefrom Mar 11, 2026
Merged
Conversation
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.
7 tasks
mason5052
added a commit
to mason5052/pentagi
that referenced
this pull request
Mar 6, 2026
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>
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>
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>
018a2fb to
1e2cf11
Compare
This was referenced Mar 10, 2026
Contributor
|
tnx for the PR! I agree that the tests are redundant here, but let them stay, it's nice to see that the behavior is confirmed by tests )) |
36 tasks
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.
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