Skip to content

Replace std::default_random_engine with std::mt19937 (rolling) #2843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from

Conversation

keeponoiro
Copy link

This PR addresses issue #2842, where std::default_random_engine can lead to periodic goal ID duplication due to its underlying implementation in GCC (minstd_rand0). When actions are executed frequently, this periodicity increases the likelihood of duplicate goal IDs, potentially causing unintended behavior.

In extreme cases, duplicate goal IDs can:

Trigger an exception when attempting to accept a new goal (goal ID already exists).

Extend the expiration time of the first goal when a duplicate ID is encountered.

To mitigate this, this PR replaces std::default_random_engine with std::mt19937, which provides a higher-quality random number generator with better distribution properties, reducing the risk of goal ID collisions.

Changes in this PR
std::default_random_engine → std::mt19937 in client_base.cpp

Ensures goal ID randomness to prevent collisions in high-frequency action executions.

Testing
In a 13-hour test, we observed periodic goal ID duplication when using std::default_random_engine.

Same goal ID appeared twice within a 10-minute period.

When goal ID retention was reduced to 1 minute, the issue no longer occurred.

With std::mt19937, goal ID duplication is effectively mitigated.

Impact
No ABI breakage.

Improves reliability of goal ID generation.

Addresses unintended goal expiration extension issues.

@jmachowinski
Copy link
Collaborator

Pulls: #2843
Gist: https://gist.githubusercontent.com/jmachowinski/fa696c2c5e21119166bc88323ebe7a7a/raw/09ab555f570f14b4ac797af1221c6430c7837dad/ros2.repos
BUILD args:
TEST args:
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/16015

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Signed-off-by: keeponoiro <keeeeeeep@gmail.com>
@jmachowinski
Copy link
Collaborator

@keeponoiro can you signoff your commit ?

@keeponoiro keeponoiro force-pushed the improve-random-generator-rolling branch from 1f78f3f to e31e6c7 Compare May 16, 2025 16:23
Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this makes sense. lgtm with green CI.

  • we do not need to let the platform decide the best engine here for the performance.
  • consistent output across platforms with the same seed.

@keeponoiro
Copy link
Author

@fujitatomoya
Sorry, I was referring to the situation in Humble. In Rolling, the goal timeout has been changed from starting at action initiation to being set at action completion, and the timeout itself has been reduced from 15 minutes to 10 seconds.

With a 10-second timeout, the probability of goal ID collisions is significantly lower. As @fujitatomoya mentioned, it would have been better to focus on maintaining consistent performance across platforms.

That said, since ROS 2 users can modify the timeout, having a lower probability of goal ID collisions when the timeout is extended is still beneficial.

@fujitatomoya
Copy link
Collaborator

@keeponoiro no worries. timeout for expiration is the really different topic, i was just saying that has been changed in jazzy and upstream distros.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants