LOW: Add timeouts to subprocess.run() calls in container_manager.py #41

Open
opened 2026-02-20 14:50:23 +02:00 by Koko210 · 0 comments
Owner

Problem

bot/utils/container_manager.py uses subprocess.run() to execute Docker commands (docker start, docker stop, docker inspect) without specifying a timeout parameter.

If the Docker daemon hangs, becomes unresponsive, or a container gets stuck in a state transition, these subprocess calls will block indefinitely. Since they are called from the bot's async event loop (via run_in_executor or directly), this can freeze the entire bot.

Affected Calls

All subprocess.run() calls in container_manager.py, including:

  • Starting STT container (docker start miku-stt)
  • Starting TTS container (docker start miku-rvc-api)
  • Stopping containers (docker stop)
  • Health check inspections (docker inspect)

Additional Issue

There is also a duplicate 'return False' statement at lines 186-187 of container_manager.py that should be cleaned up.

Proposed Solution

Add reasonable timeouts to all subprocess.run() calls:

# For start/stop operations (may take a while for large containers)
result = subprocess.run(
    ['docker', 'start', container_name],
    capture_output=True, text=True,
    timeout=60  # 60 seconds should be more than enough
)

# For inspect/status checks (should be near-instant)
result = subprocess.run(
    ['docker', 'inspect', container_name],
    capture_output=True, text=True,
    timeout=10  # 10 seconds for a status check
)

Wrap in try/except subprocess.TimeoutExpired to handle the timeout gracefully:

try:
    result = subprocess.run(..., timeout=60)
except subprocess.TimeoutExpired:
    logger.error(f'Docker command timed out for {container_name}')
    return False

Also remove the duplicate return False at line 187.

Impact

  • Risk: None (adds safety, no behavior change in normal operation)
  • Effort: Trivial (add timeout parameter + try/except to each call)
  • Benefit: Prevents bot from hanging if Docker daemon is unresponsive

Files Affected

  • bot/utils/container_manager.py
## Problem bot/utils/container_manager.py uses subprocess.run() to execute Docker commands (docker start, docker stop, docker inspect) without specifying a timeout parameter. If the Docker daemon hangs, becomes unresponsive, or a container gets stuck in a state transition, these subprocess calls will block indefinitely. Since they are called from the bot's async event loop (via run_in_executor or directly), this can freeze the entire bot. ### Affected Calls All subprocess.run() calls in container_manager.py, including: - Starting STT container (docker start miku-stt) - Starting TTS container (docker start miku-rvc-api) - Stopping containers (docker stop) - Health check inspections (docker inspect) ### Additional Issue There is also a duplicate 'return False' statement at lines 186-187 of container_manager.py that should be cleaned up. ## Proposed Solution Add reasonable timeouts to all subprocess.run() calls: # For start/stop operations (may take a while for large containers) result = subprocess.run( ['docker', 'start', container_name], capture_output=True, text=True, timeout=60 # 60 seconds should be more than enough ) # For inspect/status checks (should be near-instant) result = subprocess.run( ['docker', 'inspect', container_name], capture_output=True, text=True, timeout=10 # 10 seconds for a status check ) Wrap in try/except subprocess.TimeoutExpired to handle the timeout gracefully: try: result = subprocess.run(..., timeout=60) except subprocess.TimeoutExpired: logger.error(f'Docker command timed out for {container_name}') return False Also remove the duplicate return False at line 187. ## Impact - Risk: None (adds safety, no behavior change in normal operation) - Effort: Trivial (add timeout parameter + try/except to each call) - Benefit: Prevents bot from hanging if Docker daemon is unresponsive ## Files Affected - bot/utils/container_manager.py
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Koko210/miku-discord#41