node/test/parallel/test-worker-message-port-jstransferable-nested-untransferable.js
Anna Henningsen 81df668480 worker: do not crash when JSTransferable lists untransferable value
This can currently be triggered when posting a closing FileHandle.

Refs: https://github.com/nodejs/node/pull/34746#issuecomment-673675333

PR-URL: https://github.com/nodejs/node/pull/34766
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-08-17 11:42:50 -07:00

39 lines
1.0 KiB
JavaScript

// Flags: --expose-internals --no-warnings
'use strict';
const common = require('../common');
const assert = require('assert');
const {
JSTransferable, kTransfer, kTransferList
} = require('internal/worker/js_transferable');
const { MessageChannel } = require('worker_threads');
// Transferring a JSTransferable that refers to another, untransferable, value
// in its transfer list should not crash hard.
class OuterTransferable extends JSTransferable {
constructor() {
super();
// Create a detached MessagePort at this.inner
const c = new MessageChannel();
this.inner = c.port1;
c.port2.postMessage(this.inner, [ this.inner ]);
}
[kTransferList] = common.mustCall(() => {
return [ this.inner ];
});
[kTransfer] = common.mustCall(() => {
return {
data: { inner: this.inner },
deserializeInfo: 'does-not:matter'
};
});
}
const { port1 } = new MessageChannel();
const ot = new OuterTransferable();
assert.throws(() => {
port1.postMessage(ot, [ot]);
}, { name: 'DataCloneError' });