-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
src: do proper error checking in AsyncWrap::MakeCallback
#21189
Changes from 1 commit
bbc5258
4bb9aac
032b390
c395973
fa46748
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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: |-
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
#include "async_wrap.h" | ||
#include "base_object-inl.h" | ||
#include "node_internals.h" | ||
#include "node_errors.h" | ||
|
||
namespace node { | ||
|
||
|
@@ -81,18 +82,17 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback( | |
const v8::Local<v8::Name> symbol, | ||
int argc, | ||
v8::Local<v8::Value>* argv) { | ||
v8::Local<v8::Value> cb_v = object()->Get(symbol); | ||
CHECK(cb_v->IsFunction()); | ||
return MakeCallback(cb_v.As<v8::Function>(), argc, argv); | ||
} | ||
|
||
|
||
inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback( | ||
uint32_t index, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good find! |
||
int argc, | ||
v8::Local<v8::Value>* argv) { | ||
v8::Local<v8::Value> cb_v = object()->Get(index); | ||
CHECK(cb_v->IsFunction()); | ||
v8::Local<v8::Value> cb_v; | ||
if (!object()->Get(env()->context(), symbol).ToLocal(&cb_v)) | ||
return v8::MaybeLocal<v8::Value>(); | ||
if (!cb_v->IsFunction()) { | ||
// Due to V8’s error handling mechanisms, this will not show up as an error | ||
// in the common case, which is that this is outside of any JS frame. | ||
// So, the exception here is mostly just there to fulfill the | ||
// `MaybeLocal<>` API contract. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, this is something I’d like to fix by adding a verbose TryCatch around the event loop (if I can make it work – not sure about that). But that would be a semver-major change. |
||
THROW_ERR_MISSING_METHOD(env()->isolate(), symbol); | ||
return v8::MaybeLocal<v8::Value>(); | ||
} | ||
return MakeCallback(cb_v.As<v8::Function>(), argc, argv); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Flags: --experimental-worker | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { MessageChannel, MessagePort } = require('worker_threads'); | ||
|
||
{ | ||
const { port1, port2 } = new MessageChannel(); | ||
|
||
// Throwing in the getter should not crash. | ||
Object.defineProperty(port1, 'onmessage', { | ||
get() { | ||
throw new Error('eyecatcher'); | ||
} | ||
}); | ||
|
||
port2.postMessage({ foo: 'bar' }); | ||
} | ||
|
||
{ | ||
const { port1, port2 } = new MessageChannel(); | ||
|
||
// Returning a non-function in the getter should not crash. | ||
Object.defineProperty(port1, 'onmessage', { | ||
get() { | ||
return 42; | ||
} | ||
}); | ||
|
||
port2.postMessage({ foo: 'bar' }); | ||
} |
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.
Maybe remove this until we fix the TODO?
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.
@joyeecheung Thanks, done!