Skip to content

fix: prevent uncaught exceptions in core node event handlers#5438

Merged
knolleary merged 2 commits intonode-red:masterfrom
Dennis-SEG:fix/harden-nodes-against-uncaught-exceptions
Jan 26, 2026
Merged

fix: prevent uncaught exceptions in core node event handlers#5438
knolleary merged 2 commits intonode-red:masterfrom
Dennis-SEG:fix/harden-nodes-against-uncaught-exceptions

Conversation

@Dennis-SEG
Copy link
Copy Markdown
Contributor

@Dennis-SEG Dennis-SEG commented Jan 16, 2026

This PR adds try-catch blocks and null checks to event handlers in core nodes to prevent uncaught exceptions from crashing the Node-RED runtime.

Problem

Several core nodes have event handlers (on('data'), on('message'), etc.) that process external data without try-catch protection. When malformed data or unexpected errors occur in these handlers, the exceptions propagate up and crash the entire Node-RED process.

Solution

Added defensive error handling to the following nodes:

TCP (31-tcpin.js)

  • Wrapped all on('data') handlers in try-catch:
    • TcpIn client mode
    • TcpIn server mode
    • TcpGet request handler

UDP (32-udp.js)

  • Wrapped on('message') handler in try-catch

Exec (90-exec.js)

  • Wrapped stdout and stderr on('data') handlers in try-catch

WebSocket (22-websocket.js)

  • Wrapped send() loop in handleEvent() with try-catch

MQTT (10-mqtt.js)

  • Added null check for packet parameter in subscriptionHandler()
  • Wrapped subscription handler callback in try-catch
  • Added null check for mpacket.properties

Impact

These changes ensure that errors in data processing are logged via node.error() instead of crashing the runtime. This improves stability when dealing with:

  • Malformed network data
  • Invalid encoding parameters
  • Unexpected null/undefined values
  • Errors in downstream processing

  • Bugfix (non-breaking change which fixes an issue)

Checklist

  • I have read the contribution guidelines
  • I have run npm run test to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

Added try-catch blocks and null checks to event handlers in core nodes
to prevent uncaught exceptions from crashing the Node-RED runtime.

Changes per node:

**TCP (31-tcpin.js)**
- Wrapped all `on('data')` handlers in try-catch (TcpIn client/server, TcpGet)

**UDP (32-udp.js)**
- Wrapped `on('message')` handler in try-catch

**Exec (90-exec.js)**
- Wrapped stdout/stderr `on('data')` handlers in try-catch

**WebSocket (22-websocket.js)**
- Wrapped send() loop in handleEvent() with try-catch

**MQTT (10-mqtt.js)**
- Added null check for packet parameter in subscriptionHandler()
- Wrapped subscription handler callback in try-catch
- Added null check for mpacket.properties

Without these protections, malformed data or unexpected errors in async
event handlers could cause uncaught exceptions that crash the entire
Node-RED process.
@dceejay
Copy link
Copy Markdown
Member

dceejay commented Jan 19, 2026

Look fine to me. Nothing really to object to as far as I can see. Simple tests working fine.

@knolleary knolleary merged commit 620da24 into node-red:master Jan 26, 2026
5 checks 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.

3 participants