You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Erase stale mapped entries in refresh_dynamic_parameters - #415
refresh_dynamic_parameters() syncs mapped (__map_) parameters with their key arrays. Until now it only handled added keys. Removing a key had been a // TODO remove any destroyed dynamic parameters since #20: RemoveRuntimeParameter existed, but its output was commented out, and its template could not compile.
This PR implements the removal and fixes the bugs that showed up while testing it and in review.
Behaviour
After a key array such as joints changes, refresh_dynamic_parameters():
Added keys: declares their parameters, as before, and adds their entries to the maps with the values on the node, level by level, so a key gets its entry even when its child key array is empty.
Removed keys: erases their entries from the Params struct level by level, e.g. pid.joints_map, joints_map[...].dof_names_map, nested_dynamic.joints_map[...]...joints_map. A key goes even when its child map is empty, for example after dof_names was cleared.
Removed keys' parameters stay declared on the node. rclcpp refuses to undeclare statically typed parameters (cannot undeclare a statically typed parameter), and all mapped parameters are statically typed. Python behaves the same, so both languages match.
Keys added again: their entries get the values their parameters kept on the node.
Existing entries are left alone.update() keeps them current. update() runs in the node's on-set callback, before the node commits a value, so re-reading the node here could revert such a value.
Keys without an entry yet:update() skips their parameters, so no entry with default values for the other fields is created. The next refresh creates the entry with all values from the node. Until then, setting such a parameter only reaches the node.
Concurrent updates: a parameter update on another thread during a refresh is never reverted. Each attempt declares and syncs from one snapshot of the key arrays, and is stored only if nothing else stored since; otherwise it is repeated. update() and refresh serialize their read-modify-write on a new lock (update_mutex_ in C++, update_lock_ in Python). The user callback runs outside that lock.
No change: if no entry was added or erased, refresh doesn't touch the stamp, so is_old()/try_update_params() users don't copy the struct on every call. The examples call refresh on every timer tick.
Not from a parameter callback: there the node hasn't stored the new values yet, and rclcpp rejects declaring parameters. The doc comments and the README say so.
Changes
Python update() no longer fails for a key without an entry. Declaring a parameter runs the on-set callback. For a new key, Python's update() then called get_entry() on an entry that didn't exist yet, so adding a key and calling refresh_dynamic_parameters() always crashed:
AttributeError: 'Params' object has no attribute 'joint_new'
In both languages, update() now only updates entries that exist. C++ used to create one with operator[].
Example:nested_map gets a second field, weight. Its key array nested_map.entries keys no other map, which the tests for restored values need. The example READMEs list it.
Sync the maps in refresh_dynamic_parameters(). In a loop:
take a snapshot and declare the parameters of new keys;
run the rewritten remove_runtime_parameter templates on a copy: erase stale keys level by level before descending, add an entry for every key level by level, and read the node only for entries that are not in the snapshot;
store the result only if nothing stored since the snapshot.
Python finds and removes map entries through the module functions _map_entry_names() and _map_remove_entry(). As struct methods, they would collide with fields or keys of the same name. Python's get_params() reads params_ once and no longer writes to it, so a concurrent update can't get an old stamp.
README: documents refresh_dynamic_parameters() under "Mapped parameters", for C++ and Python. The Python example iterates the key array.
Tests
New consistency tests in example (C++), example_python and example_cmake_python:
An added key gets its parameter and entry.
A removed key is erased at all four nesting levels, other keys stay, and its parameters stay declared.
A key added again gets its entry back with the current value.
A key whose child map is empty is still erased, and gets its entry when it is added again.
A key added again in nested_map gets back the weight it kept on the node while its value is set.
A value update() stored before the node committed it survives a refresh.
A refresh without key changes doesn't make is_old() true.
New test_refresh_dynamic_parameters.py runs a generated Python module on a real rclpy node, with a definition where pid is the only map keyed by its array:
Values kept on the node are restored.
An update between refresh's snapshot and its store is kept.
A key added between declaring and syncing is declared before it is read.
get_params() keeps the stamp of an update made during its copy.
A value update() stored before the node committed it survives a refresh.
A key array and a key named entry_names work.
A refresh without changes keeps the stamp.
Each of these failed before the change that fixed it. The two cases where another thread changes parameters during a refresh are tested in Python only, by running that action at the start of the sync. A C++ version would need a test double for NodeParametersInterface; the C++ logic mirrors the Python one.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
C++ and Python refresh commits can overwrite successful concurrent parameter updates, while additional defects leave stale map entries, discard restored values, and cause Python name collisions.
This PR extends generated C++ and Python parameter listeners to remove stale mapped entries during refresh_dynamic_parameters(), while keeping their node parameters declared for reuse.
Changes:
Adds map-entry removal and restoration, with unchanged refreshes preserving the parameter stamp.
Fixes Python updates to create missing mapped entries and adds entry-listing and removal helpers.
Documents runtime key changes and adds consistency tests across three example packages.
File
Description
README.md
Documents runtime map refresh and entry iteration.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Unresolved callback and snapshot races can overwrite parameter updates, while map-recreation and declaration gaps can leave entries missing or cause refresh failures.
If a worker setting pid.a.p from 1 to 5 pauses in the user callback, update() has already published 5, but the node still holds 1. Refresh on another thread can snapshot that new stamp, read 1 from the node, and pass this check, leaving the listener at 1 after the setter commits 5—even though refresh was not called from a parameter callback. Coordinate reconciliation with node-transaction completion or protect pending mapped values until commit, and add a two-thread regression for this interleaving.
Re the review ("Refresh can overwrite a pending parameter update"): confirmed, refresh compared existing entries with the node, which may not have committed a value update() already stored. Fixed in 1a2acc8: refresh no longer re-reads existing entries, as update() keeps them current. It reads the node only for entries that are not in its snapshot. In turn, update() no longer creates a half-default entry for a key that has none yet; refresh creates it with all values from the node. test_refresh_keeps_a_value_the_node_has_not_committed_yet (Python) and RefreshKeepsAValueTheNodeHasNotCommittedYet (C++) call update() before the node commits, which is exactly this interleaving, and fail without the fix.
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
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.
refresh_dynamic_parameters()syncs mapped (__map_) parameters with their key arrays. Until now it only handled added keys. Removing a key had been a// TODO remove any destroyed dynamic parameterssince #20:RemoveRuntimeParameterexisted, but its output was commented out, and its template could not compile.This PR implements the removal and fixes the bugs that showed up while testing it and in review.
Behaviour
After a key array such as
jointschanges,refresh_dynamic_parameters():Paramsstruct level by level, e.g.pid.joints_map,joints_map[...].dof_names_map,nested_dynamic.joints_map[...]...joints_map. A key goes even when its child map is empty, for example afterdof_nameswas cleared.cannot undeclare a statically typed parameter), and all mapped parameters are statically typed. Python behaves the same, so both languages match.update()keeps them current.update()runs in the node's on-set callback, before the node commits a value, so re-reading the node here could revert such a value.update()skips their parameters, so no entry with default values for the other fields is created. The next refresh creates the entry with all values from the node. Until then, setting such a parameter only reaches the node.update()and refresh serialize their read-modify-write on a new lock (update_mutex_in C++,update_lock_in Python). The user callback runs outside that lock.is_old()/try_update_params()users don't copy the struct on every call. The examples call refresh on every timer tick.Changes
Python
update()no longer fails for a key without an entry. Declaring a parameter runs the on-set callback. For a new key, Python'supdate()then calledget_entry()on an entry that didn't exist yet, so adding a key and callingrefresh_dynamic_parameters()always crashed:In both languages,
update()now only updates entries that exist. C++ used to create one withoperator[].Example:
nested_mapgets a second field,weight. Its key arraynested_map.entrieskeys no other map, which the tests for restored values need. The example READMEs list it.Sync the maps in
refresh_dynamic_parameters(). In a loop:remove_runtime_parametertemplates on a copy: erase stale keys level by level before descending, add an entry for every key level by level, and read the node only for entries that are not in the snapshot;Python finds and removes map entries through the module functions
_map_entry_names()and_map_remove_entry(). As struct methods, they would collide with fields or keys of the same name. Python'sget_params()readsparams_once and no longer writes to it, so a concurrent update can't get an old stamp.README: documents
refresh_dynamic_parameters()under "Mapped parameters", for C++ and Python. The Python example iterates the key array.Tests
New consistency tests in
example(C++),example_pythonandexample_cmake_python:nested_mapgets back theweightit kept on the node while itsvalueis set.update()stored before the node committed it survives a refresh.is_old()true.New
test_refresh_dynamic_parameters.pyruns a generated Python module on a real rclpy node, with a definition wherepidis the only map keyed by its array:get_params()keeps the stamp of an update made during its copy.update()stored before the node committed it survives a refresh.entry_nameswork.Each of these failed before the change that fixed it. The two cases where another thread changes parameters during a refresh are tested in Python only, by running that action at the start of the sync. A C++ version would need a test double for
NodeParametersInterface; the C++ logic mirrors the Python one.🤖 Generated with Claude Code