Skip to content

fix(logger): scope lambda context per invocation under LMI concurrency#5430

Merged
dreamorosi merged 2 commits into
mainfrom
getrequestid-lmi-bug
Jul 9, 2026
Merged

fix(logger): scope lambda context per invocation under LMI concurrency#5430
dreamorosi merged 2 commits into
mainfrom
getrequestid-lmi-bug

Conversation

@svozza

@svozza svozza commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Logger.addContext() stored the Lambda context in plain instance state, so when Lambda Managed Instances multiplexes concurrent invocations into the same execution environment, invocations overwrite each other's context and emitted log lines carry the wrong function_request_id, tenant_id, and cold_start. This PR scopes the whole lambdaContext per invocation via the InvokeStore when AWS_LAMBDA_MAX_CONCURRENCY is set, using the same dual-path pattern already used for temporary attributes.

Changes

  • Add setLambdaContext()/getLambdaContext() to LogAttributesStore, storing the context in the InvokeStore under a private symbol when shouldUseInvokeStore() is true, and in instance state otherwise (throws InvokeStore is not available if the store is missing, matching the existing attribute methods)
  • Logger.addContext() writes through the store; getPowertoolsLogData() and createChild() read through it, so formatters and child loggers stay consistent
  • Add a concurrency unit test that interleaves two invocations' addContext()/info() calls via sequence(): with the InvokeStore each log line carries its own invocation's request id, tenant id, and cold start; without it the last-write-wins behavior is preserved

Issue number: closes #5429


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.

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/L PRs between 100-499 LOC and removed size/L PRs between 100-499 LOC labels Jul 8, 2026
@svozza svozza marked this pull request as ready for review July 8, 2026 21:58
@svozza svozza force-pushed the getrequestid-lmi-bug branch from f33f4e4 to 89b72fd Compare July 8, 2026 22:00
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/L PRs between 100-499 LOC and removed size/L PRs between 100-499 LOC labels Jul 8, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/L PRs between 100-499 LOC and removed size/L PRs between 100-499 LOC labels Jul 9, 2026
@sonarqubecloud

sonarqubecloud Bot commented Jul 9, 2026

Copy link
Copy Markdown

@dreamorosi dreamorosi merged commit e5d2e69 into main Jul 9, 2026
41 checks passed
@dreamorosi dreamorosi deleted the getrequestid-lmi-bug branch July 9, 2026 14:58
svozza added a commit that referenced this pull request Jul 9, 2026
…ogs by request id

With the lambda context now scoped per invocation under LMI (#5430),
the handler selects its own log lines by function_request_id — an
independent per-invocation attribute — and the test asserts the
InvokeStore-scoped invocationKey on those lines, making the suite a
regression check for both the context scoping and appendKeys isolation.

The suite adds ~4 minutes to the logger e2e cell, inside the agreed
budget for running on every e2e dispatch, so the RUN_LMI_TESTS gate
and workflow input are removed.
svozza added a commit that referenced this pull request Jul 10, 2026
…ogs by request id

With the lambda context now scoped per invocation under LMI (#5430),
the handler selects its own log lines by function_request_id — an
independent per-invocation attribute — and the test asserts the
InvokeStore-scoped invocationKey on those lines, making the suite a
regression check for both the context scoping and appendKeys isolation.

The suite adds ~4 minutes to the logger e2e cell, inside the agreed
budget for running on every e2e dispatch, so the RUN_LMI_TESTS gate
and workflow input are removed.
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 emits wrong function_request_id under Lambda Managed Instances concurrency

2 participants