Skip to content

fix nextjs memory leak from req weakmap#7946

Merged
BridgeAR merged 1 commit intomasterfrom
fix-next-req-map-leak
Apr 8, 2026
Merged

fix nextjs memory leak from req weakmap#7946
BridgeAR merged 1 commit intomasterfrom
fix-next-req-map-leak

Conversation

@rochdev
Copy link
Copy Markdown
Member

@rochdev rochdev commented Apr 8, 2026

What does this PR do?

Fix Next.js memory leak from req WeakMap.

Motivation

Regression from #7000 reported in #7878. The original fix kept the WeakMap and cleaned it up manually, which fixes the symptom but not the underlying issue that we shouldn't be storing things in a map to begin with and instead rely on AsyncLocalStorage which is 100% safe.

Additional Notes

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.87%. Comparing base (edef930) to head (3fd5b18).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
packages/datadog-plugin-next/src/index.js 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7946   +/-   ##
=======================================
  Coverage   73.86%   73.87%           
=======================================
  Files         773      773           
  Lines       35955    35949    -6     
=======================================
- Hits        26558    26557    -1     
+ Misses       9397     9392    -5     
Flag Coverage Δ
aiguard-macos 36.40% <ø> (-0.09%) ⬇️
aiguard-ubuntu 36.50% <ø> (-0.09%) ⬇️
aiguard-windows 36.30% <ø> (-0.09%) ⬇️
apm-capabilities-tracing-macos 48.81% <0.00%> (-0.04%) ⬇️
apm-capabilities-tracing-ubuntu 48.89% <0.00%> (+0.01%) ⬆️
apm-capabilities-tracing-windows 48.66% <0.00%> (+0.17%) ⬆️
apm-integrations-child-process 36.03% <ø> (-0.10%) ⬇️
apm-integrations-couchbase-18 34.98% <ø> (-0.09%) ⬇️
apm-integrations-couchbase-eol 35.06% <ø> (-0.07%) ⬇️
apm-integrations-oracledb 35.04% <ø> (-0.09%) ⬇️
appsec-express 52.86% <ø> (-0.08%) ⬇️
appsec-fastify 49.33% <ø> (-0.07%) ⬇️
appsec-graphql 49.57% <ø> (-0.16%) ⬇️
appsec-kafka 42.12% <ø> (-0.08%) ⬇️
appsec-ldapjs 41.41% <ø> (-0.08%) ⬇️
appsec-lodash 41.44% <ø> (-0.08%) ⬇️
appsec-macos 56.76% <ø> (-0.07%) ⬇️
appsec-mongodb-core 46.06% <ø> (-0.07%) ⬇️
appsec-mongoose 46.66% <ø> (-0.07%) ⬇️
appsec-mysql 48.79% <ø> (-0.07%) ⬇️
appsec-node-serialize 40.61% <ø> (-0.08%) ⬇️
appsec-passport 44.63% <ø> (-0.08%) ⬇️
appsec-postgres 48.47% <ø> (-0.08%) ⬇️
appsec-sourcing 40.07% <ø> (-0.08%) ⬇️
appsec-stripe 42.32% <ø> (-0.08%) ⬇️
appsec-template 40.77% <ø> (-0.08%) ⬇️
appsec-ubuntu 56.85% <ø> (-0.07%) ⬇️
appsec-windows 56.63% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-bluebird 29.91% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-body-parser 37.65% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-child_process 35.42% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-cookie-parser 31.85% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-express 32.14% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 31.97% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-express-session 37.32% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-fs 29.55% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-generic-pool 31.44% <ø> (ø)
instrumentations-instrumentation-http 36.86% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-knex 29.92% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-mongoose 31.03% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-multer 37.42% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-mysql2 35.39% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-passport 41.25% <ø> (-0.08%) ⬇️
instrumentations-instrumentation-passport-http 40.94% <ø> (-0.08%) ⬇️
instrumentations-instrumentation-passport-local 41.44% <ø> (-0.08%) ⬇️
instrumentations-instrumentation-pg 34.90% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-promise 29.84% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-promise-js 29.85% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-q 29.89% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-url 29.81% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-when 29.86% <ø> (-0.10%) ⬇️
llmobs-ai 38.28% <ø> (-0.09%) ⬇️
llmobs-anthropic 37.88% <ø> (-0.09%) ⬇️
llmobs-bedrock 37.17% <ø> (-0.08%) ⬇️
llmobs-google-genai 37.56% <ø> (-0.08%) ⬇️
llmobs-langchain 37.17% <ø> (+0.03%) ⬆️
llmobs-openai 41.24% <ø> (-0.08%) ⬇️
llmobs-vertex-ai 37.73% <ø> (-0.09%) ⬇️
platform-core 30.82% <ø> (ø)
platform-esbuild 33.74% <ø> (ø)
platform-instrumentations-misc 40.89% <ø> (ø)
platform-shimmer 36.80% <ø> (ø)
platform-unit-guardrails 32.24% <ø> (ø)
platform-webpack 20.96% <ø> (ø)
plugins-azure-durable-functions 25.38% <ø> (ø)
plugins-azure-event-hubs 25.53% <ø> (ø)
plugins-azure-service-bus 24.92% <ø> (ø)
plugins-bullmq 40.72% <ø> (-0.09%) ⬇️
plugins-cassandra 35.18% <ø> (-0.09%) ⬇️
plugins-cookie 26.56% <ø> (ø)
plugins-cookie-parser 26.35% <ø> (ø)
plugins-crypto 26.21% <ø> (ø)
plugins-dd-trace-api 35.39% <ø> (-0.10%) ⬇️
plugins-express-mongo-sanitize 26.50% <ø> (ø)
plugins-express-session 26.31% <ø> (ø)
plugins-fastify 39.44% <ø> (-0.09%) ⬇️
plugins-fetch 35.70% <ø> (-0.09%) ⬇️
plugins-fs 35.64% <ø> (-0.10%) ⬇️
plugins-generic-pool 25.57% <ø> (ø)
plugins-google-cloud-pubsub 43.03% <ø> (-0.08%) ⬇️
plugins-grpc 38.04% <ø> (-0.09%) ⬇️
plugins-handlebars 26.54% <ø> (ø)
plugins-hapi 37.24% <ø> (-0.09%) ⬇️
plugins-hono 37.52% <ø> (-0.09%) ⬇️
plugins-ioredis 35.61% <ø> (-0.09%) ⬇️
plugins-knex 26.20% <ø> (ø)
plugins-langgraph 35.08% <ø> (-0.09%) ⬇️
plugins-ldapjs 24.12% <ø> (ø)
plugins-light-my-request 25.93% <ø> (ø)
plugins-limitd-client 30.17% <ø> (-0.10%) ⬇️
plugins-lodash 25.66% <ø> (ø)
plugins-mariadb 36.50% <ø> (-0.09%) ⬇️
plugins-memcached 35.26% <ø> (-0.09%) ⬇️
plugins-microgateway-core 36.37% <ø> (-0.09%) ⬇️
plugins-moleculer 38.04% <ø> (-0.09%) ⬇️
plugins-mongodb 36.38% <ø> (-0.09%) ⬇️
plugins-mongodb-core 36.07% <ø> (-0.12%) ⬇️
plugins-mongoose 36.05% <ø> (-0.09%) ⬇️
plugins-multer 26.31% <ø> (ø)
plugins-mysql 36.34% <ø> (-0.09%) ⬇️
plugins-mysql2 36.34% <ø> (-0.09%) ⬇️
plugins-node-serialize 26.60% <ø> (ø)
plugins-opensearch 34.94% <ø> (-0.09%) ⬇️
plugins-passport-http 26.37% <ø> (ø)
plugins-postgres 34.32% <ø> (-0.09%) ⬇️
plugins-process 26.21% <ø> (ø)
plugins-pug 26.56% <ø> (ø)
plugins-redis 35.91% <ø> (-0.09%) ⬇️
plugins-router 40.01% <ø> (-0.09%) ⬇️
plugins-sequelize 25.19% <ø> (ø)
plugins-test-and-upstream-amqp10 35.63% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-amqplib 40.83% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-apollo 36.53% <ø> (-0.08%) ⬇️
plugins-test-and-upstream-avsc 35.63% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-bunyan 31.28% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-connect 37.84% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-graphql 37.18% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-koa 37.49% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-protobufjs 35.85% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-rhea 40.93% <ø> (-0.09%) ⬇️
plugins-undici 36.60% <ø> (-0.09%) ⬇️
plugins-url 26.21% <ø> (ø)
plugins-valkey 35.29% <ø> (-0.09%) ⬇️
plugins-vm 26.21% <ø> (ø)
plugins-winston 31.72% <ø> (-0.09%) ⬇️
plugins-ws 38.94% <ø> (-0.09%) ⬇️
profiling-macos 38.06% <ø> (-0.09%) ⬇️
profiling-ubuntu 38.23% <ø> (-0.49%) ⬇️
profiling-windows 39.53% <ø> (-0.09%) ⬇️
serverless-azure-functions-client 25.26% <ø> (ø)
serverless-azure-functions-eventhubs 25.26% <ø> (ø)
serverless-azure-functions-servicebus 25.26% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

