-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
handle_wrap: expose {un}refed check to JS #5834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const strictEqual = require('assert').strictEqual; | ||
const spawn = require('child_process').spawn; | ||
|
||
function makeAssert(message) { | ||
return function(actual, expected) { | ||
strictEqual(actual, expected, message); | ||
}; | ||
} | ||
const assert = makeAssert('isRefed() not working on tty_wrap'); | ||
|
||
if (process.argv[2] === 'child') { | ||
// Test tty_wrap in piped child to guarentee stdin being a TTY. | ||
const ReadStream = require('tty').ReadStream; | ||
const tty = new ReadStream(0); | ||
assert(Object.getPrototypeOf(tty._handle).hasOwnProperty('isRefed'), true); | ||
assert(tty._handle.isRefed(), true); | ||
tty.unref(); | ||
assert(tty._handle.isRefed(), false); | ||
return; | ||
} | ||
|
||
// Use spawn so that we can be sure that stdin has a _handle property. | ||
// Refs: https://github.com/nodejs/node/pull/5916 | ||
const proc = spawn(process.execPath, [__filename, 'child'], { stdio: 'pipe' }); | ||
proc.stderr.pipe(process.stderr); | ||
proc.on('exit', common.mustCall(function(exitCode) { | ||
process.exitCode = exitCode; | ||
})); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,97 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'use strict'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const common = require('../common'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const strictEqual = require('assert').strictEqual; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
function makeAssert(message) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return function(actual, expected) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
strictEqual(actual, expected, message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// child_process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const assert = makeAssert('isRefed() not working on process_wrap'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const spawn = require('child_process').spawn; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const cmd = common.isWindows ? 'rundll32' : 'ls'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const cp = spawn(cmd); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(Object.getPrototypeOf(cp._handle).hasOwnProperty('isRefed'), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(cp._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cp.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(cp._handle.isRefed(), false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cp.ref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(cp._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cp.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// dgram | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const assert = makeAssert('isRefed() not working on udp_wrap'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const dgram = require('dgram'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const sock4 = dgram.createSocket('udp4'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(Object.getPrototypeOf(sock4._handle).hasOwnProperty('isRefed'), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(sock4._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sock4.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(sock4._handle.isRefed(), false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sock4.ref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(sock4._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sock4.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const sock6 = dgram.createSocket('udp6'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(Object.getPrototypeOf(sock6._handle).hasOwnProperty('isRefed'), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(sock6._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sock6.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(sock6._handle.isRefed(), false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sock6.ref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(sock6._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sock6.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// pipe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const assert = makeAssert('isRefed() not working on pipe_wrap'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const Pipe = process.binding('pipe_wrap').Pipe; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const handle = new Pipe(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(Object.getPrototypeOf(handle).hasOwnProperty('isRefed'), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
handle.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(handle.isRefed(), false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
handle.ref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
handle.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// tcp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const assert = makeAssert('isRefed() not working on tcp_wrap'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const net = require('net'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const server = net.createServer(() => {}).listen(common.PORT); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(Object.getPrototypeOf(server._handle).hasOwnProperty('isRefed'), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(server._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(server._unref, false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
server.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(server._handle.isRefed(), false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(server._unref, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
server.ref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(server._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(server._unref, false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
server.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// timers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const assert = makeAssert('isRefed() not working on timer_wrap'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const timer = setTimeout(() => {}, 500); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
timer.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(Object.getPrototypeOf(timer._handle).hasOwnProperty('isRefed'), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(timer._handle.isRefed(), false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
timer.ref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(timer._handle.isRefed(), true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
timer.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Timeout.prototype.unref = function() { | |
if (this._handle) { | |
this._handle.unref(); | |
} else if (typeof this._onTimeout === 'function') { | |
var now = TimerWrap.now(); | |
if (!this._idleStart) this._idleStart = now; | |
var delay = this._idleStart + this._idleTimeout - now; | |
if (delay < 0) delay = 0; | |
// Prevent running cb again when unref() is called during the same cb | |
if (this._called && !this._repeat) { | |
exports.unenroll(this); | |
return; | |
} | |
var handle = reuse(this); | |
this._handle = handle || new TimerWrap(); | |
this._handle.owner = this; | |
this._handle[kOnTimeout] = unrefdHandle; | |
this._handle.start(delay, 0); | |
this._handle.domain = this.domain; | |
this._handle.unref(); | |
} | |
return this; | |
}; |
timer.unref()
assigns a ._handle
for that specific timer, and is unlikely to change in the short term.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize that. What I failed to say is that a note of this should be placed in timer_wrap.cc
where the method is added so future generations can immediately see why this non-obvious implementation detail actually works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where exactly do you mean? Isn't the same thing for the un(ref)
methods on timer_wrap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is it may be non-obvious why setTimeout(()=>{}, 100)._handle === undefined
but setTimeout(()=>{}, 100).unref()._handle
links to a Timer
instance. So using isRefed()
is conditional to the state of timer. So not having a uniform way to access that data from the public object instance could be confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but the regular timers implementation will also benefit from this. e.g.
Lines 210 to 214 in cf94929
if (list._unrefed === true) { | |
delete unrefedLists[msecs]; | |
} else { | |
delete refedLists[msecs]; | |
} |
So I'm not sure what I would say in handle_wrap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: This is unsafe, but is currently being done elsewhere in core. Am going to investigate how to not do this in the future.