Skip to content

Commit f6182bd

Browse files
niieanibbrzoska
authored andcommitted
fix: better memory cleanup
1 parent 4379334 commit f6182bd

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/v3/Trace.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,8 @@ export class Trace<
12481248
transition.transitionToState === 'complete'
12491249
) {
12501250
const traceRecording = createTraceRecording(
1251+
// we don't want to pass 'this' but select the relevant properties
1252+
// to avoid circular references
12511253
{
12521254
definition: this.definition,
12531255
recordedItems: this.recordedItems,
@@ -1259,9 +1261,11 @@ export class Trace<
12591261
this.onEnd(traceRecording)
12601262

12611263
// memory clean-up in case something retains the Trace instance
1262-
this.recordedItems.clear()
1263-
this.occurrenceCounters.clear()
1264+
this.recordedItems = new Set()
1265+
this.occurrenceCounters = new Map()
12641266
this.processedPerformanceEntries = new WeakMap()
1267+
// @ts-expect-error memory cleanup force override the otherwise readonly property
1268+
this.recordedItemsByLabel = {}
12651269
this.traceUtilities.performanceEntryDeduplicationStrategy?.reset()
12661270
}
12671271
},

src/v3/recordingComputeUtils.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ describe('recordingComputeUtils', () => {
5959
},
6060
recordedItemsByLabel: {},
6161
},
62-
{ transitionFromState: 'active' },
62+
{
63+
transitionFromState: 'active',
64+
lastRelevantSpanAndAnnotation: undefined,
65+
transitionToState: 'complete',
66+
},
6367
)
6468

6569
expect(recording.status).toBe('error')
@@ -92,7 +96,11 @@ describe('recordingComputeUtils', () => {
9296
},
9397
recordedItemsByLabel: {},
9498
},
95-
{ transitionFromState: 'active' },
99+
{
100+
transitionFromState: 'active',
101+
lastRelevantSpanAndAnnotation: undefined,
102+
transitionToState: 'complete',
103+
},
96104
)
97105

98106
expect(recording.status).toBe('ok')
@@ -127,7 +135,11 @@ describe('recordingComputeUtils', () => {
127135
},
128136
recordedItemsByLabel: {},
129137
},
130-
{ transitionFromState: 'active' },
138+
{
139+
transitionFromState: 'active',
140+
lastRelevantSpanAndAnnotation: undefined,
141+
transitionToState: 'complete',
142+
},
131143
)
132144

133145
expect(recording.status).toBe('error')

src/v3/recordingComputeUtils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ export function createTraceRecording<
379379
>(
380380
context: TraceContext<SelectedRelationNameT, RelationSchemasT, VariantsT>,
381381
{
382-
transitionFromState,
383382
transitionToState,
384383
interruptionReason,
385384
cpuIdleSpanAndAnnotation,

0 commit comments

Comments
 (0)