FIX: Add timeouts to MySQL detection to prevent hanging

The calculate_server_memory_capacity() function was hanging during
optimization levels 1-4 because of unguarded MySQL queries.

Fixed:
1. Added 2-second timeout to MySQL queries in detect_mysql_memory_usage()
   - Lines 1395-1396: buffer_pool_mb and max_connections queries
   - These would hang indefinitely if MySQL was slow or unresponsive

2. Added 5-second timeout to detect_mysql_memory_usage() call
   - Line 1008 in calculate_server_memory_capacity()
   - Prevents the entire function from blocking

This allows optimization levels 1-5 to execute without hanging
when MySQL is unavailable or slow to respond.
This commit is contained in:
Developer
2026-04-20 22:01:38 -04:00
parent 729583581c
commit a4adf9a398
+5 -5
View File
@@ -1001,11 +1001,11 @@ calculate_server_memory_capacity() {
done <<< "$user_domains" done <<< "$user_domains"
done <<< "$users" done <<< "$users"
# Add MySQL memory usage to total # Add MySQL memory usage to total (with timeout to prevent hanging)
local mysql_memory_mb=0 local mysql_memory_mb=0
local mysql_status local mysql_status
local mysql_info local mysql_info
mysql_info=$(detect_mysql_memory_usage 2>/dev/null) mysql_info=$(timeout 5 detect_mysql_memory_usage 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$mysql_info" ]; then if [ $? -eq 0 ] && [ -n "$mysql_info" ]; then
mysql_memory_mb=$(echo "$mysql_info" | cut -d'|' -f3 || echo "0") mysql_memory_mb=$(echo "$mysql_info" | cut -d'|' -f3 || echo "0")
mysql_status=$(echo "$mysql_info" | cut -d'|' -f4) mysql_status=$(echo "$mysql_info" | cut -d'|' -f4)
@@ -1391,9 +1391,9 @@ detect_mysql_memory_usage() {
local max_connections=150 # Default local max_connections=150 # Default
if command -v mysql >/dev/null 2>&1; then if command -v mysql >/dev/null 2>&1; then
# Try to query MySQL directly # Try to query MySQL directly (with 2 second timeout to prevent hanging)
buffer_pool_mb=$(mysql -Nse "SELECT ROUND(@@innodb_buffer_pool_size/1024/1024)" 2>/dev/null || echo "0") buffer_pool_mb=$(timeout 2 mysql -Nse "SELECT ROUND(@@innodb_buffer_pool_size/1024/1024)" 2>/dev/null || echo "0")
max_connections=$(mysql -Nse "SELECT @@max_connections" 2>/dev/null || echo "150") max_connections=$(timeout 2 mysql -Nse "SELECT @@max_connections" 2>/dev/null || echo "150")
fi fi
# If we couldn't get it from MySQL, try my.cnf # If we couldn't get it from MySQL, try my.cnf