Skip to content

Commit 7e59939

Browse files
authored
fix: prevent nextjs memory leak (#7946)
The WeakMap was effectively creating a strong reference and memory was not freed anymore. Instead, rely on the storage for state handling.
1 parent edef930 commit 7e59939

1 file changed

Lines changed: 2 additions & 14 deletions

File tree

  • packages/datadog-plugin-next/src

packages/datadog-plugin-next/src/index.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const errorPages = new Set(['/404', '/500', '/_error', '/_not-found', '/_not-fou
1010

1111
class NextPlugin extends ServerPlugin {
1212
static id = 'next'
13-
#requestsBySpanId = new WeakMap()
1413

1514
constructor (...args) {
1615
super(...args)
@@ -35,11 +34,7 @@ class NextPlugin extends ServerPlugin {
3534

3635
analyticsSampler.sample(span, this.config.measured, true)
3736

38-
// Store request by span ID to handle cases where child spans are activated
39-
const spanId = span.context()._spanId
40-
this.#requestsBySpanId.set(spanId, req)
41-
42-
return { ...store, span }
37+
return { ...store, span, req }
4338
}
4439

4540
error ({ span, error }) {
@@ -90,14 +85,7 @@ class NextPlugin extends ServerPlugin {
9085

9186
if (!store) return
9287

93-
const span = store.span
94-
95-
const spanId = span.context()._spanId
96-
const parentSpanId = span.context()._parentId
97-
98-
// Try current span first, then parent span.
99-
// This handles cases where pageLoad runs in a child span context
100-
const req = this.#requestsBySpanId.get(spanId) ?? this.#requestsBySpanId.get(parentSpanId)
88+
const { span, req } = store
10189

10290
// safeguard against missing req in complicated timeout scenarios
10391
if (!req) return

0 commit comments

Comments
 (0)