Skip to content

feat(logging): thread safety with ConcurrentDictionary for scope storage#1099

Merged
hjgraca merged 1 commit intodevelopfrom
fix-logger-thread-safety
Dec 18, 2025
Merged

feat(logging): thread safety with ConcurrentDictionary for scope storage#1099
hjgraca merged 1 commit intodevelopfrom
fix-logger-thread-safety

Conversation

@hjgraca
Copy link
Contributor

@hjgraca hjgraca commented Dec 18, 2025

Please provide the issue number

Issue number: #1076

Summary

Changes

  • Replace inner Dictionary with ConcurrentDictionary in _threadScopes to ensure thread-safe operations on scope values
  • Update Scope property return type from IDictionary to ConcurrentDictionary for consistency
  • Refactor SetKey method to use intermediate variable for better readability and null handling
  • Replace manual ContainsKey/Remove checks with TryRemove for atomic operations in RemoveKeys and RemoveKey methods
  • Update GetAllKeys to return a snapshot via ToArray() to prevent concurrent modification exceptions during enumeration
  • Add documentation clarifying that inner dictionary is ConcurrentDictionary and that GetAllKeys returns a snapshot for thread-safety
  • Improve code consistency by using ConcurrentDictionary methods throughout instead of mixing Dictionary and ConcurrentDictionary patterns

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.

…ope storage

- Replace inner Dictionary with ConcurrentDictionary in _threadScopes to ensure thread-safe operations on scope values
- Update Scope property return type from IDictionary to ConcurrentDictionary for consistency
- Refactor SetKey method to use intermediate variable for better readability and null handling
- Replace manual ContainsKey/Remove checks with TryRemove for atomic operations in RemoveKeys and RemoveKey methods
- Update GetAllKeys to return a snapshot via ToArray() to prevent concurrent modification exceptions during enumeration
- Add documentation clarifying that inner dictionary is ConcurrentDictionary and that GetAllKeys returns a snapshot for thread-safety
- Improve code consistency by using ConcurrentDictionary methods throughout instead of mixing Dictionary and ConcurrentDictionary patterns
@boring-cyborg boring-cyborg bot added the area/logging Core logging utility label Dec 18, 2025
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Dec 18, 2025
@github-actions github-actions bot added the feature New features or minor changes label Dec 18, 2025
@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Dec 18, 2025

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.50%. Comparing base (1a1a3fa) to head (38e8bad).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
.../src/AWS.Lambda.Powertools.Logging/Logger.Scope.cs 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1099      +/-   ##
===========================================
- Coverage    78.55%   78.50%   -0.06%     
===========================================
  Files          299      299              
  Lines        12376    12375       -1     
  Branches      1494     1492       -2     
===========================================
- Hits          9722     9715       -7     
- Misses        2180     2186       +6     
  Partials       474      474              

☔ 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.

@hjgraca hjgraca merged commit eb7ba1d into develop Dec 18, 2025
9 of 10 checks passed
@hjgraca hjgraca deleted the fix-logger-thread-safety branch December 18, 2025 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/logging Core logging utility feature New features or minor changes size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants