Fix context propagation and goroutine panic recovery in privileged MCP tools - #49031
Conversation
…vileged.go Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Preserves request context values for privileged MCP log subprocesses while safely containing watcher goroutine panics.
Changes:
- Uses
context.WithoutCancel(ctx)before applying the subprocess timeout. - Adds panic recovery and logging to the context-watcher goroutine.
Show a summary per file
| File | Description |
|---|---|
pkg/cli/mcp_tools_privileged.go |
Improves subprocess context propagation and goroutine safety. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Medium
|
✅ Test Quality Sentinel completed test quality analysis. Warning threat detection engine error DetailsThe threat detection engine failed to produce results. Review the workflow run logs for details. No test files were added or modified in this PR. Test Quality Sentinel skipped. PR #49031 only modified production code (pkg/cli/mcp_tools_privileged.go). |
|
✅ Design Decision Gate 🏗️ completed the design decision gate check. No ADR enforcement needed: PR does not have the implementation label and has ≤100 new lines of code in business logic directories (11 additions detected). |
|
🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅ |
|
✅ PR Code Quality Reviewer completed the code quality review. |
There was a problem hiding this comment.
The two changes are correct and well-scoped:
context.WithoutCancel(ctx)(Go 1.21+) properly preserves context values while detaching the gateway deadline — no compatibility issue given go 1.26.5.- The
defer/recoverin the goroutine is well-formed and consistent with the codebase pattern. - The cancellation guard at
ctx.Err() == context.Canceledcorrectly avoids propagatingDeadlineExceededfrom the gateway into the subprocess.
No actionable issues found.
🧵 Reviewed using Impeccable skills by Impeccable Skills Reviewer · sonnet46 · 12.2 AIC · ⌖ 4.77 AIC · ⊞ 5.3K
There was a problem hiding this comment.
🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · sonnet46 · 20.7 AIC · ⌖ 4.81 AIC · ⊞ 7K
Comment /matt to run again
There was a problem hiding this comment.
Verdict: COMMENT — no blocking issues found
Solid, well-scoped fix. context.WithoutCancel(ctx) correctly preserves context values while detaching the RPC deadline, and the watcher goroutine's cancellation-forwarding logic (only propagating context.Canceled, ignoring DeadlineExceeded) matches the stated intent and has no leak — it always terminates via one of the two Done() channels since subCtx carries a timeout.
💡 Notes considered but not blocking
- The
defer recover()added to the watcher goroutine is defensive but effectively inert: the goroutine body is just aselectover two channels and a call tosubCancel()(an idempotent context cancel func) — none of these can panic under normal Go semantics. It's harmless boilerplate, not a bug, so not requesting a change here. - No race conditions:
subCancel()may be invoked concurrently from both the deferred call and the goroutine, butcontext.CancelFuncis documented as safe for concurrent/repeated calls. - Comment accuracy: the updated comment correctly describes the new behavior and matches the code.
🔎 Code quality review by PR Code Quality Reviewer · aut00 · 37.6 AIC · ⌖ 4.5 AIC · ⊞ 7.8K
Comment /review to run again
Two correctness issues in the privileged MCP logs tool's subprocess context management:
context.Background()dropped all context values, and the context-watcher goroutine had no panic containment.Changes
context.WithoutCancel(ctx)instead ofcontext.Background()— preserves context values (trace IDs, etc.) from the incoming request context while still detaching the deadline, so the MCP gateway's 60 s RPC deadline doesn't kill the subprocess prematurely.Panic recovery in goroutine — adds a top-level
defer/recoverto the context-watcher goroutine, consistent with the pattern used elsewhere in the codebase, so unexpected panics are logged rather than crashing the process.