Skip to content

Commit df34029

Browse files
committed
src: handle exceptions in env->SetImmediates
PR-URL: #18297 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent d379db3 commit df34029

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/env.cc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,26 @@ void Environment::RunAndClearNativeImmediates() {
290290
size_t ref_count = 0;
291291
std::vector<NativeImmediateCallback> list;
292292
native_immediate_callbacks_.swap(list);
293-
for (const auto& cb : list) {
294-
cb.cb_(this, cb.data_);
295-
if (cb.keep_alive_)
296-
cb.keep_alive_->Reset();
297-
if (cb.refed_)
298-
ref_count++;
299-
}
293+
auto drain_list = [&]() {
294+
v8::TryCatch try_catch(isolate());
295+
for (auto it = list.begin(); it != list.end(); ++it) {
296+
it->cb_(this, it->data_);
297+
if (it->keep_alive_)
298+
it->keep_alive_->Reset();
299+
if (it->refed_)
300+
ref_count++;
301+
if (UNLIKELY(try_catch.HasCaught())) {
302+
FatalException(isolate(), try_catch);
303+
// Bail out, remove the already executed callbacks from list
304+
// and set up a new TryCatch for the other pending callbacks.
305+
std::move_backward(it, list.end(), list.begin() + (list.end() - it));
306+
list.resize(list.end() - it);
307+
return true;
308+
}
309+
}
310+
return false;
311+
};
312+
while (drain_list()) {}
300313

301314
#ifdef DEBUG
302315
CHECK_GE(immediate_info()->count(), count);

0 commit comments

Comments
 (0)