Closed
Description
- Version: 8.x
- Platform: docker ( official node as well as mhart/alpine-node)
- Subsystem: core
The following minimal sample code results in reproducible, unexpected behavior:
const one = 150
const two = 2
let counter = 0
setInterval(() => {
const res = Math.max(5, Math.floor(one / two))
if (res > 100) {
console.log(counter, res)
} else {
counter++
}
}, 1)
Expected behavior
This code should never output anything, because the calculation result is always 75
Actual behavior
After a while, the code starts to output the value of the variable one
. This happens reliably once at counter value 5.398 and at each iteration starting from 10.794. The behavior is identical (even more or less the counter values) when changing the values of the variables or making the timer slower. The actual bug seems to happen within the Math.max
, the result of Math.floor
looks good.
Side notes
- reliably happens when using
node:8
,node:8.9
,node:8.11
,mhart/alpine-node:8.9
(and more) docker images, both on Mac and Linux host systems - doesn't happen when changing the
setInterval
to awhile loop
- doesn't happen when using
node:6
ornode:10
docker images - bug first occured within a big software project and was easily reproducible in this minimal sample
- the counter isn't needed for showing the bug, it's just to demonstrate how deterministic the problem seems to be
- even when splitting the calculation into
const floored = Math.floor(one / two)
const res = Math.max(5, floored)
res
will carry the value of one
after around 10.000 iterations
I don't get what happens under the hood, but I assume this is critical.