Skip to content

fix(logging): read the trace ID once per log entry and handle a missing trace ID in the log buffer - #1378

Merged
hjgraca merged 2 commits into
developfrom
fix/logging-trace-id-race
Nov 26, 2026
Merged

hjgraca merged 2 commits into
developfrom
fix/logging-trace-id-race

Conversation

@hjgraca

@hjgraca hjgraca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Issue number: closes #1376

Summary

Changes

Race in PowertoolsLogger.GetLogEntry. The X-Ray trace ID was read twice: once in the null check and again to split it. IPowertoolsConfigurations.XRayTraceId reads LambdaTraceProvider or the _X_AMZN_TRACE_ID environment variable on every access. When the value was cleared between the two reads, the log call threw NullReferenceException into user code. It's now read once into a local.

Missing trace ID in LogBuffer. HasEntries, HasEvictions and ClearCurrentInvocation passed the trace ID straight to ConcurrentDictionary, which throws ArgumentNullException for a null key. Add and GetAndClear already checked for it. With log buffering enabled and no trace ID (for example outside Lambda), FlushBuffer() failed inside its own try and then logged a spurious Failed to flush log buffer error. These three members now treat a missing trace ID as "nothing buffered". The public ClearBuffer() was not affected: LogBufferManager already swallows exceptions there.

User experience

  • Before: a log call made while the trace ID changes can throw NullReferenceException. Examples are a background task that is still logging when the next invocation starts, or concurrent invocations on runtimes that use the environment variable. With buffering enabled and no trace ID, FlushBuffer() writes a Failed to flush log buffer error.
  • After: the entry is written with the trace ID that was read, and flushing without a trace ID does nothing.

Testing

New TraceIdTests:

Test Before After
Trace ID present on the first read and null afterwards: the log call doesn't throw and writes the trace ID it read NullReferenceException at PowertoolsLogger.cs:238 (the CI failure in #1376) passes
LogBuffer with no trace ID: HasEntries/HasEvictions are false and ClearCurrentInvocation doesn't throw ArgumentNullException passes
FlushBuffer() with buffering enabled and no trace ID doesn't log Failed to flush log buffer logged passes
ClearBuffer() with buffering enabled and no trace ID doesn't throw passes (guard) passes
  • dotnet test tests/AWS.Lambda.Powertools.Logging.Tests: 517/517 on net8.0 and net10.0.
  • dotnet test tests/AWS.Lambda.Powertools.ConcurrencyTests, which includes KeyIsolationTests, the test that caught this in CI: 526/526 on net8.0 and net10.0.
  • Full Release build from libraries/: 0 errors.
  • Native AOT publish (.NET 10) of Logging apps against a local pack of this branch: 0 Powertools-owned trim/AOT warnings.
  • The repro from Bug: Logger throws NullReferenceException when the X-Ray trace ID changes while a log entry is built #1376 (4 threads logging while another toggles _X_AMZN_TRACE_ID) threw on 2,653 of 80,000 calls before this change.

Merge danger: two-way door. Blast radius: how the trace ID is read while building a log entry, and the log buffer when there's no trace ID.

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.

@hjgraca hjgraca added bug Unexpected, reproducible and unintended software behaviour area/logging Core logging utility labels Nov 26, 2026
@boring-cyborg boring-cyborg Bot added the tests label Nov 26, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 26, 2026
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.78%. Comparing base (3701b0a) to head (4c04bf3).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1378      +/-   ##
===========================================
- Coverage    80.79%   80.78%   -0.01%     
===========================================
  Files          306      306              
  Lines        13098    13102       +4     
  Branches      1562     1563       +1     
===========================================
+ Hits         10582    10585       +3     
- Misses        2074     2076       +2     
+ Partials       442      441       -1     

☔ View full report in Codecov by Harness.
📢 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.

@sonarqubecloud

Copy link
Copy Markdown

@hjgraca
hjgraca merged commit 358c3fc into develop Nov 26, 2026
11 checks passed
@hjgraca
hjgraca deleted the fix/logging-trace-id-race branch November 26, 2026 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/logging Core logging utility bug Unexpected, reproducible and unintended software behaviour size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Logger throws NullReferenceException when the X-Ray trace ID changes while a log entry is built

1 participant