🔧 Implement robust cleanup system for Chromium process management

Major fixes for browser automation resource management:

- Chromium processes accumulating over time during automated trading
- Resource consumption growing after extended automation cycles
- Incomplete cleanup during analysis operations

 New Components:
- lib/enhanced-screenshot-robust.ts: Screenshot service with guaranteed cleanup
- lib/automated-cleanup-service.ts: Background process monitoring
- lib/auto-trading-service.ts: Comprehensive trading automation
- ROBUST_CLEANUP_IMPLEMENTATION.md: Complete documentation

- Finally blocks guarantee cleanup execution even during errors
- Active session tracking prevents orphaned browser instances
- Multiple kill strategies (graceful → force → process cleanup)
- Timeout protection prevents hanging cleanup operations
- Background monitoring every 30s catches missed processes

- lib/aggressive-cleanup.ts: Improved with multiple cleanup strategies
- app/api/enhanced-screenshot/route.js: Added finally block guarantees
- lib/automation-service.ts: Updated for integration

- validate-robust-cleanup.js: Implementation validation
- test-robust-cleanup.js: Comprehensive cleanup testing

The Chromium process accumulation issue is now resolved with guaranteed cleanup!
This commit is contained in:
mindesbunister
2025-07-24 08:39:26 +02:00
parent 91cc8baead
commit 5b156a0063
10 changed files with 1407 additions and 270 deletions

View File

@@ -263,8 +263,8 @@ class AggressiveCleanup {
console.log('🧹 Post-cycle cleanup triggered (analysis + decision complete)...')
// Wait for all browser processes to fully close
console.log('⏳ Waiting 3 seconds for all processes to close gracefully...')
await new Promise(resolve => setTimeout(resolve, 3000))
console.log('⏳ Waiting 5 seconds for all processes to close gracefully...')
await new Promise(resolve => setTimeout(resolve, 5000))
// Always run cleanup after complete automation cycle - don't check for active sessions
// since the analysis is complete and we need to ensure all processes are cleaned up
@@ -281,35 +281,42 @@ class AggressiveCleanup {
console.log(`🔍 Found ${chromiumProcesses.length} chromium processes for post-analysis cleanup`)
// In post-analysis cleanup, we're more aggressive since analysis is complete
// Try graceful shutdown first
for (const pid of chromiumProcesses) {
// Multiple cleanup strategies for thorough cleanup
const killCommands = [
// Graceful shutdown first
'pkill -TERM -f "chromium.*--remote-debugging-port" 2>/dev/null || true',
'pkill -TERM -f "chromium.*--user-data-dir" 2>/dev/null || true',
// Wait a bit
'sleep 3',
// Force kill stubborn processes
'pkill -KILL -f "chromium.*--remote-debugging-port" 2>/dev/null || true',
'pkill -KILL -f "chromium.*--user-data-dir" 2>/dev/null || true',
'pkill -KILL -f "/usr/lib/chromium/chromium" 2>/dev/null || true',
// Clean up zombies
'pkill -9 -f "chromium.*defunct" 2>/dev/null || true'
]
for (const command of killCommands) {
try {
console.log(`🔧 Attempting graceful shutdown of process ${pid}`)
await execAsync(`kill -TERM ${pid}`)
if (command === 'sleep 3') {
await new Promise(resolve => setTimeout(resolve, 3000))
} else {
await execAsync(command)
}
} catch (error) {
console.log(` Process ${pid} may already be terminated`)
// Ignore errors from kill commands
}
}
// Wait for graceful shutdown
await new Promise(resolve => setTimeout(resolve, 5000))
// Check which processes are still running and force kill them
const stillRunning = await this.findStillRunningProcesses(chromiumProcesses)
if (stillRunning.length > 0) {
console.log(`🗡️ Force killing ${stillRunning.length} stubborn processes`)
for (const pid of stillRunning) {
try {
await execAsync(`kill -9 ${pid}`)
console.log(`💀 Force killed process ${pid}`)
} catch (error) {
console.log(` Process ${pid} already terminated`)
}
}
// Check results
const remainingProcesses = await this.findChromiumProcesses()
if (remainingProcesses.length < chromiumProcesses.length) {
console.log(`✅ Cleanup successful: ${chromiumProcesses.length - remainingProcesses.length} processes terminated`)
} else {
console.log('✅ All processes shut down gracefully')
console.log(`⚠️ No processes were terminated, ${remainingProcesses.length} still running`)
}
// Clean up temp directories and shared memory