Commit 80b0653
committed
Fix: resolve logic bugs, resource leaks, and error handling issues in JavaScript lib/ (#1410)
Comprehensive review and fix of all JavaScript files under `lib/` that implement
the rclnodejs client library. Eight issues were identified and resolved.
### 1. Incorrect `instanceof` operator precedence (`lib/node.js`)
Changed `(!parameter) instanceof Parameter` to `!(parameter instanceof Parameter)`.
The original expression evaluated `!parameter` to a boolean first, which is never
an instance of `Parameter` — the validation was always `false`, silently accepting
invalid objects as parameter overrides.
### 2. Broken chained comparison in `validType()` (`lib/parameter.js`)
Changed `PARAMETER_NOT_SET <= parameterType <= PARAMETER_STRING_ARRAY` to two
separate comparisons joined with `&&`. JavaScript evaluates chained comparisons
as `(a <= b) <= c`, where `(a <= b)` produces a boolean (0 or 1) — the result
was always `true`, allowing any numeric value to pass type validation.
### 3. Missing `else if` in `_validArray()` (`lib/parameter.js`)
Changed three standalone `if` blocks to `else if` in the array element type
resolution. The conditions are mutually exclusive by design; without `else if`,
later branches could overwrite a correctly resolved `arrayElementType`.
### 4. Typo `_loggger` → `_logger` (`lib/lifecycle_publisher.js`)
Fixed triple-g typo in both the property declaration and usage site. The typo
was consistent so it didn't crash at runtime, but any code referencing the
standard `_logger` spelling would get `undefined`.
### 5. Duplicate getter/setter with `for...in` bug and missing unsubscribe (`lib/time_source.js`)
Removed the first duplicate `get/set isRosTimeActive` definition. It contained
two bugs: `for (const clock in ...)` iterated over array indices (strings), not
clock objects, making the property assignment a no-op; and the `else` branch
incorrectly set `this._node._clockSubscription` instead of
`this._clockSubscription`.
Additionally, the retained setter was missing cleanup logic when ROS time is
deactivated — it subscribed to the clock topic when enabled but never
unsubscribed when disabled. Added an `else` branch to destroy the clock
subscription (matching the pattern in `detachNode()`), preventing an orphaned
subscription from continuing to invoke `_clockCallback` unnecessarily.
### 6. Unhandled promise rejection in service callback (`lib/service.js`)
Added `.catch()` to the `Promise.resolve(this._callback(...)).then(...)` chain.
Without it, if the user's service callback threw or rejected, the rejection was
unhandled — potentially crashing the process or leaving clients hanging.
The error handler uses `Logging.getLogger('rclnodejs').error()` instead of
`debug()` so that service callback errors are visible through the ROS logging
system by default, without requiring the `DEBUG=rclnodejs:service` environment
variable.
### 7. TypeError on unknown distro ID (`lib/distro.js`)
Added null check before accessing `[0]` on the `Array.find()` result. Previously,
calling `getDistroName()` with an ID not in the map would throw
`TypeError: Cannot read properties of undefined`. It now returns `undefined`.
### 8. Feedback callback leak for rejected goals (`lib/action/client.js`)
Added cleanup of the `_feedbackCallbacks` map entry when a goal is rejected in
`processGoalResponse()`. Previously, the callback registered in `sendGoal()` was
never removed for rejected goals, causing unbounded map growth.
### Tests added
- `test/test-parameters.js` — 4 new tests:
- Rejects non-Parameter objects in parameterOverrides (validates issue 1)
- Rejects invalid numeric parameter types (validates issue 2)
- Accepts all known ParameterTypes
- Validates array parameter types (validates issue 3)
- `test/test-distro.js` — 1 new test:
- `getDistroName(99999)` returns `undefined` instead of throwing (validates issue 7)
Fix: #14091 parent 9339d69 commit 80b0653
10 files changed
Lines changed: 117 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
129 | 135 | | |
130 | 136 | | |
131 | 137 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
| 154 | + | |
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
864 | 864 | | |
865 | 865 | | |
866 | 866 | | |
867 | | - | |
868 | | - | |
869 | | - | |
| 867 | + | |
| 868 | + | |
870 | 869 | | |
871 | 870 | | |
872 | 871 | | |
| |||
923 | 922 | | |
924 | 923 | | |
925 | 924 | | |
926 | | - | |
927 | | - | |
| 925 | + | |
928 | 926 | | |
929 | | - | |
930 | | - | |
| 927 | + | |
931 | 928 | | |
932 | | - | |
933 | | - | |
| 929 | + | |
934 | 930 | | |
935 | 931 | | |
936 | 932 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
78 | | - | |
79 | | - | |
| 79 | + | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
89 | | - | |
90 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
91 | 95 | | |
92 | 96 | | |
93 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | 48 | | |
69 | 49 | | |
70 | 50 | | |
| |||
93 | 73 | | |
94 | 74 | | |
95 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
96 | 79 | | |
97 | 80 | | |
98 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
81 | 89 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
748 | 748 | | |
749 | 749 | | |
750 | 750 | | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
751 | 831 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
310 | | - | |
311 | | - | |
| 310 | + | |
312 | 311 | | |
313 | 312 | | |
314 | | - | |
| 313 | + | |
315 | 314 | | |
316 | 315 | | |
0 commit comments