Fix MCP cleanup trap scoping issue causing exit errors#73
Open
fletchgqc wants to merge 1 commit into
Open
Conversation
This fixes bash "unbound variable" errors that occur when ClaudeBox
exits with MCP servers configured. The issue was caused by EXIT traps
not having access to local variables from the function where they're
defined.
Changes:
- Simplified cleanup_mcp_files() to only use mcp_temp_files array
- Added user_mcp_file to mcp_temp_files array for proper cleanup
- Added parameter expansion safety (${mcp_temp_files[@]:-})
- Removed direct variable access in cleanup function
The cleanup now relies entirely on the mcp_temp_files array which is
accessible to the EXIT trap, rather than trying to access function-
scoped local variables that are out of scope when the trap executes.
Fixes the "user_mcp_file: unbound variable" error that occurred on
every ClaudeBox exit when MCP servers were configured.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThis PR refactors the MCP cleanup process by consolidating all temporary file paths into a single shared array and updating the EXIT trap to safely iterate over that array, removing direct variable references and eliminating unbound variable errors on exit. Sequence diagram for MCP temp file cleanup on EXIT trapsequenceDiagram
participant BashScript
participant "cleanup_mcp_files()"
BashScript->>"cleanup_mcp_files()": EXIT trap triggered
"cleanup_mcp_files()"->>"mcp_temp_files array": Iterate over files
"cleanup_mcp_files()"->>BashScript: Remove each temp file if exists
Class diagram for MCP temp file tracking and cleanupclassDiagram
class BashScript {
+mcp_temp_files : array
+user_mcp_file : string
+project_mcp_file : string
+cleanup_mcp_files()
}
BashScript : cleanup_mcp_files() uses mcp_temp_files
BashScript : user_mcp_file added to mcp_temp_files
BashScript : project_mcp_file (tracked via temp_project_file in mcp_temp_files)
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- Ensure mcp_temp_files is explicitly initialized (e.g. mcp_temp_files=()) before use to avoid potential unbound array warnings under set -u.
- Consider extending the trap to include INT and TERM so that cleanup runs on all termination paths, not just EXIT.
- You may want to move the cleanup_mcp_files function and its trap setup outside run_claudebox_container to avoid redefining them on every invocation and keep cleanup logic centralized.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Ensure mcp_temp_files is explicitly initialized (e.g. mcp_temp_files=()) before use to avoid potential unbound array warnings under set -u.
- Consider extending the trap to include INT and TERM so that cleanup runs on all termination paths, not just EXIT.
- You may want to move the cleanup_mcp_files function and its trap setup outside run_claudebox_container to avoid redefining them on every invocation and keep cleanup logic centralized.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
fogXploit
added a commit
to fogXploit/claudebox2.0
that referenced
this pull request
Oct 24, 2025
Fixes bash 'unbound variable' errors that occur when ClaudeBox exits
with MCP servers configured. The issue was caused by EXIT traps not
having access to local variables from the function where they're defined.
Changes:
- Simplified cleanup_mcp_files() to only use mcp_temp_files array
- Added user_mcp_file to mcp_temp_files array for proper cleanup
- Added parameter expansion safety (${mcp_temp_files[@]:-})
- Removed direct variable access in cleanup function
The cleanup now relies entirely on the mcp_temp_files array which is
accessible to the EXIT trap, rather than trying to access function-
scoped local variables that are out of scope when the trap executes.
This eliminates the 'user_mcp_file: unbound variable' error that
occurred on every ClaudeBox exit when MCP servers were configured,
ensuring clean exits with proper resource cleanup.
Test Results: All 71 tests pass ✓
Based on: RchGrav#73
Original Author: Claude <claude@example.com>
fogXploit
added a commit
to fogXploit/claudebox2.0
that referenced
this pull request
Oct 24, 2025
Updated documentation to reflect: - Implementation of PR RchGrav#78 (array expansion safety for macOS) - Implementation of PR RchGrav#73 (MCP cleanup trap scoping fix) - Status of all 11 open PRs from original repository - Prioritization and recommendations for remaining PRs - Next steps: PR RchGrav#70, RchGrav#67, RchGrav#74 ready to implement This ensures the next Claude instance has complete context about: - What's been done - What's still to do - Which PRs to skip/defer - Current test results (71/71 passing)
thieso2
added a commit
to thieso2/claudebox
that referenced
this pull request
Oct 24, 2025
Applies critical fixes from RchGrav/claudebox to enable ClaudeBox to run properly on macOS Tahoe (Sequoia 15.x): - Fix unbound variable errors on macOS (PR RchGrav#78) - Add :- operator to all array expansions for Bash 3.2 compatibility - Prevents "unbound variable" errors with set -u flag - Fix MCP cleanup trap scoping issue (PR RchGrav#73) - Consolidate MCP temp files into trap-accessible array - Eliminates exit errors when MCP servers are configured - Fix Python profile error handling (PR RchGrav#74) - Add proper error handling without masking failures - Implement broken symlink detection and auto-recovery - Ensure Bash 3.2 compatibility in docker-entrypoint - Fix awk newline error in Dockerfile substitution (PR RchGrav#55) - Replace awk-based substitution with pure bash processing - Handles multi-line LABEL and PROFILE_INSTALLATIONS content - Fixes "awk: newline in string" error Modified files: - build/docker-entrypoint: Python profile error handling - lib/*.sh: Array expansion fixes across all library modules - main.sh: Dockerfile template substitution fix - tooling/profiles/rust.sh: Array expansion fix 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.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.
Fixes #72
Summary
This PR fixes the bash "unbound variable" errors that occur when ClaudeBox exits with MCP servers configured. The issue was caused by EXIT traps not having access to local variables from the function where they're defined.
Changes Made
cleanup_mcp_files()to only usemcp_temp_filesarrayuser_mcp_filetomcp_temp_filesarray for proper cleanup tracking${mcp_temp_files[@]:-}) to handle empty arraysTechnical Details
The core issue was that EXIT traps execute in a different context where function-local variables are not accessible:
The fix ensures all temporary files are tracked in the
mcp_temp_filesarray which is accessible to the EXIT trap, eliminating the scoping issue entirely.Testing
bash -n lib/docker.sh)Impact
The fix eliminates a bash scoping anti-pattern and makes the cleanup process more predictable and maintainable.
Summary by Sourcery
Fix trap scoping issue in cleanup_mcp_files by consolidating MCP temp files into a single array accessible to the EXIT trap, eliminating unbound variable errors and restoring exit code 0.
Bug Fixes:
Enhancements: