feat: Implement improved temp file management for CommandExecutor (fixes #142) #177
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.
Summary
This PR implements improved temporary file management for the CommandExecutor to address the race conditions, incomplete cleanup, and limited error recovery identified in issue #142.
Key Changes
🆕 New TempFileManager Class
🔧 CommandExecutor Integration
⚙️ Configuration Settings
Added new environment variables for fine-tuning temp file management:
TEMP_CLEANUP_RETRY_ATTEMPTS- Number of retry attempts for failed cleanupTEMP_CLEANUP_RETRY_DELAY- Base delay between retry attemptsORPHAN_CLEANUP_INTERVAL- Interval between orphaned file cleanup runsORPHAN_FILE_MAX_AGE- Maximum age before files are considered orphanedTEMP_DIR_MAX_SIZE_MB- Maximum temp directory size monitoringTEMP_FILE_CREATION_RETRY_ATTEMPTS- Retry attempts for temp file creationProblem Solved
✅ Race Conditions: Eliminated cleanup skipping from monitor tasks
✅ Incomplete Cleanup: Robust retry mechanisms with exponential backoff
✅ Orphan Detection: Automatic detection and cleanup of abandoned files
✅ Error Recovery: Fallback temp directories and enhanced error handling
✅ Monitoring: Comprehensive metrics for operational visibility
Testing
Files Modified
mcp_tools/command_executor/temp_file_manager.py- New TempFileManager classmcp_tools/command_executor/executor.py- Integration with TempFileManagerconfig/env.template- New configuration settingsBackward Compatibility
✅ Fully backward compatible - existing CommandExecutor API unchanged
✅ Graceful fallbacks for missing configuration settings
✅ No breaking changes to existing functionality
Closes #142