Skip to content

Commit 08bcdde

Browse files
jasnellMylesBorins
authored andcommitted
src: handle exceptions in env->SetImmediates
Backport-PR-URL: #19185 PR-URL: #18297 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 6787913 commit 08bcdde

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/env.cc

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

307320
#ifdef DEBUG
308321
CHECK_GE(immediate_info()->count(), count);

0 commit comments

Comments
 (0)