Skip to content

Commit 39ab054

Browse files
committed
fix(telemetry): guard user message decrement and fix idle re-fire logic
1 parent 07c5890 commit 39ab054

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/core/task/Task.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4782,12 +4782,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
47824782

47834783
startIdleTelemetryCheck(): void {
47844784
this.idleTelemetryCheckInterval = setInterval(() => {
4785-
// lastMessageTs only moves forward on activity, so comparing it against the
4786-
// last flush tells us whether anything happened since that flush -- if the
4787-
// task has been quiet since well before the last flush, there's nothing new
4788-
// to report and flushTelemetryInstallment's own empty-check would no-op anyway,
4789-
// but skipping here avoids waking up to do that check needlessly.
4790-
const idleForMs = Date.now() - (this.lastMessageTs ?? this.lastTelemetryFlushAt)
4785+
// Measure idleness from the later of the last activity and the last flush.
4786+
// Using lastMessageTs alone would keep the condition true forever after the
4787+
// first idle flush, re-running the empty-delta check on every interval tick.
4788+
const lastEventAt = Math.max(this.lastMessageTs ?? 0, this.lastTelemetryFlushAt)
4789+
const idleForMs = Date.now() - lastEventAt
47914790

47924791
if (idleForMs >= Task.IDLE_TELEMETRY_THRESHOLD_MS) {
47934792
this.flushTelemetryInstallment("idle")

src/core/task/__tests__/Task.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,7 +3118,13 @@ describe("Telemetry installments (idle/shutdown flush)", () => {
31183118
}
31193119
})
31203120

3121+
const createdTasks: Task[] = []
3122+
31213123
afterEach(() => {
3124+
for (const task of createdTasks) {
3125+
task.dispose()
3126+
}
3127+
createdTasks.length = 0
31223128
vi.useRealTimers()
31233129
captureTaskCompletedSpy.mockRestore()
31243130
})
@@ -3131,6 +3137,7 @@ describe("Telemetry installments (idle/shutdown flush)", () => {
31313137
startTask: false,
31323138
})
31333139
task.startIdleTelemetryCheck()
3140+
createdTasks.push(task)
31343141
return task
31353142
}
31363143

0 commit comments

Comments
 (0)