Bug: Metrics thread safety issue #593
Labels
area/metrics
Core metrics utility
bug
Unexpected, reproducible and unintended software behaviour
status/confirmed
The scope is clear, ready for implementation
Expected Behaviour
I am able to add metrics from multiple threads without issue.
Current Behaviour
Metrics throws an exception:
Code snippet
To make this test fail reliably you need to set the PowertoolsConfigurations.MaxMetrics to a low number like 1
Add this method to the FunctionHandler.cs in the metrics tests project:
and this test to FunctionHandlerTests.cs:
Possible Solution
You could either add additional locks to the Metrics.AddMetric method:
Or you could possibly use concurrent collections under the hood and the Interlocked class to handle incrementing counts and swapping objects when a flush is requried.
Steps to Reproduce
Unit test that shows the issue is in the Code Snippet section. Ensure the MaxMetrics is set to a low value like 1, otherwise the test will inconsistently pass/fail.
Powertools for AWS Lambda (.NET) version
latest
AWS Lambda function runtime
dotnet6
Debugging logs
No response
The text was updated successfully, but these errors were encountered: