Skip to content

fix: Metrics Race condition if using Async calls #313

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

Merged

Conversation

hjgraca
Copy link
Contributor

@hjgraca hjgraca commented Jun 22, 2023

Please provide the issue number

Issue number: #310

Summary

Currently it is possible for the async code to hit a race condition that returns a System.ArgumentException: An item with the same key has already been added.

Changes

Add lock to avoid race condition when adding metrics.
Hold a lock for as short time as possible to reduce lock contention.
Add tests to test race condition.

User experience

Please share what the user experience looks like before and after this change

Checklist

Please leave checklist items unchecked if they do not apply to your change.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

… as short time as possible to reduce lock contention. Add tests.
@auto-assign auto-assign bot requested review from amirkaws and sliedig June 22, 2023 14:41
@boring-cyborg boring-cyborg bot added area/metrics Core metrics utility tests labels Jun 22, 2023
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 22, 2023
@hjgraca hjgraca linked an issue Jun 22, 2023 that may be closed by this pull request
@github-actions github-actions bot added the bug Unexpected, reproducible and unintended software behaviour label Jun 22, 2023
@codecov-commenter
Copy link

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.06 🎉

Comparison is base (4ba5210) 69.08% compared to head (9a34c30) 69.14%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #313      +/-   ##
===========================================
+ Coverage    69.08%   69.14%   +0.06%     
===========================================
  Files           79       79              
  Lines         3448     3452       +4     
===========================================
+ Hits          2382     2387       +5     
+ Misses        1066     1065       -1     
Flag Coverage Δ
unittests 69.14% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...Lambda.Powertools.Metrics/Model/MetricDirective.cs 86.66% <100.00%> (+0.62%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@amirkaws amirkaws left a comment

Choose a reason for hiding this comment

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

LGTM

@hjgraca hjgraca merged commit 7ea9051 into aws-powertools:develop Jun 23, 2023
@hjgraca hjgraca deleted the metrics-addmetric-raceconditiom branch June 23, 2023 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/metrics Core metrics utility bug Unexpected, reproducible and unintended software behaviour size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
Status: 🚀 Shipped
Development

Successfully merging this pull request may close these issues.

Bug: Race condition if using Async calls
3 participants