Skip to content

Conversation

@caguero
Copy link
Collaborator

@caguero caguero commented Oct 3, 2025

🦟 Bug fix

Fixes #701

Summary

According to #701, checking an environment variable every time that we're inside the Version() function might cause a crash. This patch only checks the environment variable once.

There's some behavior change here because you cannot change the value of the environment variable in the middle of a gz-transport session anymore. I think it's probably a good idea not allowing that but I can be convinced otherwise.

Checklist

  • Signed all commits for DCO
  • Added tests
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers
  • Was GenAI used to generate this PR? If so, make sure to add "Generated-by" to your commits. (See this policy for more info.)

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by and Generated-by messages.

@github-project-automation github-project-automation bot moved this from Inbox to In review in Core development Oct 3, 2025
Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

I would agree that environment variables should not be used to change behavior after the program has started. If we want to do that, we should be providing a C++ API for it.

src/Discovery.hh Outdated
/// \return The discovery version.
private: uint8_t Version() const
{
static std::string gzStats;
Copy link
Contributor

Choose a reason for hiding this comment

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

We can make gzStats a non-static std::string then. It's always good to remove static variables with non-trivial destructors https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea. 40fbf54

src/Discovery.hh Outdated
Comment on lines 1572 to 1573
static int topicStats;
static bool versionInitialized = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these be std::atomic in case they are accessed from mulitple threads?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed in 40fbf54

src/Discovery.hh Outdated
env("GZ_TRANSPORT_TOPIC_STATISTICS", gzStats) && !gzStats.empty())
{
topicStats = (gzStats == "1");
versionInitialized = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

If GZ_TRANSPORT_TOPIC_STATISTICS is not set, this if block will not get executed, so we'll end up checking the environment variable everytime still. How about putting this versionInitialized = true after the if block?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, 40fbf54.

Signed-off-by: Carlos Agüero <[email protected]>
src/Discovery.hh Outdated
Comment on lines 1574 to 1582

if (env("GZ_TRANSPORT_TOPIC_STATISTICS", gzStats) && !gzStats.empty())
if (!versionInitialized &&
env("GZ_TRANSPORT_TOPIC_STATISTICS", gzStats) && !gzStats.empty())
{
topicStats = (gzStats == "1");
}

versionInitialized = true;

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, thinking about it some more. It's probably still not safe from a data race. versionInitialized, even though atomic, the whole processes of checking the environment variable and setting topicStats is not atomic.

My suggestion would be to use std::call_once with a local lambda

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since this is the version targeting main and we can break ABI, I simplified the logic in 28aadf9.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, but note that we'll need to backport this because #701 occurs on Jetty.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Absolutely, the backport will be different, we need to preserve ABI.

@caguero caguero merged commit 815b417 into main Oct 24, 2025
12 of 13 checks passed
@caguero caguero deleted the fix_issue_701 branch October 24, 2025 17:11
@github-project-automation github-project-automation bot moved this from In review to Done in Core development Oct 24, 2025
@caguero
Copy link
Collaborator Author

caguero commented Oct 31, 2025

@Mergifyio backport gz-transport15

@mergify
Copy link
Contributor

mergify bot commented Oct 31, 2025

backport gz-transport15

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Oct 31, 2025
* Only check GZ_TRANSPORT_TOPIC_STATISTICS once

Signed-off-by: Carlos Agüero <[email protected]>
(cherry picked from commit 815b417)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants