Skip to content

fix: isolate detached command context from parent cancellation#179

Merged
asdek merged 3 commits intovxcontrol:feature/next_releasefrom
mason5052:fix/detached-cmd-context-isolation
Mar 11, 2026
Merged

fix: isolate detached command context from parent cancellation#179
asdek merged 3 commits intovxcontrol:feature/next_releasefrom
mason5052:fix/detached-cmd-context-isolation

Conversation

@mason5052
Copy link
Contributor

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's ctx.Done() fires in getExecResult and 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 nmap scans).

Closes #176

Solution

Use context.WithoutCancel(ctx) (Go 1.21+) for the detached goroutine. This:

  • Preserves context values (tracing spans, logrus fields, observability data)
  • Prevents parent cancellation from propagating to the background command
  • Keeps the command's own timeout working (context.WithTimeout in getExecResult)

Non-detached commands are unchanged -- they still use the original ctx and respect parent cancellation as before.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Areas Affected

  • Core Services (Backend API)

Testing and Verification

Test Configuration

  • PentAGI Version: master
  • Go Version: 1.24

Test Steps

  1. Confirmed context.WithoutCancel is available in Go 1.24 (introduced in Go 1.21)
  2. Verified non-detached path (last line of ExecCommand) still uses original ctx
  3. Verified detached goroutine now uses isolated context with own timeout
  4. Confirmed context values (observability, logging via t.tlp.PutMsg) are preserved through WithoutCancel
  5. Verified getExecResult's defer cancel() still prevents goroutine leak

Security Considerations

No security impact. The change only affects context propagation for background commands. Commands still terminate when their own timeout expires.

Checklist

  • My code follows the project's coding standards
  • All new and existing tests pass
  • I have run go fmt and go vet
  • Security implications considered
  • Changes are backward compatible

Copilot AI review requested due to automatic review settings March 6, 2026 00:45
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

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>
@mason5052 mason5052 force-pushed the fix/detached-cmd-context-isolation branch from 018a2fb to 1e2cf11 Compare March 10, 2026 15:35
@asdek asdek changed the base branch from master to feature/next_release March 11, 2026 13:10
@asdek asdek merged commit d17ad17 into vxcontrol:feature/next_release Mar 11, 2026
@asdek
Copy link
Contributor

asdek commented Mar 11, 2026

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 ))

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.

Agent delegation context canceled — detached commands inherit parent context

3 participants