FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

timers: assure setTimeout callback only runs once by silverwind · Pull Request #1231 · nodejs/node · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 7 additions & 0 deletions lib/timers.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function listOnTimeout() {
if (domain)
domain.enter();
threw = true;
first._called = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please add this._called = false to the constructor.

first._onTimeout();
if (domain)
domain.exit();
Expand Down Expand Up @@ -293,6 +294,7 @@ exports.clearInterval = function(timer) {


const Timeout = function(after) {
this._called = false;
this._idleTimeout = after;
this._idlePrev = this;
this._idleNext = this;
Expand All @@ -310,6 +312,10 @@ Timeout.prototype.unref = function() {
var delay = this._idleStart + this._idleTimeout - now;
if (delay < 0) delay = 0;
exports.unenroll(this);

// Prevent running cb again when unref() is called during the same cb
if (this._called && !this._repeat) return;

this._handle = new Timer();
this._handle[kOnTimeout] = this._onTimeout;
this._handle.start(delay, 0);
Expand Down Expand Up @@ -492,6 +498,7 @@ function unrefTimeout() {
if (domain) domain.enter();
threw = true;
debug('unreftimer firing timeout');
first._called = true;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Strictly, this one wasn't neccessary to fix the issue, but I thought it's better to track all callback calls if there are more edge cases.

first._onTimeout();
threw = false;
if (domain)
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-timers-unref.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var interval_fired = false,
timeout_fired = false,
unref_interval = false,
unref_timer = false,
unref_callbacks = 0,
interval, check_unref, checks = 0;

var LONG_TIME = 10 * 1000;
Expand Down Expand Up @@ -34,6 +35,16 @@ check_unref = setInterval(function() {
checks += 1;
}, 100);

setTimeout(function() {
unref_callbacks++;
this.unref();
}, SHORT_TIME);

// Should not timeout the test
setInterval(function() {
this.unref();
}, SHORT_TIME);

// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261.
(function() {
var t = setInterval(function() {}, 1);
Expand All @@ -46,4 +57,5 @@ process.on('exit', function() {
assert.strictEqual(timeout_fired, false, 'Timeout should not fire');
assert.strictEqual(unref_timer, true, 'An unrefd timeout should still fire');
assert.strictEqual(unref_interval, true, 'An unrefd interval should still fire');
assert.strictEqual(unref_callbacks, 1, 'Callback should only run once');
});

Back | FazBrowse Home | New Git URL