Skip to content
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

fix/stats race #145

Merged
merged 2 commits into from
Feb 3, 2025
Merged

Conversation

perhallgren
Copy link
Contributor

  • test: add test for concurrent statistics sampling
  • fix: avoid data race for PacketsSent

PacketsSent was not protected by statsMu when being incremented in sendICMP, which gave a data race when calling Statistics() on a running Pinger.

This PR adds a small test to test for the data race, and fixes the issue.

@SuperQ
Copy link
Contributor

SuperQ commented Feb 3, 2025

This needs a DCO sign-off. You can use git commit -s --amend to add it.

@SuperQ
Copy link
Contributor

SuperQ commented Feb 3, 2025

Hmm, should we also protect Hmm, should we also PacketsRecvDuplicates`?

Maybe we should just migrate these values to Go sync/atomic.

@perhallgren perhallgren force-pushed the fix/stats-race branch 2 times, most recently from b6ccd59 to b23432d Compare February 3, 2025 08:26
@perhallgren
Copy link
Contributor Author

Hmm, should we also protect PacketsRecvDuplicates?

Good catch, added!

The test currently fails as `PacketsSent` is not protected by `statsMu`
in `sendICMP`.

Signed-off-by: Per Hallgren <[email protected]>
Writes to the following fields are now protected by `statsMu`:

- `ipaddr`
- `addr`
- `PacketsSent`
- `PacketsRecvDuplicates`

Signed-off-by: Per Hallgren <[email protected]>
@perhallgren perhallgren marked this pull request as ready for review February 3, 2025 10:06
@perhallgren
Copy link
Contributor Author

@SuperQ marked as ready for review.

Would you like to use atomics instead of the lock? I think both will start to feel a bit ad-hoc, since there's no intuitive boundary what variables are protected by the lock and which ones are just atomic.

@perhallgren
Copy link
Contributor Author

Potentially one could rewrite to use atomics for all variables that we read as a result of a call to Statistics()

Copy link
Contributor

@SuperQ SuperQ 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 longer term, we should cleanup the whole stats part of the Pinger struct. Maybe make it a separate struct/package with an interface that returns a public value struct.

But, this is a fine enough workaround for now.

@SuperQ SuperQ merged commit a13e284 into prometheus-community:main Feb 3, 2025
5 checks passed
@perhallgren perhallgren deleted the fix/stats-race branch February 3, 2025 10:39
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.

2 participants