Skip to content

Commit 4e420cb

Browse files
authored
Ensure collected data is sent to the agent even if flushInterval is negative (#5923)
If the tracer was configured with a negative `flushInterval`, it would never flush its internal state to the agent and would keep consuming memory indefinitely until either the process exited naturally, or ran out of memory. Other minor code changes: - Once a timer has fired, there's no need to call `clearTimeout` on it. - Additionally, the fact that `clearTimeout` returns `undefined`, is just because it doesn't return anything. So if we want to set the reference to the timer to `undefined`, just do so directly instead of relying on the return value of `clearTimeout`.
1 parent a65d074 commit 4e420cb

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

packages/dd-trace/src/exporters/agent/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const log = require('../../log')
55
const Writer = require('./writer')
66

77
class AgentExporter {
8+
#timer
9+
810
constructor (config, prioritySampler) {
911
this._config = config
1012
const { url, hostname, port, lookup, protocolVersion, stats = {}, apmTracingEnabled } = config
@@ -28,8 +30,11 @@ class AgentExporter {
2830
config
2931
})
3032

31-
this._timer = undefined
32-
process.once('beforeExit', () => this._writer.flush())
33+
process.once('beforeExit', () => {
34+
clearTimeout(this.#timer)
35+
this.#timer = undefined
36+
this._writer.flush()
37+
})
3338
}
3439

3540
setUrl (url) {
@@ -49,10 +54,10 @@ class AgentExporter {
4954

5055
if (flushInterval === 0) {
5156
this._writer.flush()
52-
} else if (flushInterval > 0 && !this._timer) {
53-
this._timer = setTimeout(() => {
57+
} else if (this.#timer === undefined) {
58+
this.#timer = setTimeout(() => {
5459
this._writer.flush()
55-
this._timer = clearTimeout(this._timer)
60+
this.#timer = undefined
5661
}, flushInterval).unref()
5762
}
5863
}

packages/dd-trace/src/exporters/common/agent-info-exporter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ class AgentInfoExporter {
5959

6060
if (flushInterval === 0) {
6161
writer.flush()
62-
} else if (flushInterval > 0 && !this[timerKey]) {
62+
} else if (this[timerKey] === undefined) {
6363
this[timerKey] = setTimeout(() => {
6464
writer.flush()
65-
this[timerKey] = clearTimeout(this[timerKey])
65+
this[timerKey] = undefined
6666
}, flushInterval).unref()
6767
}
6868
}

0 commit comments

Comments
 (0)