[object Object]

← back to Commercialrealestate

serve.js: handle EADDRINUSE gracefully to stop launchd crash-restart storm

0a55b7c39e26bd8f4680988328cc7359d62ed3f2 · 2026-07-24 08:35:33 -0700 · Steve Abrams

An unhandled listen EADDRINUSE threw a full stack trace and instantly
respawned under KeepAlive, writing a 1.18M-line / 36.8MB error log when
two instances raced for port 9911. Now log one line and exit cleanly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit 0a55b7c39e26bd8f4680988328cc7359d62ed3f2
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Fri Jul 24 08:35:33 2026 -0700

    serve.js: handle EADDRINUSE gracefully to stop launchd crash-restart storm
    
    An unhandled listen EADDRINUSE threw a full stack trace and instantly
    respawned under KeepAlive, writing a 1.18M-line / 36.8MB error log when
    two instances raced for port 9911. Now log one line and exit cleanly.
    
    Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---
 scripts/serve.js | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/scripts/serve.js b/scripts/serve.js
index 1710f38..001ccf7 100644
--- a/scripts/serve.js
+++ b/scripts/serve.js
@@ -1216,4 +1216,15 @@ app.use('/', express.static(path.join(ROOT, 'public')));
 
 mountClaudeChat(app, { path: '/api/claude-chat', surface: 'sfv-cre', defaultModel: 'claude-sonnet-4-6', systemExtra: SYSTEM_EXTRA });
 
-app.listen(PORT, '127.0.0.1', () => console.log(`SFV CRE viewer + chat -> http://127.0.0.1:${PORT}`));
+const server = app.listen(PORT, '127.0.0.1', () => console.log(`SFV CRE viewer + chat -> http://127.0.0.1:${PORT}`));
+server.on('error', (err) => {
+  if (err && err.code === 'EADDRINUSE') {
+    // Another instance already owns the port (launchd KeepAlive race / manual start).
+    // Log ONE concise line and exit cleanly instead of throwing a full stack trace,
+    // which previously spun launchd into a crash-restart loop (1.18M-line err log).
+    console.error(`serve.js: port ${PORT} already in use — another instance is serving; exiting quietly.`);
+    process.exit(0);
+  }
+  console.error(`serve.js: listen error: ${err && err.message}`);
+  process.exit(1);
+});

← 5cfe1b7 auto-save: 2026-07-24T07:26:03 (1 files) — data/alerts-state  ·  back to Commercialrealestate  ·  auto-save: 2026-07-24T11:57:36 (1 files) — data/latest.json faa7715 →