Skip to content

feat: add command timeouts#32

Merged
salazarsebas merged 2 commits intoScaffoldRust:mainfrom
akintewe:feat/command-timeouts
Aug 8, 2025
Merged

feat: add command timeouts#32
salazarsebas merged 2 commits intoScaffoldRust:mainfrom
akintewe:feat/command-timeouts

Conversation

@akintewe
Copy link
Contributor

@akintewe akintewe commented Aug 5, 2025

📝 Description

Add timeout enforcement to the commandExecutor utility to prevent long-running or infinite commands from hanging the backend. This is critical for security and reliability, as Rust compilation and testing can be resource-intensive. By setting a 30-second timeout, we ensure the backend remains responsive and aligns with the project's security requirements.

🚀 Changes Made

  • ✅ Implement executeCommand function with 30-second default timeout
  • ✅ Add CommandTimeoutError with clear error messaging
  • ✅ Use setTimeout and child.kill() for timeout enforcement
  • ✅ Capture stdout/stderr before timeout occurs
  • ✅ Ensure proper cleanup of child processes to prevent resource leaks
  • ✅ Add executeCommandWithTimeout convenience wrapper
  • ✅ Create comprehensive TypeScript interfaces for type safety

🗂️ Files Added

apps/backend/src/utils/
└── commandExecutor.ts

🧪 Testing

  • ✅ Tested timeout behavior with sleep commands (5s and 40s scenarios)
  • ✅ Verified normal command execution remains unaffected
  • ✅ Confirmed stdout/stderr capture works before timeout
  • ✅ Validated proper process cleanup and resource management
  • Test Result: Command timeout functionality is working correctly!

🔒 Security Benefits

  • Prevents backend hanging from infinite or long-running commands
  • Ensures responsive API even during heavy Rust compilation
  • Protects against potential DoS scenarios
  • Aligns with project's 30-second timeout security requirement

💡 Usage Example

import { executeCommand, CommandTimeoutError } from './utils/commandExecutor';

try {
  const result = await executeCommand('cargo', ['build', '--release']);
  console.log('Build successful:', result.stdout);
} catch (error) {
  if (error instanceof CommandTimeoutError) {
    console.error('Build timed out after 30 seconds');
  }
}

🔗 Closes

Closes #30

📋 Checklist

  • Code follows project conventions
  • No linting errors
  • Timeout functionality tested and verified
  • Proper error handling implemented
  • Resource cleanup ensures no memory leaks
  • TypeScript interfaces provide type safety

Add timeout enforcement to commandExecutor utility to prevent long-running commands from hanging the backend.

- Implement executeCommand function with 30-second default timeout
- Add CommandTimeoutError with clear error messaging
- Use setTimeout and child.kill() for timeout enforcement
- Capture stdout/stderr before timeout occurs
- Ensure proper cleanup of child processes
- Add executeCommandWithTimeout convenience wrapper

Tested timeout behavior with sleep commands to verify functionality works correctly.
- Format commandExecutor.ts to match project code style
- Ensure consistent formatting across the codebase
- All files now pass Prettier checks
Copy link
Member

@salazarsebas salazarsebas left a comment

Choose a reason for hiding this comment

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

LGTM

@salazarsebas salazarsebas merged commit 1901256 into ScaffoldRust:main Aug 8, 2025
1 check passed
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.

Configure Command Timeouts [backend]

2 participants