Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Unreferenced timers run during beforeExit timeframe #14171

Description

@brycebaril

Cross-posting from nodejs/node#1264

The gist of the problem is the beforeExit implementation causes inconsistent and racy situations in timers when the application is closing, a race which can result in an application failing to close and hanging on unreferenced timers. Unreferenced timers will be executed when they shouldn't have been, and will keep the application alive longer as a result.

Sample code:

var assert = require("assert")
var intervals = 0
var i = setInterval(function () {
  intervals++
  i.unref()
  eatTime()
}, 10)

function eatTime() {
  // the goal of this function is to take longer than the interval
  var count = 0
  while (count++ < 1e7) {
    Math.random()
  }
}

process.on("exit", function () {
  assert.equal(intervals, 1)
})

0.12.1:

bryce@x220:/tmp$ node -v
v0.12.1
bryce@x220:/tmp$ node ah14.js

assert.js:86
  throw new assert.AssertionError({
        ^
AssertionError: 3 == 1
    at process.<anonymous> (/tmp/ah14.js:17:10)
    at process.emit (events.js:107:17)

0.10.38

bryce@x220:/tmp$ node -v
v0.10.38
bryce@x220:/tmp$ node ah14.js 
bryce@x220:/tmp$ 

This impacts 0.11.12 and higher, including 0.12.0 and 0.12.1.

It works as expected in 0.10 (which has no beforeExit)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions