Files
igny8/CONTAINER-RESTART-DEBUGGING.md
2025-12-15 07:14:06 +00:00

5.4 KiB

Container Restart and Auto-Logout Debugging Setup

Overview

Added comprehensive logging to track container restarts and automatic user logouts.

Changes Made

1. Container Lifecycle Logging

Backend Container (backend/container_startup.sh)

  • Logs container startup time, hostname, and PID
  • Detects and logs container restarts (by checking for previous PID file)
  • Logs environment configuration (Python version, Django settings, DB host)
  • Warns when restarts are detected and suggests checking Docker logs for SIGTERM signals
  • Integrated into Dockerfile as ENTRYPOINT

Frontend Container (frontend/container_startup.sh)

  • Logs container startup time, hostname, and PID
  • Detects and logs container restarts
  • Logs Node/NPM versions and Vite configuration
  • Checks for git directory presence and warns about file watching
  • Shows last git commit when detected
  • Integrated into Dockerfile.dev as ENTRYPOINT

2. Vite File Watching Fix (frontend/vite.config.ts)

ROOT CAUSE IDENTIFIED: usePolling: true was watching ALL files including .git directory, causing container restarts on git commits.

Fix Applied:

watch: {
  usePolling: true,
  ignored: [
    '**/node_modules/**',
    '**/.git/**',           // CRITICAL: Ignore git directory
    '**/dist/**',
    '**/build/**',
    '**/.vscode/**',
    '**/.idea/**',
  ],
  interval: 1000, // Poll every 1 second instead of default
}

3. Auto-Logout Logging (backend/igny8_core/auth/middleware.py)

Added detailed logging for all automatic logout scenarios:

Session Contamination - Account ID Mismatch

logger.warning(
    f"[AUTO-LOGOUT] Session contamination: account_id mismatch. "
    f"Session={stored_account_id}, Current={request.account.id}, "
    f"User={request.user.id}, Path={request.path}, IP={request.META.get('REMOTE_ADDR')}"
)

Session Contamination - User ID Mismatch

logger.warning(
    f"[AUTO-LOGOUT] Session contamination: user_id mismatch. "
    f"Session={stored_user_id}, Current={request.user.id}, "
    f"Account={request.account.id if request.account else None}, "
    f"Path={request.path}, IP={request.META.get('REMOTE_ADDR')}"
)

Account/Plan Validation Failures

logger.warning(
    f"[AUTO-LOGOUT] Account/plan validation failed: {error}. "
    f"User={request.user.id}, Account={getattr(request, 'account', None)}, "
    f"Path={request.path}, IP={request.META.get('REMOTE_ADDR')}"
)

4. Logging Configuration (backend/igny8_core/settings.py)

Added two new loggers:

  • auth.middleware - Captures all authentication and auto-logout events
  • container.lifecycle - Captures container startup/restart events

Both log to console (captured by Docker logs).

How to Use

Viewing Container Restart Logs

# Check backend container logs for restart events
docker logs igny8_backend 2>&1 | grep "CONTAINER-STARTUP\|RESTART"

# Check frontend container logs for restart events
docker logs igny8_frontend 2>&1 | grep "CONTAINER-STARTUP\|RESTART"

# Monitor in real-time
docker logs -f igny8_backend

Viewing Auto-Logout Logs

# Check for automatic logout events
docker logs igny8_backend 2>&1 | grep "AUTO-LOGOUT"

# Filter by specific logout reason
docker logs igny8_backend 2>&1 | grep "AUTO-LOGOUT.*contamination"
docker logs igny8_backend 2>&1 | grep "AUTO-LOGOUT.*validation failed"

# See recent logout events with context
docker logs --since 1h igny8_backend 2>&1 | grep -A2 -B2 "AUTO-LOGOUT"

Correlating Events

# See both container restarts and logouts together
docker logs --since 2h igny8_backend 2>&1 | grep -E "CONTAINER-STARTUP|AUTO-LOGOUT|SIGTERM"

# Check if git commits correlate with restarts
git log --since="2 hours ago" --format="%ai %s" && \
docker logs --since 2h igny8_frontend 2>&1 | grep "CONTAINER-STARTUP"

Next Steps to Deploy

  1. Rebuild Docker images:

    cd /data/app/igny8/backend
    docker build -t igny8-backend:latest -f Dockerfile .
    
    cd /data/app/igny8/frontend
    docker build -t igny8-frontend-dev:latest -f Dockerfile.dev .
    
  2. Restart containers:

    cd /data/app/igny8
    docker compose -f docker-compose.app.yml down
    docker compose -f docker-compose.app.yml up -d
    
  3. Verify logging is working:

    docker logs igny8_backend 2>&1 | head -30
    docker logs igny8_frontend 2>&1 | head -30
    
  4. Test git commit trigger (should NOT restart now):

    cd /data/app/igny8
    echo "test" >> README.md
    git add README.md
    git commit -m "test commit"
    
    # Wait 5 seconds and check - containers should NOT restart
    sleep 5
    docker ps --filter "name=igny8" --format "{{.Names}}: {{.Status}}"
    

Expected Outcomes

  1. Git commits should NO LONGER trigger container restarts because .git is now ignored by Vite's file watcher
  2. Every container restart will be logged with timestamp and reason
  3. Every automatic logout will be logged with user ID, account ID, reason, path, and IP address
  4. You can correlate restarts with git operations to verify the fix is working

Troubleshooting

If containers still restart after git commits:

  1. Check if the new images were built and deployed
  2. Verify the vite.config.ts changes are present in the running container
  3. Check Docker logs to see what triggered the restart
  4. Look for HMR messages in frontend logs mentioning .git files