Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions ext/mini_racer_extension/mini_racer_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ bool reply(State& st, v8::Local<v8::Value> result, v8::Local<v8::Value> err)
return true;
}

// for when a reply is not expected to fail because of serialization
// errors but can still fail when preempted by isolate termination;
// temporarily cancels the termination exception so it can send the reply
void reply_retry(State& st, v8::Local<v8::Value> response)
{
v8::TryCatch try_catch(st.isolate);
try_catch.SetVerbose(st.verbose_exceptions);
bool ok = reply(st, response);
while (!ok) {
assert(try_catch.HasCaught());
assert(try_catch.HasTerminated());
if (!try_catch.HasTerminated()) abort();
st.isolate->CancelTerminateExecution();
ok = reply(st, response);
st.isolate->TerminateExecution();
}
}

v8::Local<v8::Value> sanitize(State& st, v8::Local<v8::Value> v)
{
// punch through proxies
Expand Down Expand Up @@ -434,7 +452,7 @@ extern "C" void v8_attach(State *pst, const uint8_t *p, size_t n)
fail:
if (!cause && try_catch.HasCaught()) cause = RUNTIME_ERROR;
auto err = to_error(st, &try_catch, cause);
if (!reply(st, err)) abort();
reply_retry(st, err);
}

// response is errback [result, err] array
Expand Down Expand Up @@ -590,7 +608,7 @@ extern "C" void v8_heap_stats(State *pst)
PROP(number_of_native_contexts);
PROP(number_of_detached_contexts);
#undef PROP
if (!reply(st, response)) abort();
reply_retry(st, response);
}

struct OutputStream : public v8::OutputStream
Expand Down Expand Up @@ -641,7 +659,7 @@ extern "C" void v8_pump_message_loop(State *pst)
st.err_reason = NO_ERROR;
}
auto result = v8::Boolean::New(st.isolate, ran_task);
if (!reply(st, result)) abort();
reply_retry(st, result);
}

int snapshot(bool is_warmup, bool verbose_exceptions,
Expand Down
12 changes: 12 additions & 0 deletions test/mini_racer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ def test_return_date
end

def test_datetime_missing
# NoMethodError: undefined method `source_location' for
# #<Thread::Backtrace::Location:0x4e88>
skip "TruffleRuby bug" if RUBY_ENGINE == "truffleruby"
date_time_backup = Object.send(:remove_const, :DateTime)

begin
Expand Down Expand Up @@ -1132,4 +1135,13 @@ def test_object_ref
actual = context.call("f", expected)
assert_equal actual, expected
end

def test_termination_exception
context = MiniRacer::Context.new
a = Thread.new { context.stop while true }
b = Thread.new { context.heap_stats while true } # should not crash/abort
sleep 1.5
a.kill
b.kill
end
end
Loading