This repository demonstrates a memory leak issue with dd-trace when using ESM modules. The leak occurs when the application handles requests after being started with Datadog's ESM integration.
The memory leak is triggered when:
- The application is started with Datadog's ESM integration (
--import dd-trace/initialize.mjs) - A dynamic import that fails at runtime is executed
- Requests are made to the application
While failing dynamic imports may not be typical, they are used in some applications to conditionally execute code based on whether dependencies are installed.
- Install dependencies:
npm install- Build the application:
npm run build- Start the application with Datadog tracing:
npm start- In another terminal, continuously call the endpoint to trigger the memory leak:
./continueslyCallEndpoint.sh- Monitor memory usage - you should observe increasing memory consumption over time.
The issue is in index.ts where a failing dynamic import is executed on each request:
await import("non-existing-module"); // Always throwsThis pattern, combined with Datadog's ESM integration, causes spans and other objects to not be garbage collected properly.
Memory usage should remain stable.