Skip to content

Bug: Metrics concurrency-related exceptions when used in multi-threaded scenarios #1000

@hjgraca

Description

@hjgraca

Expected Behaviour

Metrics should be thread-safe and handle concurrent access without throwing exceptions.

Current Behaviour

  1. Collection was modified; enumeration operation may not execute

    • Occurs in LINQ FirstOrDefault operations when metrics collections are modified concurrently
    • Stack trace points to Metrics.cs line 202-203 and MetricDirective.cs
  2. IndexOutOfRangeException: Index was outside the bounds of the array

    • Occurs in Dictionary.TryInsert during AddMetadata operations
    • Stack trace: Metadata.cs:line 156 in CustomMetadata.TryAdd(key, value)

Code snippet

.

Possible Solution

No response

Steps to Reproduce

The issue manifests when:

  • Multiple threads simultaneously call Metrics.AddMetric() and Metrics.AddMetadata()
  • High concurrency scenarios

Powertools for AWS Lambda (.NET) version

latest

AWS Lambda function runtime

dotnet8

Debugging logs

Metadata

Metadata

Assignees

Labels

area/metricsCore metrics utilitybugUnexpected, reproducible and unintended software behaviour

Type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions