fix: prevent uncaught exceptions in core node event handlers#5438
Merged
knolleary merged 2 commits intonode-red:masterfrom Jan 26, 2026
Merged
Conversation
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.
Member
|
Look fine to me. Nothing really to object to as far as I can see. Simple tests working fine. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)on('data')handlers in try-catch:UDP (
32-udp.js)on('message')handler in try-catchExec (
90-exec.js)on('data')handlers in try-catchWebSocket (
22-websocket.js)send()loop inhandleEvent()with try-catchMQTT (
10-mqtt.js)packetparameter insubscriptionHandler()mpacket.propertiesImpact
These changes ensure that errors in data processing are logged via
node.error()instead of crashing the runtime. This improves stability when dealing with:Checklist
npm run testto verify the unit tests pass