Skip to content

Docs: explain how to cleanup with middy early return #1410

Closed
@quantumew

Description

@quantumew

Expected Behaviour

Instructions on how to avoid this segment leak scenario.

Current Behaviour

When middy returns early, the segment is not closed (middy does not run after middleware), eventually this results in a RangeError via the incrementCounter call due to how many open segments exist.

Code snippet

const tracer = new Tracer({ serviceName: 'xxx' })

...

export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer)).use(cacheMiddleware);

Steps to Reproduce

  1. Create a lambda with provisioned concurrency
  2. Hook up the captureLambdaHandler middleware
  3. Create middleware that runs after the capture mw, that returns early in its before
  4. Put this lambda under high load (this issue keeps arising 40 mins into our load tests)

Possible Solution

Before returning early in middy, I grab the current segment and close it. That does not seem to solve the problem, surprisingly. Do I have to also close all potential sub segments? Is the handler segment not the issue, is it actually the parent lambda segment?

AWS Lambda Powertools for TypeScript version

latest

AWS Lambda function runtime

16.x

Packaging format used

npm

Execution logs

2023-04-10T14:16:56.326Z	d94677bc-a252-4830-a678-2c5ece7e0aff	ERROR	Invoke Error 	

{""errorType"":""RangeError"",""errorMessage"":""Maximum call stack size exceeded"",""stack"":[""RangeError: Maximum call stack size exceeded"",""    

at Ae.incrementCounter (/var/task/index.js:25:9545)"",""    

at Ae.incrementCounter (/var/task/index.js:25:9621)"",""    at Ae.incrementCounter (/var/task/index.js:25:9621)"",""

at Ae.incrementCounter (/var/task/index.js:25:9621)"",""    at Ae.incrementCounter (/var/task/index.js:25:9621)"","" 

at Ae.incrementCounter (/var/task/index.js:25:9621)"",""    at Ae.incrementCounter (/var/task/index.js:25:9621)"","" 

at Ae.incrementCounter (/var/task/index.js:25:9621)"",""    at Ae.incrementCounter (/var/task/index.js:25:9621)"",""

at Ae.incrementCounter (/var/task/index.js:25:9621)""]}

Metadata

Metadata

Assignees

Labels

confirmedThe scope is clear, ready for implementationdocumentationImprovements or additions to documentationtracerThis item relates to the Tracer Utility

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions