worker: no throw on property access/postMessage after termination
PR-URL: https://github.com/nodejs/node/pull/25871 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
e0a3d74135
commit
2fc759b9b8
@ -184,6 +184,8 @@ class Worker extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
postMessage(...args) {
|
postMessage(...args) {
|
||||||
|
if (this[kPublicPort] === null) return;
|
||||||
|
|
||||||
this[kPublicPort].postMessage(...args);
|
this[kPublicPort].postMessage(...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,14 +221,20 @@ class Worker extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get stdin() {
|
get stdin() {
|
||||||
|
if (this[kParentSideStdio] === null) return null;
|
||||||
|
|
||||||
return this[kParentSideStdio].stdin;
|
return this[kParentSideStdio].stdin;
|
||||||
}
|
}
|
||||||
|
|
||||||
get stdout() {
|
get stdout() {
|
||||||
|
if (this[kParentSideStdio] === null) return null;
|
||||||
|
|
||||||
return this[kParentSideStdio].stdout;
|
return this[kParentSideStdio].stdout;
|
||||||
}
|
}
|
||||||
|
|
||||||
get stderr() {
|
get stderr() {
|
||||||
|
if (this[kParentSideStdio] === null) return null;
|
||||||
|
|
||||||
return this[kParentSideStdio].stderr;
|
return this[kParentSideStdio].stderr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
38
test/parallel/test-worker-safe-getters.js
Normal file
38
test/parallel/test-worker-safe-getters.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common');
|
||||||
|
|
||||||
|
const assert = require('assert');
|
||||||
|
const { Worker, isMainThread } = require('worker_threads');
|
||||||
|
|
||||||
|
if (isMainThread) {
|
||||||
|
const w = new Worker(__filename, {
|
||||||
|
stdin: true,
|
||||||
|
stdout: true,
|
||||||
|
stderr: true
|
||||||
|
});
|
||||||
|
|
||||||
|
w.on('exit', common.mustCall((code) => {
|
||||||
|
assert.strictEqual(code, 0);
|
||||||
|
|
||||||
|
// `postMessage` should not throw after termination
|
||||||
|
// (this mimics the browser behavior).
|
||||||
|
w.postMessage('foobar');
|
||||||
|
w.ref();
|
||||||
|
w.unref();
|
||||||
|
|
||||||
|
// Although not browser specific, probably wise to
|
||||||
|
// make sure the stream getters don't throw either.
|
||||||
|
w.stdin;
|
||||||
|
w.stdout;
|
||||||
|
w.stderr;
|
||||||
|
|
||||||
|
// Sanity check.
|
||||||
|
assert.strictEqual(w.threadId, -1);
|
||||||
|
assert.strictEqual(w.stdin, null);
|
||||||
|
assert.strictEqual(w.stdout, null);
|
||||||
|
assert.strictEqual(w.stderr, null);
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
process.exit(0);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user