Skip to content

Conversation

@kimjaejung96
Copy link

@kimjaejung96 kimjaejung96 commented Jan 17, 2026

Fixes #6233

Changes

This PR fixes a thread-safety issue in LogRecordSharedPool.Rent() where the same LogRecord instance could be rented to multiple threads concurrently under high contention.

  • Fixed Race Condition: In Rent(), when TryRentCoreRare fails (due to a race), changed the behavior to break the loop and create a new LogRecord instance. Previously, it would continue and retry with a new index, which could lead to a duplicate rental.
  • Optimized Calculation: Extracted slotIndex calculation to avoid redundant modulo operations.
  • Added Regression Test: Added RentShouldNeverReturnSameInstanceConcurrently in LogRecordSharedPoolTests.cs to verify the fix.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

Fixes open-telemetry#6233

- Change TryRentCoreRare failure handling from 'continue' to 'break' to prevent duplicate LogRecord rentals under high contention
- Optimize slotIndex calculation
- Add regression test RentShouldNeverReturnSameInstanceConcurrently
@kimjaejung96 kimjaejung96 requested a review from a team as a code owner January 17, 2026 05:42
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 17, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: Kielek / name: Piotr Kiełkowicz (b45f9c4)
  • ✅ login: kimjaejung96 / name: kimjaejung96 (5d5a38a, 8a8f10b, 9edc7dc)
  • ✅ login: martincostello / name: Martin Costello (5d5a38a)

@github-actions github-actions bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Jan 17, 2026
@kimjaejung96
Copy link
Author

kimjaejung96 commented Jan 22, 2026

Hi.

@alanwest @open-telemetry/dotnet-approvers could you please review this? It addresses the thread-safety issue in LogRecordSharedPool (Fixes #6233).

@TimothyMothra, tagging you as well since this is in the Logs/LogRecord pool area.

Also, this is my first time requesting a review in this repo—if I’m not tagging the right people or there’s a preferred process, please let me know and I’ll follow it.

@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Jan 29, 2026
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Jan 29, 2026
Copy link
Member

@martincostello martincostello left a comment

Choose a reason for hiding this comment

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

Please update the CHANGELOG with the bug fix details.

if (!inUseRecords.TryAdd(record, Environment.CurrentManagedThreadId))
{
Volatile.Write(ref duplicateDetected, true);
duplicateDetails = $"LogRecord {record.GetHashCode()} rented by thread {inUseRecords[record]} and {Environment.CurrentManagedThreadId}";
Copy link
Member

Choose a reason for hiding this comment

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

If the bug happened again, this might overwrite a race itself. It would be better to add the message to a concurrent collection, then join its contents to construct the failure message.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the review, @martincostello!

I've addressed your feedback:

  1. CHANGELOG updated - Added the bug fix entry under the Unreleased section in src/OpenTelemetry/CHANGELOG.md

  2. Test race condition fixed - Changed the duplicate detection logic to use ConcurrentQueue<string> instead of a single string variable. Now all duplicate events are collected safely and joined in the final assertion message.

Changes:

  • Replaced duplicateDetected (bool) + duplicateDetails (string) with ConcurrentQueue<string> duplicateMessages
  • Used TryGetValue to safely retrieve the first thread ID
  • Updated the assertion to Assert.True(duplicateMessages.IsEmpty, ...)

Please let me know if there's anything else that needs to be addressed.

- Add CHANGELOG entry for thread-safety fix in LogRecordSharedPool.Rent()
- Use ConcurrentQueue for duplicate detection messages in test to avoid race condition when collecting failure details"
@codecov
Copy link

codecov bot commented Jan 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.14%. Comparing base (cfc2d0c) to head (b45f9c4).
⚠️ Report is 7 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6833      +/-   ##
==========================================
- Coverage   87.17%   87.14%   -0.04%     
==========================================
  Files         263      263              
  Lines       12385    12386       +1     
==========================================
- Hits        10797    10794       -3     
- Misses       1588     1592       +4     
Flag Coverage Δ
unittests-Project-Experimental 87.01% <100.00%> (-0.15%) ⬇️
unittests-Project-Stable 87.06% <100.00%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Δ
src/OpenTelemetry/Logs/Pool/LogRecordSharedPool.cs 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2026

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Feb 7, 2026
@kimjaejung96
Copy link
Author

Hi @martincostello — re-requesting review 🙇
Please let me know if there's anything else I should update.

@github-actions github-actions bot removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] LogRecordSharedPool isn't thread safe

3 participants