Skip to content

Add process monitoring and graceful shutdown handlers - #216

Merged
tobie merged 1 commit into
mainfrom
claude/eager-meitner-1cn7jm
Sep 24, 2026
Merged

tobie merged 1 commit into
mainfrom
claude/eager-meitner-1cn7jm

Conversation

@tobie

@tobie tobie commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR adds comprehensive process monitoring and graceful shutdown capabilities to the application, including memory usage tracking, process event handlers, and improved logging for debugging production issues.

Key Changes

  • Memory monitoring: Added memory() function to capture process memory usage (RSS, heap, external, array buffers) in MB for inclusion in log lines
  • Process event handlers: Implemented installProcessHandlers() to catch and log uncaught exceptions, unhandled promise rejections, process warnings, and shutdown signals (SIGTERM, SIGINT, SIGHUP)
  • Graceful shutdown: Added signal handlers that trigger server shutdown with a 5-second timeout to prevent hanging processes
  • Request-level logging: Added debug-level logging for incoming requests with memory snapshots, excluding health checks and loopback traffic to reduce noise
  • Periodic memory snapshots: Added 60-second interval memory usage logging to help identify memory leaks in production
  • Server error handling: Added error handler for server startup failures
  • Enhanced startup logging: Improved startup log to include memory usage and startup duration

Implementation Details

  • Memory values are rounded to nearest MB for readability
  • Loopback requests (127.0.0.1, ::1, ::ffff:127.0.0.1) and /health endpoint are excluded from request logging to reduce log noise
  • Shutdown timeout of 5000ms prevents indefinite hangs if connections don't close gracefully
  • All process handlers log with memory snapshots for correlation with resource usage
  • Memory snapshots use unref() on intervals to prevent them from keeping the process alive

https://claude.ai/code/session_01CnAkjjVdTENcYvZSiGWWVA

Ported from specref (#965):
- lib/logger.js: memory() gives a snapshot of process.memoryUsage() in MB,
  and installProcessHandlers() logs uncaught exceptions, unhandled
  rejections, process warnings, SIGTERM/SIGINT/SIGHUP (closing the server
  first) and the process exit, with the memory usage where it helps.
- index.js: memory usage on startup, a "memory usage" line every minute,
  and a fatal line on a server error.
- lib/app.js: at debug level, a "request received" line with the memory
  usage, skipping /health and loopback requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CnAkjjVdTENcYvZSiGWWVA
@tobie
tobie merged commit 588db83 into main Sep 24, 2026
1 check passed
@tobie
tobie deleted the claude/eager-meitner-1cn7jm branch September 24, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants