-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Allow passing KeyObjects via postMessage #33360
Conversation
assert(!(port2Moved instanceof Object)); | ||
|
||
port2Moved.onmessage = common.mustCall(({ data: { key } }) => { | ||
assert.strictEqual(keyToString(key), repr); |
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 assume that key instanceof Object
is true here, but port2Moved
should only create objects in context
and not in the outer/main context (see also the comment for TransferData::Deserialize()
).
NativeKeyObject::KeyObjectTransferData::Deserialize()
should either fail if context != env->context()
, or env->crypto_key_object_secret_constructor()
etc. need to be stored as templates instead.
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.
Can you give me a hint how to store the constructors as templates? I didn't think it was possible with classes defined in JavaScript :)
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.
Ah right, it’s not … I guess that means that you’ll have to throw an exception here if the contexts mismatch
(The alternative would be giving node_crypto multi-context support, but we’re not really quite there yet I’d say … and in any case, that would be a lot more work than this PR)
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 can't quite figure out where to throw. Throwing in Deserialize
compiles, but then how would the user catch it? It doesn't seem to cause an error
event. Throwing in CloneForMessaging
seems like the better choice to me, but that seems to be even more difficult (since it only happens in SerializerDelegate::Finish
without access to the environment/context etc.). Do you have another hint for me? :)
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.
Hm – Deserialize()
would be the right place, yes. Maybe adding
TryCatchScope try_catch(env());
try_catch.SetVerbose(true);
after the Context::Scope context_scope(context);
line in MessagePort::OnMessage()
helps? It would still result in an uncaught exception, I think, but I’m not really sure what the alternative would be… there is onmessageerror
on the Web MessagePort
, but we’re not currently implementing that.
(I think even just discarding the message would also be fine as a step for now – really anything that does not result in an object being created in the wrong Context
.)
@tniessen @addaleax thanks for taking my proposal into consideration. I've checked out the branch and did some initial testing. The goal I had in mind for this is to have somewhat of an answer to #678. In my JOSE library I encourage developers to work with JWK key abstraction object instances that use Still, depending on the Using the feature from this PR I can add an async interface that would unblock the main thread for operations that I know are going to be more expensive than the cost of a As you can see in my testing KeyObject instance transfer is always faster then key export and subsequent import in a worker thread. The test doesn't run any crypto operation other than importing the key (in case of export-import) and then replying back to the main thread. ops/sec therefore observe the cost of key export(optional), sending a message, key import(optional), response. What you can observe from the results is that keyobject transfer as more or less stable ops/sec where as export-import depends on the type of a key.
|
get symmetricKeySize() { | ||
return this[kHandle].getSymmetricKeySize(); | ||
get type() { | ||
return this[kKeyType]; |
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.
use private filed? I wouldn't like to add an extra symbol which is only used once, in this simple class KeyObject
like:
class KeyObject extends NativeKeyObject {
#type
constructor(type, handle) {
super(checkKeyTypeAndHandle(type, handle) || handle);
this.#type = type;
ObjectDefineProperty(this, kHandle, {
value: handle,
enumerable: false,
configurable: false,
writable: false
});
}
get type() {
return this.#type;
}
}
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.
That's pre-existing code, but I'll consider it. I guess the same is true for a lot of existing class fields in core.
0bd789a
to
53edb69
Compare
8ae28ff
to
2935f72
Compare
@tniessen Let me know if you’d like me to rebase this, the conflicts are kind of my fault after all :) |
53edb69
to
2c30a06
Compare
@addaleax I rebased it, but either (a) there was a bug in this PR before, or (b) I broke something while rebasing this PR, or (c) something changed on the upstream branch that I didn't see. Passing
|
Also, I see that you added a |
@tniessen Before #33772, it didn’t really occur to me that we could use that trick to make JS wrapper objects transferable. It may or may not be easier to keep going that route instead of changing the class structure here, but I think you can judge that better than me :) As for the error … in (As a side note, you generally don’t want to create a new |
2c30a06
to
77d4b6c
Compare
Right, you mentioned that! Thank you :)
@addaleax Could you clarify this? From my perspective, |
77d4b6c
to
946e996
Compare
Right, I was misled by the name to think that it would create one KeyObject object per call, not multiple classes :) Sorry! |
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.
Nice work, LGTM!
+---------------------+ | BaseObject | +---------------------+ | | | +---------------------+ | NativeKeyObject | +---------------------+ | | | +---------------------+ | KeyObject | +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | SecretKeyObject | | AsymmetricKeyObject | +---------------------+ +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | PublicKeyObject | | PrivateKeyObject | +---------------------+ +---------------------+
This separates key handles from the actual key data: +-----------------+ | NativeKeyObject | +-----------------+ ^ extends | +-----------------+ +-----------------+ +---------------+ | KeyObject (JS) | -> | KeyObjectHandle | -> | KeyObjectData | +-----------------+ +-----------------+ +---------------+
PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
+---------------------+ | BaseObject | +---------------------+ | | | +---------------------+ | NativeKeyObject | +---------------------+ | | | +---------------------+ | KeyObject | +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | SecretKeyObject | | AsymmetricKeyObject | +---------------------+ +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | PublicKeyObject | | PrivateKeyObject | +---------------------+ +---------------------+ PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This separates key handles from the actual key data: +-----------------+ | NativeKeyObject | +-----------------+ ^ extends | +-----------------+ +-----------------+ +---------------+ | KeyObject (JS) | -> | KeyObjectHandle | -> | KeyObjectData | +-----------------+ +-----------------+ +---------------+ PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This change allows sharing KeyObjects between threads via postMessage. The receiver acquires a new KeyObject and a new KeyObjectHandle, but refers to the same KeyObjectData: +-------------------+ | NativeKeyObject 1 | ------------------------------------------+ +-------------------+ | ^ | extends | | | +-------------------+ +-------------------+ | | KeyObject 1 (JS) | -> | KeyObjectHandle 1 | --------------+ | +-------------------+ +-------------------+ | | | | | | | | | | | | +-------------------+ | | | NativeKeyObject 2 | ------------------------------------+ | | +-------------------+ | | | ^ | | | extends | | | | | | | +-------------------+ +-------------------+ | | | | KeyObject 2 (JS) | -> | KeyObjectHandle 2 | --------+ | | | +-------------------+ +-------------------+ | | | | | | | | | | | | | | | | | | | | | | | | +-------------------+ | | | | | NativeKeyObject 3 | ------------------------------+ | | | | +-------------------+ | | | | | ^ | | | | | extends | | | | | | v v v v v +-------------------+ +-------------------+ +---------------+ | KeyObject 3 (JS) | -> | KeyObjectHandle 3 | -> | KeyObjectData | +-------------------+ +-------------------+ +---------------+ Co-authored-by: Anna Henningsen <anna@addaleax.net> PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
+---------------------+ | BaseObject | +---------------------+ | | | +---------------------+ | NativeKeyObject | +---------------------+ | | | +---------------------+ | KeyObject | +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | SecretKeyObject | | AsymmetricKeyObject | +---------------------+ +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | PublicKeyObject | | PrivateKeyObject | +---------------------+ +---------------------+ PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This separates key handles from the actual key data: +-----------------+ | NativeKeyObject | +-----------------+ ^ extends | +-----------------+ +-----------------+ +---------------+ | KeyObject (JS) | -> | KeyObjectHandle | -> | KeyObjectData | +-----------------+ +-----------------+ +---------------+ PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This change allows sharing KeyObjects between threads via postMessage. The receiver acquires a new KeyObject and a new KeyObjectHandle, but refers to the same KeyObjectData: +-------------------+ | NativeKeyObject 1 | ------------------------------------------+ +-------------------+ | ^ | extends | | | +-------------------+ +-------------------+ | | KeyObject 1 (JS) | -> | KeyObjectHandle 1 | --------------+ | +-------------------+ +-------------------+ | | | | | | | | | | | | +-------------------+ | | | NativeKeyObject 2 | ------------------------------------+ | | +-------------------+ | | | ^ | | | extends | | | | | | | +-------------------+ +-------------------+ | | | | KeyObject 2 (JS) | -> | KeyObjectHandle 2 | --------+ | | | +-------------------+ +-------------------+ | | | | | | | | | | | | | | | | | | | | | | | | +-------------------+ | | | | | NativeKeyObject 3 | ------------------------------+ | | | | +-------------------+ | | | | | ^ | | | | | extends | | | | | | v v v v v +-------------------+ +-------------------+ +---------------+ | KeyObject 3 (JS) | -> | KeyObjectHandle 3 | -> | KeyObjectData | +-------------------+ +-------------------+ +---------------+ Co-authored-by: Anna Henningsen <anna@addaleax.net> PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Notable changes: async_hooks: * add AsyncResource.bind utility (James M Snell) (#34574) buffer: * also alias BigUInt methods (Anna Henningsen) (#34960) * alias UInt ➡️ Uint in buffer methods (Anna Henningsen) (#34729) build: * add build flag for OSS-Fuzz integration (davkor) (#34761) cli: * add alias for report-directory to make it consistent (Ash Cripps) (#33587) crypto: * allow KeyObjects in postMessage (Tobias Nießen) (#33360) * add randomInt function (Oli Lalonde) (#34600) deps: * upgrade to libuv 1.39.0 (Colin Ihrig) (#34915) dgram: * add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) (#14500) * allow typed arrays in .send() (Sarat Addepalli) (#22413) doc: * add basic embedding example documentation (Anna Henningsen) (#30467) embedding: * make Stop() stop Workers (Anna Henningsen) (#32531) * provide hook for custom process.exit() behaviour (Anna Henningsen) (#32531) fs: * implement lutimes (Maël Nison) (#33399) http: * return this from IncomingMessage#destroy() (Colin Ihrig) (#32789) * expose host and protocol on ClientRequest (wenningplus) [#33803](#33803) http2: * return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) (#33994) * do not modify explicity set date headers (Pranshu Srivastava) (#33160) * n-api**: * support type-tagging objects (Gabriel Schulhof) (#28237) * provide asynchronous cleanup hooks (Anna Henningsen) (#34572) perf_hooks: * add idleTime and event loop util (Trevor Norris) (#34938) timers: * allow timers to be used as primitives (Denys Otrishko) [#34017](#34017) tls: * make 'createSecureContext' honor more options (Mateusz Krawczuk) (#33974) worker: * add public method for marking objects as untransferable (Anna Henningsen) (#33979) * emit `'messagerror'` events for failed deserialization (Anna Henningsen) (#33772) * allow passing JS wrapper objects via postMessage (Anna Henningsen) (#33772) * allow transferring/cloning generic BaseObjects (Anna Henningsen) (#33772) * add option to track unmanaged file descriptors (Anna Henningsen) (#34303) * add stack size resource limit option (Anna Henningsen) (#33085) * make FileHandle transferable (Anna Henningsen) (#33772) zlib: * add `maxOutputLength` option (unknown) (#33516) PR-URL: TODO
Notable changes: async_hooks: * add AsyncResource.bind utility (James M Snell) (#34574) buffer: * also alias BigUInt methods (Anna Henningsen) (#34960) * alias UInt ➡️ Uint in buffer methods (Anna Henningsen) (#34729) build: * add build flag for OSS-Fuzz integration (davkor) (#34761) cli: * add alias for report-directory to make it consistent (Ash Cripps) (#33587) crypto: * allow KeyObjects in postMessage (Tobias Nießen) (#33360) * add randomInt function (Oli Lalonde) (#34600) deps: * upgrade to libuv 1.39.0 (Colin Ihrig) (#34915) dgram: * add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) (#14500) * allow typed arrays in .send() (Sarat Addepalli) (#22413) doc: * add basic embedding example documentation (Anna Henningsen) (#30467) embedding: * make Stop() stop Workers (Anna Henningsen) (#32531) * provide hook for custom process.exit() behaviour (Anna Henningsen) (#32531) fs: * implement lutimes (Maël Nison) (#33399) http: * return this from IncomingMessage#destroy() (Colin Ihrig) (#32789) * expose host and protocol on ClientRequest (wenningplus) [#33803](#33803) http2: * return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) (#33994) * do not modify explicity set date headers (Pranshu Srivastava) (#33160) * n-api**: * support type-tagging objects (Gabriel Schulhof) (#28237) * provide asynchronous cleanup hooks (Anna Henningsen) (#34572) perf_hooks: * add idleTime and event loop util (Trevor Norris) (#34938) timers: * allow timers to be used as primitives (Denys Otrishko) [#34017](#34017) tls: * make 'createSecureContext' honor more options (Mateusz Krawczuk) (#33974) worker: * add public method for marking objects as untransferable (Anna Henningsen) (#33979) * emit `'messagerror'` events for failed deserialization (Anna Henningsen) (#33772) * allow passing JS wrapper objects via postMessage (Anna Henningsen) (#33772) * allow transferring/cloning generic BaseObjects (Anna Henningsen) (#33772) * add option to track unmanaged file descriptors (Anna Henningsen) (#34303) * add stack size resource limit option (Anna Henningsen) (#33085) * make FileHandle transferable (Anna Henningsen) (#33772) zlib: * add `maxOutputLength` option (unknown) (#33516) PR-URL: TODO
Notable changes: assert: * (SEMVER-MINOR) port common.mustCall() to assert (ConorDavenport) #31982 async_hooks: * (SEMVER-MINOR) add AsyncResource.bind utility (James M Snell) #34574 buffer: * (SEMVER-MINOR) also alias BigUInt methods (Anna Henningsen) #34960 * (SEMVER-MINOR) alias UInt ➡️ Uint in buffer methods (Anna Henningsen) #34729 build: * (SEMVER-MINOR) add build flag for OSS-Fuzz integration (davkor) #34761 cli: * (SEMVER-MINOR) add alias for report-directory to make it consistent (Ash Cripps) #33587 crypto: * (SEMVER-MINOR) allow KeyObjects in postMessage (Tobias Nießen) #33360 * (SEMVER-MINOR) add randomInt function (Oli Lalonde) #34600 deps: * upgrade to libuv 1.39.0 (Colin Ihrig) #34915 * upgrade npm to 6.14.7 (claudiahdz) #34468 * upgrade to libuv 1.38.1 (Colin Ihrig) #34187 dgram: * (SEMVER-MINOR) add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) #14500 * (SEMVER-MINOR) allow typed arrays in .send() (Sarat Addepalli) #22413 doc: * (SEMVER-MINOR) Add maxTotalSockets option to agent constructor (rickyes) #33617 * (SEMVER-MINOR) add basic embedding example documentation (Anna Henningsen) #30467 * add Ricky Zhou to collaborators (rickyes) #34676 * add release key for Ruy Adorno (Ruy Adorno) #34628 * add DerekNonGeneric to collaborators (Derek Lewis) #34602 * add AshCripps to collaborators (Ash Cripps) #34494 * add HarshithaKP to collaborators (Harshitha K P) #34417 * add rexagod to collaborators (Pranshu Srivastava) #34457 * add release key for Richard Lau (Richard Lau) #34397 * add danielleadams to collaborators (Danielle Adams) #34360 * add sxa as collaborator (Stewart X Addison) #34338 * add ruyadorno to collaborators (Ruy Adorno) #34297 * (SEMVER-MAJOR) deprecate process.umask() with no arguments (Colin Ihrig) #32499 embedding: * (SEMVER-MINOR) make Stop() stop Workers (Anna Henningsen) #32531 * (SEMVER-MINOR) provide hook for custom process.exit() behaviour (Anna Henningsen) #32531 fs: * (SEMVER-MINOR) implement lutimes (Maël Nison) #33399 http: * (SEMVER-MINOR) add maxTotalSockets to agent class (rickyes) #33617 * (SEMVER-MINOR) return this from IncomingMessage#destroy() (Colin Ihrig) #32789 * (SEMVER-MINOR) expose host and protocol on ClientRequest (wenningplus) #33803 http2: * (SEMVER-MINOR) return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) #33994 * (SEMVER-MINOR) do not modify explicity set date headers (Pranshu Srivastava) #33160 module: * (SEMVER-MINOR) named exports for CJS via static analysis (Guy Bedford) #35249 * (SEMVER-MINOR) exports pattern support (Guy Bedford) #34718 * (SEMVER-MINOR) package "imports" field (Guy Bedford) #34117 * (SEMVER-MINOR) deprecate module.parent (Antoine du HAMEL) #32217 n-api: * (SEMVER-MINOR) create N-API version 7 (Gabriel Schulhof) #35199 * (SEMVER-MINOR) support type-tagging objects (Gabriel Schulhof) #28237 n-api,src: * (SEMVER-MINOR) provide asynchronous cleanup hooks (Anna Henningsen) #34572 perf_hooks: * (SEMVER-MINOR) add idleTime and event loop util (Trevor Norris) #34938 timers: * (SEMVER-MINOR) allow timers to be used as primitives (Denys Otrishko) #34017 tls: * (SEMVER-MINOR) make 'createSecureContext' honor more options (Mateusz Krawczuk) #33974 worker: * (SEMVER-MINOR) add public method for marking objects as untransferable (Anna Henningsen) #33979 * (SEMVER-MINOR) emit `'messagerror'` events for failed deserialization (Anna Henningsen) #33772 * (SEMVER-MINOR) allow passing JS wrapper objects via postMessage (Anna Henningsen) #33772 * (SEMVER-MINOR) allow transferring/cloning generic BaseObjects (Anna Henningsen) #33772 * (SEMVER-MINOR) add option to track unmanaged file descriptors (Anna Henningsen) #34303 * (SEMVER-MINOR) add stack size resource limit option (Anna Henningsen) #33085 worker,fs: * (SEMVER-MINOR) make FileHandle transferable (Anna Henningsen) #33772 zlib: * (SEMVER-MINOR) add `maxOutputLength` option (unknown) #33516 * switch to lazy init for zlib streams (Andrey Pechkurov) #34048 PR-URL: TODO
Notable changes: assert: * (SEMVER-MINOR) port common.mustCall() to assert (ConorDavenport) #31982 async_hooks: * (SEMVER-MINOR) add AsyncResource.bind utility (James M Snell) #34574 buffer: * (SEMVER-MINOR) also alias BigUInt methods (Anna Henningsen) #34960 * (SEMVER-MINOR) alias UInt ➡️ Uint in buffer methods (Anna Henningsen) #34729 build: * (SEMVER-MINOR) add build flag for OSS-Fuzz integration (davkor) #34761 cli: * (SEMVER-MINOR) add alias for report-directory to make it consistent (Ash Cripps) #33587 crypto: * (SEMVER-MINOR) allow KeyObjects in postMessage (Tobias Nießen) #33360 * (SEMVER-MINOR) add randomInt function (Oli Lalonde) #34600 deps: * upgrade to libuv 1.39.0 (Colin Ihrig) #34915 * upgrade npm to 6.14.7 (claudiahdz) #34468 * upgrade to libuv 1.38.1 (Colin Ihrig) #34187 dgram: * (SEMVER-MINOR) add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) #14500 * (SEMVER-MINOR) allow typed arrays in .send() (Sarat Addepalli) #22413 doc: * (SEMVER-MINOR) Add maxTotalSockets option to agent constructor (rickyes) #33617 * (SEMVER-MINOR) add basic embedding example documentation (Anna Henningsen) #30467 * add Ricky Zhou to collaborators (rickyes) #34676 * add release key for Ruy Adorno (Ruy Adorno) #34628 * add DerekNonGeneric to collaborators (Derek Lewis) #34602 * add AshCripps to collaborators (Ash Cripps) #34494 * add HarshithaKP to collaborators (Harshitha K P) #34417 * add rexagod to collaborators (Pranshu Srivastava) #34457 * add release key for Richard Lau (Richard Lau) #34397 * add danielleadams to collaborators (Danielle Adams) #34360 * add sxa as collaborator (Stewart X Addison) #34338 * add ruyadorno to collaborators (Ruy Adorno) #34297 * (SEMVER-MAJOR) deprecate process.umask() with no arguments (Colin Ihrig) #32499 embedding: * (SEMVER-MINOR) make Stop() stop Workers (Anna Henningsen) #32531 * (SEMVER-MINOR) provide hook for custom process.exit() behaviour (Anna Henningsen) #32531 fs: * (SEMVER-MINOR) implement lutimes (Maël Nison) #33399 http: * (SEMVER-MINOR) add maxTotalSockets to agent class (rickyes) #33617 * (SEMVER-MINOR) return this from IncomingMessage#destroy() (Colin Ihrig) #32789 * (SEMVER-MINOR) expose host and protocol on ClientRequest (wenningplus) #33803 http2: * (SEMVER-MINOR) return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) #33994 * (SEMVER-MINOR) do not modify explicity set date headers (Pranshu Srivastava) #33160 module: * (SEMVER-MINOR) named exports for CJS via static analysis (Guy Bedford) #35249 * (SEMVER-MINOR) exports pattern support (Guy Bedford) #34718 * (SEMVER-MINOR) package "imports" field (Guy Bedford) #34117 * (SEMVER-MINOR) deprecate module.parent (Antoine du HAMEL) #32217 n-api: * (SEMVER-MINOR) create N-API version 7 (Gabriel Schulhof) #35199 * (SEMVER-MINOR) support type-tagging objects (Gabriel Schulhof) #28237 n-api,src: * (SEMVER-MINOR) provide asynchronous cleanup hooks (Anna Henningsen) #34572 perf_hooks: * (SEMVER-MINOR) add idleTime and event loop util (Trevor Norris) #34938 timers: * (SEMVER-MINOR) allow timers to be used as primitives (Denys Otrishko) #34017 tls: * (SEMVER-MINOR) make 'createSecureContext' honor more options (Mateusz Krawczuk) #33974 worker: * (SEMVER-MINOR) add public method for marking objects as untransferable (Anna Henningsen) #33979 * (SEMVER-MINOR) emit `'messagerror'` events for failed deserialization (Anna Henningsen) #33772 * (SEMVER-MINOR) allow passing JS wrapper objects via postMessage (Anna Henningsen) #33772 * (SEMVER-MINOR) allow transferring/cloning generic BaseObjects (Anna Henningsen) #33772 * (SEMVER-MINOR) add option to track unmanaged file descriptors (Anna Henningsen) #34303 * (SEMVER-MINOR) add stack size resource limit option (Anna Henningsen) #33085 worker,fs: * (SEMVER-MINOR) make FileHandle transferable (Anna Henningsen) #33772 zlib: * (SEMVER-MINOR) add `maxOutputLength` option (unknown) #33516 * switch to lazy init for zlib streams (Andrey Pechkurov) #34048 PR-URL: TODO
Notable changes: assert: * (SEMVER-MINOR) port common.mustCall() to assert (ConorDavenport) #31982 async_hooks: * (SEMVER-MINOR) add AsyncResource.bind utility (James M Snell) #34574 buffer: * (SEMVER-MINOR) also alias BigUInt methods (Anna Henningsen) #34960 * (SEMVER-MINOR) alias UInt ➡️ Uint in buffer methods (Anna Henningsen) #34729 build: * (SEMVER-MINOR) add build flag for OSS-Fuzz integration (davkor) #34761 cli: * (SEMVER-MINOR) add alias for report-directory to make it consistent (Ash Cripps) #33587 crypto: * (SEMVER-MINOR) allow KeyObjects in postMessage (Tobias Nießen) #33360 * (SEMVER-MINOR) add randomInt function (Oli Lalonde) #34600 deps: * upgrade to libuv 1.39.0 (Colin Ihrig) #34915 * upgrade npm to 6.14.7 (claudiahdz) #34468 * upgrade to libuv 1.38.1 (Colin Ihrig) #34187 dgram: * (SEMVER-MINOR) add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) #14500 * (SEMVER-MINOR) allow typed arrays in .send() (Sarat Addepalli) #22413 doc: * (SEMVER-MINOR) Add maxTotalSockets option to agent constructor (rickyes) #33617 * (SEMVER-MINOR) add basic embedding example documentation (Anna Henningsen) #30467 * add Ricky Zhou to collaborators (rickyes) #34676 * add release key for Ruy Adorno (Ruy Adorno) #34628 * add DerekNonGeneric to collaborators (Derek Lewis) #34602 * add AshCripps to collaborators (Ash Cripps) #34494 * add HarshithaKP to collaborators (Harshitha K P) #34417 * add rexagod to collaborators (Pranshu Srivastava) #34457 * add release key for Richard Lau (Richard Lau) #34397 * add danielleadams to collaborators (Danielle Adams) #34360 * add sxa as collaborator (Stewart X Addison) #34338 * add ruyadorno to collaborators (Ruy Adorno) #34297 * (SEMVER-MAJOR) deprecate process.umask() with no arguments (Colin Ihrig) #32499 embedding: * (SEMVER-MINOR) make Stop() stop Workers (Anna Henningsen) #32531 * (SEMVER-MINOR) provide hook for custom process.exit() behaviour (Anna Henningsen) #32531 fs: * (SEMVER-MINOR) implement lutimes (Maël Nison) #33399 http: * (SEMVER-MINOR) add maxTotalSockets to agent class (rickyes) #33617 * (SEMVER-MINOR) return this from IncomingMessage#destroy() (Colin Ihrig) #32789 * (SEMVER-MINOR) expose host and protocol on ClientRequest (wenningplus) #33803 http2: * (SEMVER-MINOR) return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) #33994 * (SEMVER-MINOR) do not modify explicity set date headers (Pranshu Srivastava) #33160 module: * (SEMVER-MINOR) named exports for CJS via static analysis (Guy Bedford) #35249 * (SEMVER-MINOR) exports pattern support (Guy Bedford) #34718 * (SEMVER-MINOR) package "imports" field (Guy Bedford) #34117 * (SEMVER-MINOR) deprecate module.parent (Antoine du HAMEL) #32217 n-api: * (SEMVER-MINOR) create N-API version 7 (Gabriel Schulhof) #35199 * (SEMVER-MINOR) support type-tagging objects (Gabriel Schulhof) #28237 n-api,src: * (SEMVER-MINOR) provide asynchronous cleanup hooks (Anna Henningsen) #34572 perf_hooks: * (SEMVER-MINOR) add idleTime and event loop util (Trevor Norris) #34938 timers: * (SEMVER-MINOR) allow timers to be used as primitives (Denys Otrishko) #34017 tls: * (SEMVER-MINOR) make 'createSecureContext' honor more options (Mateusz Krawczuk) #33974 worker: * (SEMVER-MINOR) add public method for marking objects as untransferable (Anna Henningsen) #33979 * (SEMVER-MINOR) emit `'messagerror'` events for failed deserialization (Anna Henningsen) #33772 * (SEMVER-MINOR) allow passing JS wrapper objects via postMessage (Anna Henningsen) #33772 * (SEMVER-MINOR) allow transferring/cloning generic BaseObjects (Anna Henningsen) #33772 * (SEMVER-MINOR) add option to track unmanaged file descriptors (Anna Henningsen) #34303 * (SEMVER-MINOR) add stack size resource limit option (Anna Henningsen) #33085 worker,fs: * (SEMVER-MINOR) make FileHandle transferable (Anna Henningsen) #33772 zlib: * (SEMVER-MINOR) add `maxOutputLength` option (unknown) #33516 * switch to lazy init for zlib streams (Andrey Pechkurov) #34048 PR-URL: TODO
Notable changes: assert: * (SEMVER-MINOR) port common.mustCall() to assert (ConorDavenport) #31982 async_hooks: * (SEMVER-MINOR) add AsyncResource.bind utility (James M Snell) #34574 buffer: * (SEMVER-MINOR) also alias BigUInt methods (Anna Henningsen) #34960 * (SEMVER-MINOR) alias UInt ➡️ Uint in buffer methods (Anna Henningsen) #34729 build: * (SEMVER-MINOR) add build flag for OSS-Fuzz integration (davkor) #34761 cli: * (SEMVER-MINOR) add alias for report-directory to make it consistent (Ash Cripps) #33587 crypto: * (SEMVER-MINOR) allow KeyObjects in postMessage (Tobias Nießen) #33360 * (SEMVER-MINOR) add randomInt function (Oli Lalonde) #34600 deps: * upgrade to libuv 1.39.0 (Colin Ihrig) #34915 * upgrade npm to 6.14.7 (claudiahdz) #34468 * upgrade to libuv 1.38.1 (Colin Ihrig) #34187 dgram: * (SEMVER-MINOR) add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) #14500 * (SEMVER-MINOR) allow typed arrays in .send() (Sarat Addepalli) #22413 doc: * (SEMVER-MINOR) Add maxTotalSockets option to agent constructor (rickyes) #33617 * (SEMVER-MINOR) add basic embedding example documentation (Anna Henningsen) #30467 * add Ricky Zhou to collaborators (rickyes) #34676 * add release key for Ruy Adorno (Ruy Adorno) #34628 * add DerekNonGeneric to collaborators (Derek Lewis) #34602 * add AshCripps to collaborators (Ash Cripps) #34494 * add HarshithaKP to collaborators (Harshitha K P) #34417 * add rexagod to collaborators (Pranshu Srivastava) #34457 * add release key for Richard Lau (Richard Lau) #34397 * add danielleadams to collaborators (Danielle Adams) #34360 * add sxa as collaborator (Stewart X Addison) #34338 * add ruyadorno to collaborators (Ruy Adorno) #34297 * (SEMVER-MAJOR) deprecate process.umask() with no arguments (Colin Ihrig) #32499 embedding: * (SEMVER-MINOR) make Stop() stop Workers (Anna Henningsen) #32531 * (SEMVER-MINOR) provide hook for custom process.exit() behaviour (Anna Henningsen) #32531 fs: * (SEMVER-MINOR) implement lutimes (Maël Nison) #33399 http: * (SEMVER-MINOR) add maxTotalSockets to agent class (rickyes) #33617 * (SEMVER-MINOR) return this from IncomingMessage#destroy() (Colin Ihrig) #32789 * (SEMVER-MINOR) expose host and protocol on ClientRequest (wenningplus) #33803 http2: * (SEMVER-MINOR) return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) #33994 * (SEMVER-MINOR) do not modify explicity set date headers (Pranshu Srivastava) #33160 module: * (SEMVER-MINOR) named exports for CJS via static analysis (Guy Bedford) #35249 * (SEMVER-MINOR) exports pattern support (Guy Bedford) #34718 * (SEMVER-MINOR) package "imports" field (Guy Bedford) #34117 * (SEMVER-MINOR) deprecate module.parent (Antoine du HAMEL) #32217 n-api: * (SEMVER-MINOR) create N-API version 7 (Gabriel Schulhof) #35199 * (SEMVER-MINOR) support type-tagging objects (Gabriel Schulhof) #28237 n-api,src: * (SEMVER-MINOR) provide asynchronous cleanup hooks (Anna Henningsen) #34572 perf_hooks: * (SEMVER-MINOR) add idleTime and event loop util (Trevor Norris) #34938 timers: * (SEMVER-MINOR) allow timers to be used as primitives (Denys Otrishko) #34017 tls: * (SEMVER-MINOR) make 'createSecureContext' honor more options (Mateusz Krawczuk) #33974 worker: * (SEMVER-MINOR) add public method for marking objects as untransferable (Anna Henningsen) #33979 * (SEMVER-MINOR) emit `'messagerror'` events for failed deserialization (Anna Henningsen) #33772 * (SEMVER-MINOR) allow passing JS wrapper objects via postMessage (Anna Henningsen) #33772 * (SEMVER-MINOR) allow transferring/cloning generic BaseObjects (Anna Henningsen) #33772 * (SEMVER-MINOR) add option to track unmanaged file descriptors (Anna Henningsen) #34303 * (SEMVER-MINOR) add stack size resource limit option (Anna Henningsen) #33085 worker,fs: * (SEMVER-MINOR) make FileHandle transferable (Anna Henningsen) #33772 zlib: * (SEMVER-MINOR) add `maxOutputLength` option (unknown) #33516 * switch to lazy init for zlib streams (Andrey Pechkurov) #34048 PR-URL: #35401
Notable changes: assert: * (SEMVER-MINOR) port common.mustCall() to assert (ConorDavenport) #31982 async_hooks: * (SEMVER-MINOR) add AsyncResource.bind utility (James M Snell) #34574 buffer: * (SEMVER-MINOR) also alias BigUInt methods (Anna Henningsen) #34960 * (SEMVER-MINOR) alias UInt ➡️ Uint in buffer methods (Anna Henningsen) #34729 build: * (SEMVER-MINOR) add build flag for OSS-Fuzz integration (davkor) #34761 cli: * (SEMVER-MINOR) add alias for report-directory to make it consistent (Ash Cripps) #33587 crypto: * (SEMVER-MINOR) allow KeyObjects in postMessage (Tobias Nießen) #33360 * (SEMVER-MINOR) add randomInt function (Oli Lalonde) #34600 deps: * upgrade to libuv 1.39.0 (Colin Ihrig) #34915 * upgrade npm to 6.14.7 (claudiahdz) #34468 * upgrade to libuv 1.38.1 (Colin Ihrig) #34187 dgram: * (SEMVER-MINOR) add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) #14500 * (SEMVER-MINOR) allow typed arrays in .send() (Sarat Addepalli) #22413 doc: * (SEMVER-MINOR) Add maxTotalSockets option to agent constructor (rickyes) #33617 * (SEMVER-MINOR) add basic embedding example documentation (Anna Henningsen) #30467 * add Ricky Zhou to collaborators (rickyes) #34676 * add release key for Ruy Adorno (Ruy Adorno) #34628 * add DerekNonGeneric to collaborators (Derek Lewis) #34602 * add AshCripps to collaborators (Ash Cripps) #34494 * add HarshithaKP to collaborators (Harshitha K P) #34417 * add rexagod to collaborators (Pranshu Srivastava) #34457 * add release key for Richard Lau (Richard Lau) #34397 * add danielleadams to collaborators (Danielle Adams) #34360 * add sxa as collaborator (Stewart X Addison) #34338 * add ruyadorno to collaborators (Ruy Adorno) #34297 * (SEMVER-MAJOR) deprecate process.umask() with no arguments (Colin Ihrig) #32499 embedding: * (SEMVER-MINOR) make Stop() stop Workers (Anna Henningsen) #32531 * (SEMVER-MINOR) provide hook for custom process.exit() behaviour (Anna Henningsen) #32531 fs: * (SEMVER-MINOR) implement lutimes (Maël Nison) #33399 http: * (SEMVER-MINOR) add maxTotalSockets to agent class (rickyes) #33617 * (SEMVER-MINOR) return this from IncomingMessage#destroy() (Colin Ihrig) #32789 * (SEMVER-MINOR) expose host and protocol on ClientRequest (wenningplus) #33803 http2: * (SEMVER-MINOR) return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) #33994 * (SEMVER-MINOR) do not modify explicity set date headers (Pranshu Srivastava) #33160 module: * (SEMVER-MINOR) named exports for CJS via static analysis (Guy Bedford) #35249 * (SEMVER-MINOR) exports pattern support (Guy Bedford) #34718 * (SEMVER-MINOR) package "imports" field (Guy Bedford) #34117 * (SEMVER-MINOR) deprecate module.parent (Antoine du HAMEL) #32217 n-api: * (SEMVER-MINOR) create N-API version 7 (Gabriel Schulhof) #35199 * (SEMVER-MINOR) support type-tagging objects (Gabriel Schulhof) #28237 n-api,src: * (SEMVER-MINOR) provide asynchronous cleanup hooks (Anna Henningsen) #34572 perf_hooks: * (SEMVER-MINOR) add idleTime and event loop util (Trevor Norris) #34938 timers: * (SEMVER-MINOR) allow timers to be used as primitives (Denys Otrishko) #34017 tls: * (SEMVER-MINOR) make 'createSecureContext' honor more options (Mateusz Krawczuk) #33974 worker: * (SEMVER-MINOR) add public method for marking objects as untransferable (Anna Henningsen) #33979 * (SEMVER-MINOR) emit `'messagerror'` events for failed deserialization (Anna Henningsen) #33772 * (SEMVER-MINOR) allow passing JS wrapper objects via postMessage (Anna Henningsen) #33772 * (SEMVER-MINOR) allow transferring/cloning generic BaseObjects (Anna Henningsen) #33772 * (SEMVER-MINOR) add option to track unmanaged file descriptors (Anna Henningsen) #34303 * (SEMVER-MINOR) add stack size resource limit option (Anna Henningsen) #33085 worker,fs: * (SEMVER-MINOR) make FileHandle transferable (Anna Henningsen) #33772 zlib: * (SEMVER-MINOR) add `maxOutputLength` option (unknown) #33516 * switch to lazy init for zlib streams (Andrey Pechkurov) #34048 PR-URL: #35401
👋 Hi, I don't have a simple reproduction yet, but I think this change has resulted in a memory leak where every instance inherited from NativeKeyObject is retained forever. I noticed this in a production application which exhibits a clear memory leak on node 12.19.0 but not on node 12.18.4. I've taken heap dumps of my application surrounding a few minutes of generated requests, and I can see that I'm not exactly an expert at using the snapshot analyzer, but I think Global handles is significant: I can also locate these object in the For context, my application is experiencing this when using the |
@panva, thanks for the issue reference. The fix you mentioned was released in v14.13.1, and I confirmed the leak does not reproduce on v14.15.0. |
Notable changes: assert: * (SEMVER-MINOR) port common.mustCall() to assert (ConorDavenport) nodejs#31982 async_hooks: * (SEMVER-MINOR) add AsyncResource.bind utility (James M Snell) nodejs#34574 buffer: * (SEMVER-MINOR) also alias BigUInt methods (Anna Henningsen) nodejs#34960 * (SEMVER-MINOR) alias UInt ➡️ Uint in buffer methods (Anna Henningsen) nodejs#34729 build: * (SEMVER-MINOR) add build flag for OSS-Fuzz integration (davkor) nodejs#34761 cli: * (SEMVER-MINOR) add alias for report-directory to make it consistent (Ash Cripps) nodejs#33587 crypto: * (SEMVER-MINOR) allow KeyObjects in postMessage (Tobias Nießen) nodejs#33360 * (SEMVER-MINOR) add randomInt function (Oli Lalonde) nodejs#34600 deps: * upgrade to libuv 1.39.0 (Colin Ihrig) nodejs#34915 * upgrade npm to 6.14.7 (claudiahdz) nodejs#34468 * upgrade to libuv 1.38.1 (Colin Ihrig) nodejs#34187 dgram: * (SEMVER-MINOR) add IPv6 scope id suffix to received udp6 dgrams (Pekka Nikander) nodejs#14500 * (SEMVER-MINOR) allow typed arrays in .send() (Sarat Addepalli) nodejs#22413 doc: * (SEMVER-MINOR) Add maxTotalSockets option to agent constructor (rickyes) nodejs#33617 * (SEMVER-MINOR) add basic embedding example documentation (Anna Henningsen) nodejs#30467 * add Ricky Zhou to collaborators (rickyes) nodejs#34676 * add release key for Ruy Adorno (Ruy Adorno) nodejs#34628 * add DerekNonGeneric to collaborators (Derek Lewis) nodejs#34602 * add AshCripps to collaborators (Ash Cripps) nodejs#34494 * add HarshithaKP to collaborators (Harshitha K P) nodejs#34417 * add rexagod to collaborators (Pranshu Srivastava) nodejs#34457 * add release key for Richard Lau (Richard Lau) nodejs#34397 * add danielleadams to collaborators (Danielle Adams) nodejs#34360 * add sxa as collaborator (Stewart X Addison) nodejs#34338 * add ruyadorno to collaborators (Ruy Adorno) nodejs#34297 * (SEMVER-MAJOR) deprecate process.umask() with no arguments (Colin Ihrig) nodejs#32499 embedding: * (SEMVER-MINOR) make Stop() stop Workers (Anna Henningsen) nodejs#32531 * (SEMVER-MINOR) provide hook for custom process.exit() behaviour (Anna Henningsen) nodejs#32531 fs: * (SEMVER-MINOR) implement lutimes (Maël Nison) nodejs#33399 http: * (SEMVER-MINOR) add maxTotalSockets to agent class (rickyes) nodejs#33617 * (SEMVER-MINOR) return this from IncomingMessage#destroy() (Colin Ihrig) nodejs#32789 * (SEMVER-MINOR) expose host and protocol on ClientRequest (wenningplus) nodejs#33803 http2: * (SEMVER-MINOR) return this for Http2ServerRequest#setTimeout (Pranshu Srivastava) nodejs#33994 * (SEMVER-MINOR) do not modify explicity set date headers (Pranshu Srivastava) nodejs#33160 module: * (SEMVER-MINOR) named exports for CJS via static analysis (Guy Bedford) nodejs#35249 * (SEMVER-MINOR) exports pattern support (Guy Bedford) nodejs#34718 * (SEMVER-MINOR) package "imports" field (Guy Bedford) nodejs#34117 * (SEMVER-MINOR) deprecate module.parent (Antoine du HAMEL) nodejs#32217 n-api: * (SEMVER-MINOR) create N-API version 7 (Gabriel Schulhof) nodejs#35199 * (SEMVER-MINOR) support type-tagging objects (Gabriel Schulhof) nodejs#28237 n-api,src: * (SEMVER-MINOR) provide asynchronous cleanup hooks (Anna Henningsen) nodejs#34572 perf_hooks: * (SEMVER-MINOR) add idleTime and event loop util (Trevor Norris) nodejs#34938 timers: * (SEMVER-MINOR) allow timers to be used as primitives (Denys Otrishko) nodejs#34017 tls: * (SEMVER-MINOR) make 'createSecureContext' honor more options (Mateusz Krawczuk) nodejs#33974 worker: * (SEMVER-MINOR) add public method for marking objects as untransferable (Anna Henningsen) nodejs#33979 * (SEMVER-MINOR) emit `'messagerror'` events for failed deserialization (Anna Henningsen) nodejs#33772 * (SEMVER-MINOR) allow passing JS wrapper objects via postMessage (Anna Henningsen) nodejs#33772 * (SEMVER-MINOR) allow transferring/cloning generic BaseObjects (Anna Henningsen) nodejs#33772 * (SEMVER-MINOR) add option to track unmanaged file descriptors (Anna Henningsen) nodejs#34303 * (SEMVER-MINOR) add stack size resource limit option (Anna Henningsen) nodejs#33085 worker,fs: * (SEMVER-MINOR) make FileHandle transferable (Anna Henningsen) nodejs#33772 zlib: * (SEMVER-MINOR) add `maxOutputLength` option (unknown) nodejs#33516 * switch to lazy init for zlib streams (Andrey Pechkurov) nodejs#34048 PR-URL: nodejs#35401
This patch allows to pass
KeyObject
instances between threads/contexts, and is based on some great work by @addaleax.While the public API has not changed, the internal structure has become a little complicated. In order to be cloneable, the
KeyObject
class must be implemented in C++. However,KeyObject
relies on its JavaScript class hierarchy. To get around this problem, a new base classNativeKeyObject
is added, from which allKeyObject
classes inherit:NativeKeyObject
has no properties and doesn't change the behavior ofKeyObject
. It only exists to be cloneable.Each
KeyObject
k
has an associatedKeyObjectHandle
, which is stored ink[kHandle]
. The publicKeyObject
API uses the internalKeyObjectHandle
API to perform most operations, however, aKeyObjectHandle
is aBaseObject
and cannot be shared between threads directly. Instead, eachKeyObjectHandle
references aKeyObjectData
object, which contains the actual key. This object can be shared between threads:When a key is passed via a
MessagePort
, the receiver creates a newKeyObjectHandle
and the correctKeyObject
(one ofSecretKeyObject
,PublicKeyObject
,PrivateKeyObject
). However, it does not create a newKeyObjectData
object: All threads always refer to the sameKeyObjectData
for the same key:Since almost all operations go through the
KeyObjectHandle
, it makes sense for theKeyObjectHandle
to contain a pointer to theKeyObjectData
. However, theNativeKeyObject
class implementsCloneForMessaging
and therefore also needs access to the sameKeyObjectData
. In theory, we could go throughthis[kHandle]->Data()
, but it is easier to also maintain a pointer from theNativeKeyObject
to the sameKeyObjectData
.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passescc @addaleax @panva @nodejs/crypto @nodejs/security-wg