Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

enque callback called twice when flush is triggered #308

Closed
yo1dog opened this issue Nov 12, 2021 · 1 comment · Fixed by #315
Closed

enque callback called twice when flush is triggered #308

yo1dog opened this issue Nov 12, 2021 · 1 comment · Fixed by #315

Comments

@yo1dog
Copy link

yo1dog commented Nov 12, 2021

The callback passed to enque can be called twice if a flush is triggered. The callback is passed both to the queued message and to flush:

analytics-node/index.js

Lines 210 to 223 in 70f24fe

this.queue.push({ message, callback })
if (!this.flushed) {
this.flushed = true
this.flush(callback)
return
}
const hasReachedFlushAt = this.queue.length >= this.flushAt
const hasReachedQueueSize = this.queue.reduce((acc, item) => acc + JSON.stringify(item).length, 0) >= this.maxQueueSize
if (hasReachedFlushAt || hasReachedQueueSize) {
this.flush(callback)
return
}

When flush's done function is called, the callback is called twice: Once from the callbacks array (as it was added to the array from the queued message) and again when callback is called (as it was passed to flush as the callback argument).

analytics-node/index.js

Lines 265 to 268 in 70f24fe

const done = err => {
callbacks.forEach(callback => callback(err))
callback(err, data)
}

The fix is to not pass enque's callback to flush.

In general, a callback should never have more than 1 handler.

@felipe-najson-ntf
Copy link
Contributor

felipe-najson-ntf commented Nov 19, 2021

Hello @yo1dog, this issue will be solved with pr #299 which I requested to merge ASAP.
Thanks for your contribution again!

Segment team

@nd4p90x nd4p90x linked a pull request Dec 8, 2021 that will close this issue
@nd4p90x nd4p90x closed this as completed Dec 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants