Skip to content

fix: register process error handlers before startup awaits to prevent silent ECONNRESET crash - #2

Draft
obrienma with Copilot wants to merge 3 commits into
masterfrom
copilot/fix-connection-reset-error
Draft

obrienma with Copilot wants to merge 3 commits into
masterfrom
copilot/fix-connection-reset-error

Conversation

Copilot AI commented Apr 1, 2026

Copy link
Copy Markdown

Both worker and server processes were crashing with an unhandled ECONNRESET from amqplib's TCP socket during startup, producing a bare Node.js stack dump with no actionable output.

Root causes

  • worker.ts had no uncaughtException/unhandledRejection handlers at all — any rejection from startWorker() (including amqp.connect() failing mid-handshake) was unhandled.
  • server.ts had both handlers, but registered them after the await connectDb() → connectQueue() chain. A rejection during startup fired before the handlers existed.
// Before: handlers registered after the awaits that could reject
await connectDb();
await connectQueue();          // ← ECONNRESET here = no handler yet
// ...
process.on("uncaughtException", ...);   // ← too late
process.on("unhandledRejection", ...);  // ← too late

Changes

  • src/server.ts — moved both process error handlers to immediately after Fastify() construction, before any await. Removed the now-duplicate late registrations.
  • src/processing/worker.ts — added uncaughtException and unhandledRejection handlers before the top-level await startWorker().
  • vitest.config.ts — added env block with the minimum required env vars (MONGO_URI, RABBITMQ_URL, EXCHANGE_NAME, QUEUE_NAME, DEAD_LETTER_QUEUE, MONGO_DB_NAME) so config.ts Zod validation passes at import time during tests. All I/O is mocked; these values never reach real infrastructure.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • fastdl.mongodb.org
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node ./postinstall.js (dns block)
    • Triggering command: /opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node --experimental-import-meta-resolve --require /home/REDACTED/work/EventHorizon/EventHorizon/node_modules/vitest/suppress-warnings.cjs --conditions node --conditions development /home/REDACTED/work/EventHorizon/EventHorizon/node_modules/vitest/dist/workers/forks.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

[worker] > event-horizon@1.0.0 worker
[worker] > tsx src/processing/worker.ts
[worker]
[server]
[server] > event-horizon@1.0.0 dev
[server] > tsx src/server.ts
[server]
[worker] [db] connected to "eventhorizon"
[worker] [repository] indexes ensured
[worker] node:internal/modules/run_main:107
[worker] triggerUncaughtException(
[worker] ^
[worker]
[worker] Error: read ECONNRESET
[worker] at TCP.onStreamRead (node:internal/stream_base_commons:216:20) {
[worker] errno: -104,
[worker] code: 'ECONNRESET',
[worker] syscall: 'read'
[worker] }
[worker]
[worker] Node.js v24.11.1
[worker] npm run worker exited with code 1
[server] [ws] server registered at /ws
[server] [db] connected to "eventhorizon"
[server] [repository] indexes ensured
[server] node:internal/modules/run_main:107
[server] triggerUncaughtException(
[server] ^

The user has attached the following file paths as relevant context:

  • .github/copilot-instructions.md
  • CLAUDE.md
[Chronological Review: The conversation began with the user executing commands to run a worker and server for the Event Horizon project. The worker encountered an error (ECONNRESET) during execution, which caused it to exit with code 1. The user then attempted to cancel a previous command, "Delegate to cloud agent." The conversation continued with the user executing the same worker and server commands, leading to the same error being reported again.]

[Intent Mapping: The user requested to run the worker and server for the Event Horizon project, indicated by the command "[worker] > event-horizon@1.0.0 worker" and "[server] > event-horizon@1.0.0 dev." The user also attempted to cancel a command related to delegating tasks to a cloud agent.]

[Technical Inventory: The user is working with Node.js (version 24.11.1) and TypeScript (tsx) for the Event Horizon project. The commands executed involve starting a worker and server, connecting to a database named "eventhorizon," and ensuring repository indexes.]

[Code Archaeology: The specific files mentioned include "src/processing/worker.ts" and "src/server.ts." The worker file is responsible for processing tasks, while the server file handles incoming requests and WebSocket connections.]

[Progress Assessment: The worker was successfully initiated but encountered an error (ECONNRESET) that caused it to exit. The server appears to be running, as indicated by the successful connection to the database and registration of the WebSocket server.]

[Context Validation: The conversation captures the user's attempts to run the Event Horizon project components and the resulting errors, which are critical for troubleshooting and continuing development.]

[Recent Commands Analysis: The last agent commands executed were:

  1. "[worker] > tsx src/processing/worker.ts" - This command was intended to start the worker process.
  2. "[server] > tsx src/server.ts" - This command was intended to start the server process.
    The tool results indicated that the worker connected to the database but encountered an ECONNRESET error, causing it to exit. The server successfully connected to the database and registered a WebSocket server.]
1. Conversation Overview: - Primary Objectives: The user aimed to run the Event Horizon project's worker and server components, as indicated by the commands executed. The user also attempted to cancel a previous command related to delegating tasks. - Session Context: The conversation involved executing commands to start the worker and server, encountering errors, and attempting to manage command execution. - User Intent Evolution: The user's focus shifted from executing commands to addressing errors encountered during execution.
  1. Technical Foundation:
  • Node.js: Version 24.11.1, used for running the Event Horizon project.
  • TypeScript (tsx): Utilized for executing TypeScript files in the project.
  • Database: "eventhorizon," connected successfully by both the worker and server.
  1. Codebase Status:
  • File Name: src/processing/worker.ts
    • Purpose: Handles processing tasks for the Event Horizon project.
    • Current State: Encountered an ECONNRESET error during execution.
  • File Name: src/server.ts
    • Purpose: Manages incoming requests and WebSocket connections.
    • Current State: Successfully connected to the database and registered the WebSocket server.
  1. Problem Resolution:
  • Issues Encountered: The worker process faced an ECONNRESET error, causing it to exit unexpectedly.
  • Solutions Implemented: No solutions have been applied yet; the user is in the process of troubleshooting the error.
  • Debugging Context: The user is actively trying to understand and resolve the ECONNRESET error affecting the worker.
  1. Progress Tracking:
  • Completed Tasks: The server has been successfully started and connected to the database.
  • Partially Complete Work: The worker has been initiated but is currently failing due to an error.
  • Validated Outcomes: The server's connection to the ...

Created from VS Code.

Copilot AI and others added 2 commits April 1, 2026 04:35
Agent-Logs-Url: https://github.com/obrienma/EventHorizon/sessions/ed3266a5-799a-44b1-8911-977e8c3729dc

Co-authored-by: obrienma <3357771+obrienma@users.noreply.github.com>
…worker and server

- worker.ts: register uncaughtException/unhandledRejection handlers before startWorker() so ECONNRESET during AMQP startup does not crash the process silently
- server.ts: move existing process error handlers to before all top-level awaits so they guard connectDb/connectQueue startup failures
- vitest.config.ts: add required env vars to vitest config so config.ts Zod validation passes during unit tests

Agent-Logs-Url: https://github.com/obrienma/EventHorizon/sessions/ed3266a5-799a-44b1-8911-977e8c3729dc

Co-authored-by: obrienma <3357771+obrienma@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ECONNRESET error in worker process fix: register process error handlers before startup awaits to prevent silent ECONNRESET crash Apr 1, 2026
Copilot AI requested a review from obrienma April 1, 2026 04:40
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