Fix node_removed event sent before BasicInformation Leave event#439
Closed
markvp wants to merge 1 commit intomatter-js:mainfrom
Closed
Fix node_removed event sent before BasicInformation Leave event#439markvp wants to merge 1 commit intomatter-js:mainfrom
markvp wants to merge 1 commit intomatter-js:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Defers nodeDecommissioned emission to a microtask so that synchronous node events (notably BasicInformation Leave) are relayed to WebSocket clients before node_removed, ensuring node_removed is always the final event for a node (fixes #75).
Changes:
- Wrap
nodeDecommissionedemission inqueueMicrotask()to reorder event delivery across the same call stack.
b10e4b0 to
29808cb
Compare
When another controller removes a node from the fabric, defer nodeDecommissioned emission via queueMicrotask so that any events emitted synchronously in the same call stack (e.g. the BasicInformation Leave event) are relayed to WebSocket clients before node_removed. Also clean up internal state (#nodes, #customClusterPoller) in the decommissioned handler so that nodes removed by an external controller are cleaned up identically to the WebSocket remove_node command path. Wrap the deferred microtask body in try/catch to prevent uncaught exceptions from crashing the process if a listener throws. Add unit tests for the decommission event ordering pattern. Fixes matter-js#75 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
29808cb to
466b1ca
Compare
Apollon77
requested changes
Mar 24, 2026
Collaborator
Apollon77
left a comment
There was a problem hiding this comment.
Honestly ... this PR is difficult because this test does not at all test the reality but just takes assumptions. So when we need to very this then via enhancing the integration test.
And even then I am unsure if I like this solution because this is still hacky and only works in very defined considerations that might not match the reality
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.
Summary
nodeDecommissionedemission viaqueueMicrotask()so that events emitted synchronously in the same call stack (e.g. the BasicInformation Leave event that triggers decommissioning from another controller) are relayed to WebSocket clients beforenode_removed#nodes,#customClusterPoller) in thedecommissionedhandler so that nodes removed by an external controller are cleaned up identically to the WebSocketremove_nodecommand pathFixes #75
Details
When another controller (e.g. Apple Home) removes a node from the fabric, matter.js's
PairedNodeprocesses the Leave event in this order within a single call stack:_triggerEventUpdate()fires the cluster-level Leave event →Peers.#onLeave()→decommissioned.emit()(synchronous)eventTriggered.emit()fires the PairedNode-level eventSince
ControllerCommandHandlerrelayed both synchronously,node_removedreached WebSocket clients before the Leavenode_event. By deferringnodeDecommissionedto a microtask, the synchronouseventTriggeredcompletes first.Internal state cleanup
Previously, the
decommissionedevent handler only emittednodeDecommissionedto notify WebSocket clients but did not clean up#nodesor#customClusterPoller. This meant nodes removed by an external controller left stale entries in server state. The handler now mirrors the cleanup performed bydecommissionNode().Offline removal via WebSocket command
When a node is removed via the WebSocket
remove_nodecommand while the device is offline, no Leave event is emitted — onlynode_removed. This is correct behaviour becauseevents.decommissionedis always emitted regardless of the removal path:Peers.#onLeave()→decommissioned.emit()PairedNode.decommission()→decommissioned.emit()PairedNode.close(true)→decommissioned.emit()The Leave event is purely informational for clients — the server performs no special processing on it.
node_removedis the authoritative signal that cleanup is complete.Test plan
npm run formatpassesnpm run lintpassesnpm run buildpassesnpm test— ws-controller (3/3 new + existing), ws-client (43/43) pass; integration test failures are pre-existing on mainnode_event(Leave) arrives beforenode_removedwhen both fire synchronouslynode_removedis emitted even when no Leave event fires🤖 Generated with Claude Code