Skip to content

Commit 46fbc23

Browse files
addaleaxtargos
authored andcommitted
lib,src: standardize owner_symbol for handles
Instead of somtimes using an `owner` string to link from a native handle object to the corresponding JS object, standardize on a single symbol that fulfills this role. PR-URL: #22002 Backport-PR-URL: #22507 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 198cf41 commit 46fbc23

14 files changed

+91
-48
lines changed

lib/_tls_wrap.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const debug = util.debuglog('tls');
3535
const tls_wrap = process.binding('tls_wrap');
3636
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
3737
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
38+
const { owner_symbol } = require('internal/async_hooks').symbols;
3839
const {
3940
SecureContext: NativeSecureContext
4041
} = process.binding('crypto');
@@ -77,7 +78,7 @@ function onhandshakestart(now) {
7778
else
7879
this.handshakes++;
7980

80-
const { owner } = this;
81+
const owner = this[owner_symbol];
8182
if (this.handshakes > tls.CLIENT_RENEG_LIMIT) {
8283
owner._emitTLSError(new ERR_TLS_SESSION_ATTACK());
8384
return;
@@ -90,7 +91,7 @@ function onhandshakestart(now) {
9091
function onhandshakedone() {
9192
debug('onhandshakedone');
9293

93-
const owner = this.owner;
94+
const owner = this[owner_symbol];
9495

9596
// `newSession` callback wasn't called yet
9697
if (owner._newSessionPending) {
@@ -103,7 +104,7 @@ function onhandshakedone() {
103104

104105

105106
function loadSession(hello) {
106-
const owner = this.owner;
107+
const owner = this[owner_symbol];
107108

108109
var once = false;
109110
function onSession(err, session) {
@@ -131,7 +132,7 @@ function loadSession(hello) {
131132

132133

133134
function loadSNI(info) {
134-
const owner = this.owner;
135+
const owner = this[owner_symbol];
135136
const servername = info.servername;
136137
if (!servername || !owner._SNICallback)
137138
return requestOCSP(owner, info);
@@ -213,7 +214,7 @@ function requestOCSPDone(socket) {
213214

214215

215216
function onnewsession(key, session) {
216-
const owner = this.owner;
217+
const owner = this[owner_symbol];
217218

218219
if (!owner.server)
219220
return;
@@ -242,11 +243,11 @@ function onnewsession(key, session) {
242243

243244

244245
function onocspresponse(resp) {
245-
this.owner.emit('OCSPResponse', resp);
246+
this[owner_symbol].emit('OCSPResponse', resp);
246247
}
247248

248249
function onerror(err) {
249-
const owner = this.owner;
250+
const owner = this[owner_symbol];
250251

251252
if (owner._writableState.errorEmitted)
252253
return;
@@ -365,9 +366,9 @@ for (var n = 0; n < proxiedMethods.length; n++) {
365366

366367
tls_wrap.TLSWrap.prototype.close = function close(cb) {
367368
let ssl;
368-
if (this.owner) {
369-
ssl = this.owner.ssl;
370-
this.owner.ssl = null;
369+
if (this[owner_symbol]) {
370+
ssl = this[owner_symbol].ssl;
371+
this[owner_symbol].ssl = null;
371372
}
372373

373374
// Invoke `destroySSL` on close to clean up possibly pending write requests
@@ -406,7 +407,7 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
406407
handle = options.pipe ?
407408
new Pipe(PipeConstants.SOCKET) :
408409
new TCP(TCPConstants.SOCKET);
409-
handle.owner = this;
410+
handle[owner_symbol] = this;
410411
}
411412

412413
// Wrap socket's handle

lib/dgram.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const { isUint8Array } = require('internal/util/types');
4747
const EventEmitter = require('events');
4848
const {
4949
defaultTriggerAsyncIdScope,
50-
symbols: { async_id_symbol }
50+
symbols: { async_id_symbol, owner_symbol }
5151
} = require('internal/async_hooks');
5252
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
5353

@@ -82,7 +82,7 @@ function Socket(type, listener) {
8282
}
8383

8484
var handle = newHandle(type, lookup);
85-
handle.owner = this;
85+
handle[owner_symbol] = this;
8686

8787
this[async_id_symbol] = handle.getAsyncId();
8888
this.type = type;
@@ -136,7 +136,7 @@ function replaceHandle(self, newHandle) {
136136
newHandle.lookup = oldHandle.lookup;
137137
newHandle.bind = oldHandle.bind;
138138
newHandle.send = oldHandle.send;
139-
newHandle.owner = self;
139+
newHandle[owner_symbol] = self;
140140

141141
// Replace the existing handle by the handle we got from master.
142142
oldHandle.close();
@@ -620,7 +620,7 @@ function stopReceiving(socket) {
620620

621621

622622
function onMessage(nread, handle, buf, rinfo) {
623-
var self = handle.owner;
623+
var self = handle[owner_symbol];
624624
if (nread < 0) {
625625
return self.emit('error', errnoException(nread, 'recvmsg'));
626626
}
@@ -730,6 +730,14 @@ Socket.prototype._stopReceiving = function() {
730730
};
731731

732732

733+
// Legacy alias on the C++ wrapper object. This is not public API, so we may
734+
// want to runtime-deprecate it at some point. There's no hurry, though.
735+
Object.defineProperty(UDP.prototype, 'owner', {
736+
get() { return this[owner_symbol]; },
737+
set(v) { return this[owner_symbol] = v; }
738+
});
739+
740+
733741
module.exports = {
734742
_createSocketHandle,
735743
createSocket,

lib/internal/async_hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const async_wrap = process.binding('async_wrap');
3030
* It has a fixed size, so if that is exceeded, calls to the native
3131
* side are used instead in pushAsyncIds() and popAsyncIds().
3232
*/
33-
const { async_hook_fields, async_id_fields } = async_wrap;
33+
const { async_hook_fields, async_id_fields, owner_symbol } = async_wrap;
3434
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
3535
// Environment::AsyncHooks::async_ids_stack_ tracks the resource responsible for
3636
// the current execution stack. This is unwound as each resource exits. In the
@@ -434,7 +434,7 @@ module.exports = {
434434
symbols: {
435435
async_id_symbol, trigger_async_id_symbol,
436436
init_symbol, before_symbol, after_symbol, destroy_symbol,
437-
promise_resolve_symbol
437+
promise_resolve_symbol, owner_symbol
438438
},
439439
constants: {
440440
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve

lib/internal/child_process.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const { TTY } = process.binding('tty_wrap');
2828
const { TCP } = process.binding('tcp_wrap');
2929
const { UDP } = process.binding('udp_wrap');
3030
const SocketList = require('internal/socket_list');
31+
const { owner_symbol } = require('internal/async_hooks').symbols;
3132
const { convertToValidSignal } = require('internal/util');
3233
const { isUint8Array } = require('internal/util/types');
3334
const spawn_sync = process.binding('spawn_sync');
@@ -210,7 +211,7 @@ function ChildProcess() {
210211
this.spawnfile = null;
211212

212213
this._handle = new Process();
213-
this._handle.owner = this;
214+
this._handle[owner_symbol] = this;
214215

215216
this._handle.onexit = (exitCode, signalCode) => {
216217
if (signalCode) {

lib/internal/cluster/child.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = require('assert');
33
const util = require('util');
44
const path = require('path');
55
const EventEmitter = require('events');
6+
const { owner_symbol } = require('internal/async_hooks').symbols;
67
const Worker = require('internal/cluster/worker');
78
const { internal, sendHelper } = require('internal/cluster/utils');
89
const cluster = new EventEmitter();
@@ -207,8 +208,8 @@ function _disconnect(masterInitiated) {
207208
delete handles[key];
208209
waitingCount++;
209210

210-
if (handle.owner)
211-
handle.owner.close(checkWaitingCount);
211+
if (handle[owner_symbol])
212+
handle[owner_symbol].close(checkWaitingCount);
212213
else
213214
handle.close(checkWaitingCount);
214215
}

lib/internal/fs/watchers.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const {
1111
getStatsFromBinding,
1212
validatePath
1313
} = require('internal/fs/utils');
14-
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
14+
const {
15+
defaultTriggerAsyncIdScope,
16+
symbols: { owner_symbol }
17+
} = require('internal/async_hooks');
1518
const { toNamespacedPath } = require('path');
1619
const { validateUint32 } = require('internal/validators');
1720
const { getPathFromURL } = require('internal/url');
@@ -20,7 +23,6 @@ const assert = require('assert');
2023

2124
const kOldStatus = Symbol('kOldStatus');
2225
const kUseBigint = Symbol('kUseBigint');
23-
const kOwner = Symbol('kOwner');
2426

2527
function emitStop(self) {
2628
self.emit('stop');
@@ -36,7 +38,7 @@ function StatWatcher(bigint) {
3638
util.inherits(StatWatcher, EventEmitter);
3739

3840
function onchange(newStatus, stats) {
39-
const self = this[kOwner];
41+
const self = this[owner_symbol];
4042
if (self[kOldStatus] === -1 &&
4143
newStatus === -1 &&
4244
stats[2/* new nlink */] === stats[16/* old nlink */]) {
@@ -59,7 +61,7 @@ StatWatcher.prototype.start = function(filename, persistent, interval) {
5961
return;
6062

6163
this._handle = new _StatWatcher(this[kUseBigint]);
62-
this._handle[kOwner] = this;
64+
this._handle[owner_symbol] = this;
6365
this._handle.onchange = onchange;
6466
if (!persistent)
6567
this._handle.unref();
@@ -104,7 +106,7 @@ function FSWatcher() {
104106
EventEmitter.call(this);
105107

106108
this._handle = new FSEvent();
107-
this._handle.owner = this;
109+
this._handle[owner_symbol] = this;
108110

109111
this._handle.onchange = (status, eventType, filename) => {
110112
// TODO(joyeecheung): we may check self._handle.initialized here
@@ -131,6 +133,7 @@ function FSWatcher() {
131133
}
132134
util.inherits(FSWatcher, EventEmitter);
133135

136+
134137
// FIXME(joyeecheung): this method is not documented.
135138
// At the moment if filename is undefined, we
136139
// 1. Throw an Error if it's the first time .start() is called
@@ -187,6 +190,13 @@ function emitCloseNT(self) {
187190
self.emit('close');
188191
}
189192

193+
// Legacy alias on the C++ wrapper object. This is not public API, so we may
194+
// want to runtime-deprecate it at some point. There's no hurry, though.
195+
Object.defineProperty(FSEvent.prototype, 'owner', {
196+
get() { return this[owner_symbol]; },
197+
set(v) { return this[owner_symbol] = v; }
198+
});
199+
190200
module.exports = {
191201
FSWatcher,
192202
StatWatcher

lib/internal/http2/core.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
defaultTriggerAsyncIdScope,
2929
symbols: {
3030
async_id_symbol,
31+
owner_symbol,
3132
},
3233
} = require('internal/async_hooks');
3334
const { internalBinding } = require('internal/bootstrap/loaders');
@@ -144,7 +145,7 @@ const kInfoHeaders = Symbol('sent-info-headers');
144145
const kMaybeDestroy = Symbol('maybe-destroy');
145146
const kLocalSettings = Symbol('local-settings');
146147
const kOptions = Symbol('options');
147-
const kOwner = Symbol('owner');
148+
const kOwner = owner_symbol;
148149
const kProceed = Symbol('proceed');
149150
const kProtocol = Symbol('protocol');
150151
const kProxySocket = Symbol('proxy-socket');

lib/internal/wrap_js_stream.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ const { Socket } = require('net');
66
const { JSStream } = process.binding('js_stream');
77
const uv = process.binding('uv');
88
const debug = util.debuglog('stream_wrap');
9+
const { owner_symbol } = require('internal/async_hooks').symbols;
910
const { ERR_STREAM_WRAP } = require('internal/errors').codes;
1011

1112
const kCurrentWriteRequest = Symbol('kCurrentWriteRequest');
1213
const kCurrentShutdownRequest = Symbol('kCurrentShutdownRequest');
1314

14-
function isClosing() { return this.owner.isClosing(); }
15-
function onreadstart() { return this.owner.readStart(); }
16-
function onreadstop() { return this.owner.readStop(); }
17-
function onshutdown(req) { return this.owner.doShutdown(req); }
18-
function onwrite(req, bufs) { return this.owner.doWrite(req, bufs); }
15+
function isClosing() { return this[owner_symbol].isClosing(); }
16+
function onreadstart() { return this[owner_symbol].readStart(); }
17+
function onreadstop() { return this[owner_symbol].readStop(); }
18+
function onshutdown(req) { return this[owner_symbol].doShutdown(req); }
19+
function onwrite(req, bufs) { return this[owner_symbol].doWrite(req, bufs); }
1920

2021
/* This class serves as a wrapper for when the C++ side of Node wants access
2122
* to a standard JS stream. For example, TLS or HTTP do not operate on network
@@ -37,7 +38,7 @@ class JSStreamWrap extends Socket {
3738
this.doClose(cb);
3839
};
3940
// Inside of the following functions, `this` refers to the handle
40-
// and `this.owner` refers to this JSStreamWrap instance.
41+
// and `this[owner_symbol]` refers to this JSStreamWrap instance.
4142
handle.isClosing = isClosing;
4243
handle.onreadstart = onreadstart;
4344
handle.onreadstop = onreadstop;

lib/net.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const {
5656
const {
5757
newAsyncId,
5858
defaultTriggerAsyncIdScope,
59-
symbols: { async_id_symbol }
59+
symbols: { async_id_symbol, owner_symbol }
6060
} = require('internal/async_hooks');
6161
const {
6262
createWriteWrap,
@@ -207,7 +207,7 @@ function initSocketHandle(self) {
207207

208208
// Handle creation may be deferred to bind() or connect() time.
209209
if (self._handle) {
210-
self._handle.owner = self;
210+
self._handle[owner_symbol] = self;
211211
self._handle.onread = onread;
212212
self[async_id_symbol] = getNewAsyncId(self._handle);
213213
}
@@ -371,7 +371,7 @@ Socket.prototype._final = function(cb) {
371371

372372

373373
function afterShutdown(status, handle) {
374-
var self = handle.owner;
374+
var self = handle[owner_symbol];
375375

376376
debug('afterShutdown destroyed=%j', self.destroyed,
377377
self._readableState);
@@ -620,7 +620,7 @@ Socket.prototype._destroy = function(exception, cb) {
620620
// buffer, or when there's an error reading.
621621
function onread(nread, buffer) {
622622
var handle = this;
623-
var self = handle.owner;
623+
var self = handle[owner_symbol];
624624
assert(handle === self._handle, 'handle != self._handle');
625625

626626
self._unrefTimer();
@@ -819,7 +819,7 @@ protoGetter('bytesWritten', function bytesWritten() {
819819

820820

821821
function afterWrite(status, handle, err) {
822-
var self = handle.owner;
822+
var self = handle[owner_symbol];
823823
if (self !== process.stderr && self !== process.stdout)
824824
debug('afterWrite', status);
825825

@@ -1121,7 +1121,7 @@ Socket.prototype.unref = function() {
11211121

11221122

11231123
function afterConnect(status, handle, req, readable, writable) {
1124-
var self = handle.owner;
1124+
var self = handle[owner_symbol];
11251125

11261126
// callback may come after call to destroy
11271127
if (self.destroyed) {
@@ -1323,7 +1323,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
13231323

13241324
this[async_id_symbol] = getNewAsyncId(this._handle);
13251325
this._handle.onconnection = onconnection;
1326-
this._handle.owner = this;
1326+
this._handle[owner_symbol] = this;
13271327

13281328
// Use a backlog of 512 entries. We pass 511 to the listen() call because
13291329
// the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
@@ -1536,7 +1536,7 @@ Server.prototype.address = function() {
15361536

15371537
function onconnection(err, clientHandle) {
15381538
var handle = this;
1539-
var self = handle.owner;
1539+
var self = handle[owner_symbol];
15401540

15411541
debug('onconnection');
15421542

@@ -1667,6 +1667,14 @@ function emitCloseNT(self) {
16671667
}
16681668

16691669

1670+
// Legacy alias on the C++ wrapper object. This is not public API, so we may
1671+
// want to runtime-deprecate it at some point. There's no hurry, though.
1672+
Object.defineProperty(TCP.prototype, 'owner', {
1673+
get() { return this[owner_symbol]; },
1674+
set(v) { return this[owner_symbol] = v; }
1675+
});
1676+
1677+
16701678
Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
16711679
return this.listen({ fd: fd });
16721680
}, 'Server.listenFD is deprecated. Use Server.listen({fd: <number>}) instead.',

0 commit comments

Comments
 (0)