Skip to content

fix(broker): log fatal errors, batch abuse saves in one transaction, add LOG_LEVEL - #7

Merged
Elektr0Vodka merged 1 commit into
Dutch-MeshCore:mainfrom
marcelverdult:fix/broker-stability
Oct 20, 2026
Merged

Elektr0Vodka merged 1 commit into
Dutch-MeshCore:mainfrom
marcelverdult:fix/broker-stability

Conversation

@marcelverdult

Copy link
Copy Markdown

Why

The collector we run for meshcore-analyzer.eu (image built from 6e17036) exited with code 1 about 2.5 times a day for two weeks (38 restarts between 2026-10-05 and 2026-10-20), and failed its health check for minutes before each exit. The reason was never visible: there is no uncaughtException / unhandledRejection handler, and at ~180 log lines/s the default 3 x 10 MB json-file rotation held about two hours, so every stack trace was gone before anyone looked. Details: EU-Meshcore-Analyzer issue 1193.

What

  • Crash visibility (src/server.ts): process-level uncaughtException and unhandledRejection handlers log [FATAL] with the full stack, then exit(1) as before (the restart policy brings the broker back). error listeners on the HTTP server and the WebSocketServer: before listening (e.g. EADDRINUSE) stays fatal; after listening an error such as an accept-time EMFILE is logged (throttled to one per 5 s) instead of becoming an uncaught exception.
  • Timers cannot kill the process: the periodic abuse-state save and the stats publish are wrapped, so a failed tick logs and the next one runs.
  • Abuse-state save in one transaction (src/abuse-detector.ts): statements prepared once at init, the whole save in one db.transaction, journal_mode = WAL and synchronous = NORMAL at open. Before, every client was its own INSERT OR REPLACE with its own fsync, all synchronous on the event loop. Schema and loaded data are unchanged. The shutdown save uses the same path.
  • LOG_LEVEL (src/logger.ts, README, .env.example): debug|info|warn|error. Unset = everything is logged exactly as today. At info the per-publish and per-connection chatter is dropped ([STREAM] close()/destroy() called, Publish authorized, Published JWT payload, [PUBLISH] Internal ->, PING/PONG, connection details). Warnings, errors, denials, disconnect reasons and abuse events stay. The [AUTH] Token input line (a full credential) and the token hex dump are debug-only.

Checks

  • npx tsc --noEmit clean on main and on this branch.
  • Smoke test with Node 22 (the Dockerfile's version), once with LOG_LEVEL unset and once with info: ready line, TCP connect to the port succeeds, SIGTERM exits 0 with [ABUSE] Shutdown complete; the chatter lines appear only without LOG_LEVEL.
  • Forced throw in a timer (temporary, not committed): [FATAL] Uncaught exception (uncaughtException): Error: forced test throw with stack, exit code 1.
  • Abuse save, 700 synthetic clients (100 hashes, 300 timestamps each), on macOS: 245 to 442 ms before, 73 to 229 ms after. The gain on Linux with a real fsync is expected to be larger but was not measured.

Behaviour notes

  • WAL adds -wal and -shm files next to the abuse DB; the data volume must be writable (it already is for the DB itself).
  • An exception thrown inside an aedes event handler still exits 1, now with a stack trace.

…add LOG_LEVEL

- add uncaughtException/unhandledRejection handlers that log the stack and exit(1)
- add http/ws server error listeners (accept errors are logged, listen errors stay fatal)
- wrap periodic timers (abuse save, stats publish) so a failed tick only logs
- save trust states in one transaction with prepared statements, WAL + synchronous=NORMAL
- add LOG_LEVEL (debug|info|warn|error, default logs everything); JWT token input only at debug
@Elektr0Vodka
Elektr0Vodka merged commit 7864f22 into Dutch-MeshCore:main Oct 20, 2026
1 check passed
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