fix: guard undefined entity on the incoming-message handlers#2922
Open
krobipd wants to merge 1 commit into
Open
fix: guard undefined entity on the incoming-message handlers#2922krobipd wants to merge 1 commit into
krobipd wants to merge 1 commit into
Conversation
handleMessage and onZigbeeEvent run for every incoming zigbee message, bound
fire-and-forget on the herdsman 'message' event and the controller 'event'/'msg'
events. resolveEntity() can return undefined for a message from a device that
does not resolve (unknown/leaving device, DB race), and neither path guarded it:
- handleMessage built `options: entity.options || {}` without checking entity
(the line just above already guards `entity &&`), throwing on undefined entity.
- onZigbeeEvent dereferenced `entity.device` at the top, before its try block.
Since both handlers are async and their callers don't await them, the throw
became an unhandled promise rejection — fatal under compact mode. Guard the
entity (onZigbeeEvent returns early; handleMessage uses `entity?.options ?? {}`)
and wrap handleMessage in a top-level try/catch.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Collaborator
|
This PR will not go through. Please rework:
Alternatively, this will return onto the stack of planned reworks and be done when I get to it. A. |
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.
Problem
The adapter occasionally crashes on incoming zigbee traffic (and under compact mode can take the controller with it) — part of the "randomly wobbly" class of failures. Both handlers that run for every incoming message dereference the resolved entity without guarding it, and
resolveEntity()can returnundefinedfor a message from a device that does not resolve (a device that is leaving/unknown, or a DB race). Both handlers are async and bound fire-and-forget (herdsmanmessage, controllerevent/msg), so the throw becomes an unhandled promise rejection.ZigbeeController.handleMessagebuildsoptions: entity.options || {}— the line just above already guardsentity &&, but this one dereferencesentity.optionsunconditionally →TypeError: Cannot read properties of undefinedon an unresolved entity.StatesController.onZigbeeEventdereferencesentity.deviceat the very top, before itstryblock.Fix
handleMessage: guard withentity?.options ?? {}and wrap the body in a top-leveltry/catch(log instead of crashing — it is the central message entry point).onZigbeeEvent: return early whenentity/entity.deviceis missing (an event for an unresolvable entity cannot be processed; everything below needs the device).No behaviour change for resolved entities. The missing guard is visible directly in the code (the preceding line guards
entity, this one does not;event()forwards the entity unconditionally), independent of reproducibility.Testing
node --checkpasses on both files.undefined-entity path is now handled instead of throwing.