System Overload Prevention

Documentation for Developers

System Overload Prevention Guide

Last Updated: October 19, 2025

Overview

This document describes the measures implemented to prevent system overload, file descriptor exhaustion, and build process issues in our monorepo.


Root Cause Analysis

Primary Issues Identified:

  1. File Descriptor Exhaustion (EMFILE: too many open files)

    • Angular language servers consuming 4,000+ file descriptors per process
    • Multiple TypeScript compiler instances running simultaneously
    • Insufficient cleanup of child processes and stdio streams
  2. Memory Leaks

    • Stuck build processes not timing out
    • Child process stdio streams not being destroyed
    • Temporary files accumulating
  3. Resource Contention

    • Too many parallel build tasks
    • NX daemon spawning excessive concurrent operations

Implemented Solutions

1. Build Script Optimizations (build-angular-library.js)

Process Management

// ✅ Added explicit process cleanup
- Timeout protection (5 minutes per command)
- Explicit stdio stream destruction
- Proper error handling with cleanup

Features:

Temporary File Management

// ✅ Automatic cleanup in finally blocks
- tsconfig cleanup
- SCSS temp directory cleanup
- Source pollution cleanup

2. NX Configuration (nx.json)

Conservative Parallelism Settings

{
  "tasksRunnerOptions": {
    "default": {
      "options": {
        "maxParallel": 2,           // ✅ Max 2 parallel builds
        "maxMemory": 8192,          // ✅ 8GB memory limit
        "daemonMaxConcurrency": 4,  // ✅ Limited daemon concurrency
        "useDaemonProcess": true    // ✅ Reuse daemon process
      }
    }
  },
  "parallel": 2  // ✅ Global parallel limit
}

Why These Settings?

3. Cleanup Script Optimizations (cleanup-all-source-pollution.js)

Batched File Operations

// ✅ Process files in batches of 10
const BATCH_SIZE = 10;
for (let i = 0; i < allFiles.length; i += BATCH_SIZE) {
  const batch = allFiles.slice(i, i + BATCH_SIZE);
  await Promise.all(batch.map(async (file) => {
    // Process file
  }));
}

Benefits:

4. Process Monitoring Script (monitor-processes.sh)

Automatic Detection and Cleanup

# ✅ Monitors processes with excessive open files
# ✅ Auto-kills Angular language servers with > 3,000 open files
# ✅ Detects stuck NX/TSC processes running > 15 minutes
# ✅ Reports system load average

Usage

Daily Development

Before Starting Work

# Check system health
yarn monitor:processes

# Clean up if needed
yarn cleanup:stuck-processes

During Development

# If builds start slowing down
yarn monitor:processes

# Emergency cleanup (kills stuck processes)
yarn cleanup:stuck-processes

# Reset NX cache
yarn nx reset

Build Commands (Already Optimized)

# Single library (safe)
yarn nx build my-lib --skip-nx-cache

# Multiple libraries (parallelism limited to 2)
yarn nx run-many --target=build --all --parallel=1

# Full monorepo build (controlled)
yarn build:all

Monitoring System Health

Check Open File Descriptors

yarn monitor:processes

Check System Load

uptime

Check Memory Usage

top -o MEM

Emergency Procedures

System Overloaded (Load > 10)

# 1. Kill stuck Angular language servers
pkill -f 'typescript-language-features'

# 2. Kill stuck NX/TSC processes
pkill -f 'nx|tsc'

# 3. Reset NX cache
yarn nx reset

# 4. Restart IDE
# Close and reopen your IDE (Cursor/VSCode)

EMFILE Errors

# 1. Check which processes have too many open files
yarn monitor:processes

# 2. Clean up
yarn cleanup:stuck-processes

# 3. Increase system limits (macOS)
sudo launchctl limit maxfiles 65536 200000
ulimit -n 65536

Performance Metrics

Before Optimizations

After Optimizations


Best Practices

DO ✅

DON'T ❌


Troubleshooting

Builds Are Slow

  1. Check system load: uptime
  2. Monitor processes: yarn monitor:processes
  3. Clean up: yarn cleanup:stuck-processes
  4. Reduce parallelism: --parallel=1

EMFILE Errors

  1. Run: yarn monitor:processes
  2. Kill excessive processes
  3. Increase limits: ulimit -n 65536
  4. Restart IDE

Out of Memory

  1. Check: top -o MEM
  2. Close unused applications
  3. Kill stuck processes: yarn cleanup:stuck-processes
  4. Restart IDE

Stuck Builds (> 15 minutes)

  1. Cancel with Ctrl+C
  2. Run: yarn cleanup:stuck-processes
  3. Rebuild: yarn nx build my-lib --skip-nx-cache

Related Documentation


Scripts Reference

Command Description
yarn monitor:processes Monitor system resources and detect issues
yarn cleanup:stuck-processes Kill stuck language servers and reset NX
yarn nx reset Clear NX cache
yarn build:all Optimized full monorepo build

Status: ✅ Production Ready
Maintainer: Build System Team
Contact: @fguijarro