llrt icon indicating copy to clipboard operation
llrt copied to clipboard

Support for `AsyncLocalStorage` from `async_hooks`

Open nabetti1720 opened this issue 3 months ago • 3 comments

Although LLRT implements the async_hooks module, it does not expose AsyncLocalStorage. https://nodejs.org/api/async_context.html#class-asynclocalstorage

It seems that several well-known packages have adopted or are considering adopting it, so if it is implemented in LLRT, I think it will be able to accommodate new use cases.

Some packages that already use it include:

  • hono/context-storage: https://github.com/honojs/hono/blob/main/src/middleware/context-storage/index.ts
  • @opentelemetry/context-async-hooks : https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-context-async-hooks/src/AsyncLocalStorageContextManager.ts

Information on the packages we plan to adopt is as follows: https://github.com/aws-powertools/powertools-lambda-typescript/issues/2050#issuecomment-3341946805

Additionally, I'd encourage LLRT to add support for AsyncLocalStorage from async_hooks; even for Logger & Metrics (core utilities) we'll most likely start using this API in the next couple months to allow for future use cases.

There's even a new package called @aws/lambda-invoke-store (repo) that, quoting its readme "is designed to be integrated with the AWS Lambda Node.js Runtime Interface Client (RIC)", which means applications targeting versions of Node.js managed runtimes that use this package, might rely on AsyncLocalStorage and not work on LLRT.

Additionally, InvokeStore imported via @aws/lambda-invoke-store should be exposed as globalThis.awslambda.InvokeStore.

nabetti1720 avatar Sep 27 '25 23:09 nabetti1720

Newer versions of the AWS SDK now depend on @aws/lambda-invoke-store

Note PowerTools briefly depended on it and reverted.

perpil avatar Oct 23 '25 16:10 perpil

@perpil I tried updating to @aws-sdk(3.914.0) and building LLRT, but because browser was selected as the platform at build time, recursionDetectionMiddleware seems to be automatically replaced with a workaround implementation.

However, @aws-sdk/middleware-recursion-detection is not currently built to be imported on its own, so if you try to use it by itself, your build may be affected depending on your platform selection.

Do you know via which module the new PowerTools uses @aws-sdk/middleware-recursion-detection?I f you are using it via a module already bundled with LLRT, it may work with the current flavor. However, if you are using @aws-sdk/middleware-recursion-detection directly, you will need to add @aws-sdk/middleware-recursion-detection to your build so that it can be used independently.

nabetti1720 avatar Oct 25 '25 02:10 nabetti1720

The package that PowerTools will eventually depend on is aws-lambda-invoke-store. Details here. In the change that was reverted, it was being used by the Metrics and Tracer functionality.

I believe the purpose of middleware-recursion-detection in the SDK is to detect recursive loops in lambda invocations. It would have application only if you are using the LambdaClient from Lambda. See code here: https://github.com/aws/aws-sdk-js-v3/blob/2d30ee72d3a6e3741d3cab50890bf72a5d8e60c2/packages/middleware-recursion-detection/src/recursionDetectionMiddleware.ts If LLRT is using the browser platform, it is going to skip this logic, and although it will be missing the recursion detection functionality, shouldn't be impacted by AsyncStorage yet.

perpil avatar Oct 25 '25 18:10 perpil