Overall package size

Self size: 5.47 MB
Deduped: 6.31 MB
No deduping: 6.31 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.1 | 82.56 kB | 817.39 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-prod-us1-5
Copy link
Copy Markdown

datadog-prod-us1-5 bot commented Apr 8, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 0.00%
Overall Coverage: 68.30% (+0.01%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 3fd5b18 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@rochdev rochdev marked this pull request as ready for review April 8, 2026 01:56
@rochdev rochdev requested a review from a team as a code owner April 8, 2026 01:56
@rochdev
Copy link
Copy Markdown
Member Author

rochdev commented Apr 8, 2026

@crysmags Does this seem like it should work the same as #7000? It seems like it to me but I'm not sure if there were any edge cases you know about where the change wouldn't work.

Copy link
Copy Markdown
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, this is a nice fix!

@rochdev rochdev force-pushed the fix-next-req-map-leak branch from 41a5e4d to 3fd5b18 Compare April 8, 2026 22:23
@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Apr 8, 2026

Benchmarks

Benchmark execution time: 2026-04-08 22:33:45

Comparing candidate commit 3fd5b18 in PR branch fix-next-req-map-leak with baseline commit edef930 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 229 metrics, 31 unstable metrics.

@BridgeAR BridgeAR merged commit 7e59939 into master Apr 8, 2026
923 of 927 checks passed
@BridgeAR BridgeAR deleted the fix-next-req-map-leak branch April 8, 2026 22:46
dd-octo-sts bot pushed a commit that referenced this pull request Apr 9, 2026
The WeakMap was effectively creating a strong reference and memory
was not freed anymore. Instead, rely on the storage for state handling.
@dd-octo-sts dd-octo-sts bot mentioned this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants