-
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.
src: do proper error checking in
AsyncWrap::MakeCallback
At least one method on a native object is added as a getter, namely `MessagePort.prototype.onmessage`. When a MessagePort attempts to call this method from C++ in response to receiving data, it will first invoke that getter and then call the function. Since `worker.terminate()` interrupts execution, this means that the getter may fail (without being faulty code on its own). This means that at least one test exercising these methods in combination has been flaky and could have crashed, because we did not actually check that the getter returns a value so far, resulting in dereferencing an empty `Local`. The proper fix for this is to use the non-deprecated overload of `Get()` and check the result like we should be doing. Also, as a (related) fix, don’t crash if the method is not a function but rather something else, like a getter could provide. Example test failure: https://ci.nodejs.org/job/node-test-commit-linux-containered/4976/nodes=ubuntu1604_sharedlibs_zlib_x64/console 17:56:56 not ok 1955 parallel/test-worker-dns-terminate 17:56:56 --- 17:56:56 duration_ms: 1.237 17:56:56 severity: crashed 17:56:56 exitcode: -11 17:56:56 stack: |- PR-URL: #21189 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
6 changed files
with
75 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Flags: --experimental-worker | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { MessageChannel } = require('worker_threads'); | ||
|
||
{ | ||
const { port1, port2 } = new MessageChannel(); | ||
|
||
// Returning a non-function in the getter should not crash. | ||
Object.defineProperty(port1, 'onmessage', { | ||
get() { | ||
port1.unref(); | ||
return 42; | ||
} | ||
}); | ||
|
||
port2.postMessage({ foo: 'bar' }); | ||
|
||
// We need to start the port manually because .onmessage assignment tracking | ||
// has been overridden. | ||
port1.start(); | ||
port1.ref(); | ||
} | ||
|
||
{ | ||
const err = new Error('eyecatcher'); | ||
process.on('uncaughtException', common.mustCall((exception) => { | ||
port1.unref(); | ||
assert.strictEqual(exception, err); | ||
})); | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
|
||
// Throwing in the getter should not crash. | ||
Object.defineProperty(port1, 'onmessage', { | ||
get() { | ||
throw err; | ||
} | ||
}); | ||
|
||
port2.postMessage({ foo: 'bar' }); | ||
|
||
// We need to start the port manually because .onmessage assignment tracking | ||
// has been overridden. | ||
port1.start(); | ||
port1.ref(); | ||
} |