Skip to content

fix(logger): scope log buffer per invocation under LMI concurrency - #5543

Open
vishwakt wants to merge 1 commit into
aws-powertools:mainfrom
vishwakt:fix/5541-logger-buffer-invoke-store
Open

fix(logger): scope log buffer per invocation under LMI concurrency#5543
vishwakt wants to merge 1 commit into
aws-powertools:mainfrom
vishwakt:fix/5541-logger-buffer-invoke-store

Conversation

@vishwakt

@vishwakt vishwakt commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Changes

The log buffer cleared all entries whenever a log was buffered for a trace id it had not seen before, based on the assumption that only one request can be in flight per execution environment (the comment in bufferLogItem() stated this explicitly). Under Lambda Managed Instances concurrency that assumption no longer holds: an invocation starting to buffer wiped every other in-flight invocation's buffered logs, so a failing invocation had nothing left to flush and its debug context was silently lost.

Following the direction agreed in the issue, the buffer is now scoped through the InvokeStore when concurrency is enabled (AWS_LAMBDA_MAX_CONCURRENCY set): a new private #getBuffer() accessor returns an invocation-scoped CircularMap stored in the InvokeStore, so each invocation's buffered logs live and die with its invocation context and no cross-invocation cleanup is needed. The sequential path is unchanged: it keeps the instance-level buffer and its new-trace clear() behavior, preserving the stale-buffer safeguard introduced in #3705.

bufferLogItem(), flushBuffer(), and clearBuffer() all resolve the buffer through the accessor. The clear() now only runs on the sequential path.

Added regression tests in tests/unit/concurrency/logBuffer.test.ts:

  • interleaved invocations keep and flush their own buffered logs (fails on main, where invocation B's first buffered log wipes invocation A's buffer)
  • each invocation flushes independently while the other is mid-flight
  • clearBuffer() throws when concurrency is enabled but the InvokeStore is unavailable, matching the behavior of the attribute store

All existing logger unit tests pass unchanged (226 tests), confirming sequential behavior is preserved, with 100% coverage on Logger.ts.

Issue number: closes #5541


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

The log buffer cleared all entries whenever a log was buffered for a trace
id it had not seen before, based on the assumption that only one request can
be in flight per execution environment. Under Lambda Managed Instances
concurrency that assumption no longer holds: an invocation starting to
buffer wiped every other in-flight invocation's buffered logs, so a failing
invocation had nothing left to flush.

Scope the buffer through the InvokeStore when concurrency is enabled, so
each invocation's buffered logs live and die with its invocation context.
The sequential path keeps the existing instance buffer and its new-trace
cleanup behavior, preserving the stale-buffer safeguard from aws-powertools#3705.

Closes aws-powertools#5541
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L PRs between 100-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Logger log buffer wipes other in-flight invocations' buffered logs under Lambda Managed Instances concurrency

1 participant