Skip to content

setTimeout callback order isn't guaranteed #13579

Closed
@sam-github

Description

@sam-github

Perhaps I expect too much, but I expect a sequence of

setTimeout(100, a)
setTimeout(100, b)

to always call a before b, but this doesn't happen. I was looking at this because @hhellyer pointed out that unrefed and refed timers use different timer handles (expected), so I looked, and see that they are in two lists, however, the lists are internally designed so that new callbacks for the same time are appended to a list, guaranteeing order, but it isn't clear that those ordering guarantees are maintained across refed and unrefed timers, since they use different lists.

Or, perhaps I'm seeing an artifact of timer accuracy?

@Fishrock123 @mscdex You two seem familiar with the timers, what do you think, is this a problem?

_.js

function F(s) {
  return () => console.log(s);
}

setTimeout(F('A'), 100)
setTimeout(F('B'), 100).unref()
setTimeout(F('C'), 100)
setTimeout(F('D'), 100).unref()

Output:

% ./node -v
v9.0.0-pre
% ./node _.js
A
C
B
D
% node -v
v8.0.0
% node _.js
A
B
D
C
% node _.js
A
C
B
D
% node _.js
A
C
B
D
core/node (check-is-listening $%) % node -v
v8.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.questionIssues that look for answers.timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions