Skip to content

Conversation

hjgraca
Copy link
Contributor

@hjgraca hjgraca commented Sep 19, 2025

Please provide the issue number

Issue number: #1000

Summary

Fixes concurrency issues in the Metrics utility that were causing IndexOutOfRangeException and InvalidOperationException in multi-threaded environments.

Issues Fixed

  • Collection modification during enumeration: LINQ FirstOrDefault operations were failing when collections were modified concurrently
  • Dictionary thread safety: CustomMetadata dictionary was not thread-safe, causing IndexOutOfRangeException during concurrent access

Changes Made

Core Fixes

  • Replaced LINQ enumeration with thread-safe iteration: Added GetExistingMetric helper methods in Metrics.cs and MetricDirective.cs that use traditional for-loops instead of FirstOrDefault
  • Implemented thread-safe metadata storage: Changed CustomMetadata from Dictionary<string, object> to ConcurrentDictionary<string, object> in Metadata.cs
  • Added exception handling: Graceful handling of ArgumentOutOfRangeException during concurrent collection modifications

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.

- Replace LINQ FirstOrDefault with thread-safe iteration in Metrics.cs and MetricDirective.cs - Change CustomMetadata from Dictionary to ConcurrentDictionary in Metadata.cs - Add comprehensive concurrency tests with proper cleanup - Fix IndexOutOfRangeException and InvalidOperationException in multi-threaded scenarios - Maintain backward compatibility and performance
@boring-cyborg boring-cyborg bot added area/metrics Core metrics utility tests labels Sep 19, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 19, 2025
@github-actions github-actions bot added the bug Unexpected, reproducible and unintended software behaviour label Sep 19, 2025
Copy link

codecov bot commented Sep 19, 2025

Codecov Report

❌ Patch coverage is 79.77528% with 54 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.80%. Comparing base (4c3c3ce) to head (6742670).
⚠️ Report is 8 commits behind head on develop.

Files with missing lines Patch % Lines
...Lambda.Powertools.Metrics/Model/MetricDirective.cs 75.00% 21 Missing and 11 partials ⚠️
...aries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs 68.57% 8 Missing and 3 partials ⚠️
...rc/AWS.Lambda.Powertools.Metrics/Model/RootNode.cs 86.07% 0 Missing and 11 partials ⚠️
Additional details and impacted files
@@ Coverage Diff @@ ## develop #1001 +/- ## =========================================== - Coverage 77.89% 77.80% -0.09%  =========================================== Files 287 287 Lines 11444 11647 +203 Branches 1346 1391 +45 =========================================== + Hits 8914 9062 +148  - Misses 2099 2133 +34  - Partials 431 452 +21 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
svozza
svozza previously approved these changes Sep 19, 2025
@pull-request-size pull-request-size bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 22, 2025
@hjgraca hjgraca merged commit 496424e into develop Sep 25, 2025
8 of 9 checks passed
@hjgraca hjgraca deleted the fix/metrics-concurrency branch September 25, 2025 07:40
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/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. tests
3 participants