mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-06 07:29:42 +02:00
fix: async I/O, map iteration bug, unified cleanup, Zod validation, error handling
- Convert sync readFileSync/statSync to async in subagent-watcher.ts to unblock event loop on hot paths (transcript reads, liveness checks) - Fix Map mutation during iteration in closeSessionLogViewerWindows and closeSessionImagePopups (collect IDs first, then iterate to close) - Unify session cleanup into shared _cleanupSessionData() method called from both closeSession() and session:deleted handler to prevent leaks - Add Zod validation schemas for 20+ API routes that used raw type casts - Add consecutive error tracking (5 errors/60s triggers exit for systemd restart) and SIGHUP handler for SSH disconnect safety Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+19
-1
@@ -14,12 +14,29 @@ import { program } from './cli.js';
|
||||
// In web mode, we should NOT exit on transient errors — log and continue
|
||||
const isWebMode = process.argv.includes('web');
|
||||
|
||||
// Track consecutive unhandled errors in web mode — restart after too many
|
||||
let consecutiveErrors = 0;
|
||||
const MAX_CONSECUTIVE_ERRORS = 5;
|
||||
const ERROR_RESET_MS = 60000; // Reset counter after 1 minute of no errors
|
||||
let errorResetTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
function trackError(): void {
|
||||
consecutiveErrors++;
|
||||
if (errorResetTimer) clearTimeout(errorResetTimer);
|
||||
errorResetTimer = setTimeout(() => { consecutiveErrors = 0; }, ERROR_RESET_MS);
|
||||
|
||||
if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
|
||||
console.error(`[FATAL] ${MAX_CONSECUTIVE_ERRORS} consecutive unhandled errors — exiting for systemd restart`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle uncaught errors
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error('Uncaught exception:', err.message);
|
||||
if (isWebMode) {
|
||||
// Log full stack trace for debugging but keep the server running
|
||||
console.error('[RECOVERED] Server continuing after uncaught exception:', err.stack);
|
||||
trackError();
|
||||
} else {
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -29,6 +46,7 @@ process.on('unhandledRejection', (reason) => {
|
||||
console.error('Unhandled rejection:', reason);
|
||||
if (isWebMode) {
|
||||
console.error('[RECOVERED] Server continuing after unhandled rejection');
|
||||
trackError();
|
||||
} else {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user