-
Notifications
You must be signed in to change notification settings - Fork 30k
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: use env’s instead of isolate’s RequestInterrupt() + v8::Platform #32523
Changes from all commits
171409a
4de323c
23b2da6
62889df
d9689db
aeb354d
7e75cc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,11 +42,13 @@ using v8::NewStringType; | |
using v8::Number; | ||
using v8::Object; | ||
using v8::Private; | ||
using v8::Script; | ||
using v8::SnapshotCreator; | ||
using v8::StackTrace; | ||
using v8::String; | ||
using v8::Symbol; | ||
using v8::TracingController; | ||
using v8::TryCatch; | ||
using v8::Undefined; | ||
using v8::Value; | ||
using worker::Worker; | ||
|
@@ -394,7 +396,30 @@ Environment::Environment(IsolateData* isolate_data, | |
} | ||
|
||
Environment::~Environment() { | ||
if (interrupt_data_ != nullptr) *interrupt_data_ = nullptr; | ||
if (Environment** interrupt_data = interrupt_data_.load()) { | ||
// There are pending RequestInterrupt() callbacks. Tell them not to run, | ||
// then force V8 to run interrupts by compiling and running an empty script | ||
// so as not to leak memory. | ||
*interrupt_data = nullptr; | ||
|
||
Isolate::AllowJavascriptExecutionScope allow_js_here(isolate()); | ||
HandleScope handle_scope(isolate()); | ||
TryCatch try_catch(isolate()); | ||
Context::Scope context_scope(context()); | ||
|
||
#ifdef DEBUG | ||
bool consistency_check = false; | ||
isolate()->RequestInterrupt([](Isolate*, void* data) { | ||
*static_cast<bool*>(data) = true; | ||
}, &consistency_check); | ||
#endif | ||
|
||
Local<Script> script; | ||
if (Script::Compile(context(), String::Empty(isolate())).ToLocal(&script)) | ||
USE(script->Run(context())); | ||
|
||
DCHECK(consistency_check); | ||
} | ||
|
||
// FreeEnvironment() should have set this. | ||
CHECK(is_stopping()); | ||
|
@@ -735,12 +760,23 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) { | |
} | ||
|
||
void Environment::RequestInterruptFromV8() { | ||
if (interrupt_data_ != nullptr) return; // Already scheduled. | ||
|
||
// The Isolate may outlive the Environment, so some logic to handle the | ||
// situation in which the Environment is destroyed before the handler runs | ||
// is required. | ||
interrupt_data_ = new Environment*(this); | ||
|
||
// We allocate a new pointer to a pointer to this Environment instance, and | ||
// try to set it as interrupt_data_. If interrupt_data_ was already set, then | ||
// callbacks are already scheduled to run and we can delete our own pointer | ||
// and just return. If it was nullptr previously, the Environment** is stored; | ||
// ~Environment sets the Environment* contained in it to nullptr, so that | ||
// the callback can check whether ~Environment has already run and it is thus | ||
// not safe to access the Environment instance itself. | ||
Environment** interrupt_data = new Environment*(this); | ||
Environment** dummy = nullptr; | ||
if (!interrupt_data_.compare_exchange_strong(dummy, interrupt_data)) { | ||
delete interrupt_data; | ||
return; // Already scheduled. | ||
} | ||
|
||
isolate()->RequestInterrupt([](Isolate* isolate, void* data) { | ||
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. I don't remember if this callback runs asynchronously. Any chance we could get a situation where we request the interrupt here but don't run it before the Isolate is deleted? If that's possible, we would have a leak on 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. Right – that is a possibility, but I don’t quite know what to do about it (without changing V8 APIs). The leak was already there before, too, and it’s limited to the size of a pointer… 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. Actually, I have an idea. Let me see. :) 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. Sure. If ASAN starts to complain, we can force it to ignore this pointer (for now ASAN is happy, so we can leave it as is). 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. @mmarchini 7e75cc1 should address this by flushing interrupts when freeing the Environment instance … feels a bit like a hack to me but I think it’s the best we can do :) |
||
std::unique_ptr<Environment*> env_ptr { static_cast<Environment**>(data) }; | ||
|
@@ -751,9 +787,9 @@ void Environment::RequestInterruptFromV8() { | |
// handled during cleanup. | ||
return; | ||
} | ||
env->interrupt_data_ = nullptr; | ||
env->interrupt_data_.store(nullptr); | ||
env->RunAndClearInterrupts(); | ||
}, interrupt_data_); | ||
}, interrupt_data); | ||
} | ||
|
||
void Environment::ScheduleTimer(int64_t duration_ms) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ class JSBindingsConnection : public AsyncWrap { | |
|
||
private: | ||
Environment* env_; | ||
JSBindingsConnection* connection_; | ||
BaseObjectPtr<JSBindingsConnection> connection_; | ||
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, I’m not sure whether this is necessary anymore, but it was at one point while working on this, and it doesn’t hurt for the code to be a bit more robust here. |
||
}; | ||
|
||
JSBindingsConnection(Environment* env, | ||
|
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 it's worth adding a comment here explaining what this code does.
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.
@mmarchini Done, PTAL :)