-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: add public method for marking objects as untransferable
We currently mark a number of `ArrayBuffer`s as not transferable, including the `Buffer` pool and ones with finalizers provided by C++ addons. There is no good reason to assume that userland code might not encounter similar problems, for example when doing `ArrayBuffer` pooling similar to ours. Therefore, provide an API that lets userland code also mark objects as not transferable. PR-URL: #33979 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
- Loading branch information
1 parent
74f4aae
commit 4e42eb5
Showing
9 changed files
with
118 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
37 changes: 37 additions & 0 deletions
37
test/parallel/test-worker-message-transfer-port-mark-as-untransferable.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { MessageChannel, markAsUntransferable } = require('worker_threads'); | ||
|
||
{ | ||
const ab = new ArrayBuffer(8); | ||
|
||
markAsUntransferable(ab); | ||
assert.strictEqual(ab.byteLength, 8); | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
port1.postMessage(ab, [ ab ]); | ||
|
||
assert.strictEqual(ab.byteLength, 8); // The AB is not detached. | ||
port2.once('message', common.mustCall()); | ||
} | ||
|
||
{ | ||
const channel1 = new MessageChannel(); | ||
const channel2 = new MessageChannel(); | ||
|
||
markAsUntransferable(channel2.port1); | ||
|
||
assert.throws(() => { | ||
channel1.port1.postMessage(channel2.port1, [ channel2.port1 ]); | ||
}, /was found in message but not listed in transferList/); | ||
|
||
channel2.port1.postMessage('still works, not closed/transferred'); | ||
channel2.port2.once('message', common.mustCall()); | ||
} | ||
|
||
{ | ||
for (const value of [0, null, false, true, undefined, [], {}]) { | ||
markAsUntransferable(value); // Has no visible effect. | ||
} | ||
} |