Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9fab228

Browse filesBrowse files
ywave620targos
authored andcommitted
src: fix cb scope bugs involved in termination
Be more aggresive to clean up the async id stack, and ensure the cleanup when terminating. Calling SetIdle() when terminating is not harmless. When node terminates due to an unhandled exception, v8 preseves the vm state, which is JS and notifies node through PerIsolateMessageListener(). If node calls SetIdle() later, v8 complains because it requires the vm state to either be EXTERNEL or IDLE when embedder calling SetIdle(). PR-URL: #45596 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent cc74821 commit 9fab228
Copy full SHA for 9fab228

File tree

Expand file treeCollapse file tree

4 files changed

+40
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+40
-3
lines changed
Open diff view settings
Collapse file

‎src/api/callback.cc‎

Copy file name to clipboardExpand all lines: src/api/callback.cc
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ void InternalCallbackScope::Close() {
9797
if (closed_) return;
9898
closed_ = true;
9999

100-
Isolate* isolate = env_->isolate();
101-
auto idle = OnScopeLeave([&]() { isolate->SetIdle(true); });
100+
// This function must ends up with either cleanup the
101+
// async id stack or pop the topmost one from it
102102

103-
if (!env_->can_call_into_js()) return;
104103
auto perform_stopping_check = [&]() {
105104
if (env_->is_stopping()) {
106105
MarkAsFailed();
@@ -109,6 +108,11 @@ void InternalCallbackScope::Close() {
109108
};
110109
perform_stopping_check();
111110

111+
if (env_->is_stopping()) return;
112+
113+
Isolate* isolate = env_->isolate();
114+
auto idle = OnScopeLeave([&]() { isolate->SetIdle(true); });
115+
112116
if (!failed_ && async_context_.async_id != 0 && !skip_hooks_) {
113117
AsyncWrap::EmitAfter(env_, async_context_.async_id);
114118
}
Collapse file

‎src/api/environment.cc‎

Copy file name to clipboardExpand all lines: src/api/environment.cc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ ThreadId AllocateEnvironmentThreadId() {
881881
}
882882

883883
void DefaultProcessExitHandlerInternal(Environment* env, ExitCode exit_code) {
884+
env->set_stopping(true);
884885
env->set_can_call_into_js(false);
885886
env->stop_sub_worker_contexts();
886887
env->isolate()->DumpAndResetStats();
Collapse file
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// https://github.com/nodejs/node/issues/45421
5+
//
6+
// Check that node will NOT call v8::Isolate::SetIdle() when exiting
7+
// due to an unhandled exception, otherwise the assertion(enabled in
8+
// debug build only) in the SetIdle(), which checks that the vm state
9+
// is either EXTERNAL or IDLE will fail.
10+
//
11+
// The root cause of this issue is that before PerIsolateMessageListener()
12+
// is invoked by v8, v8 preserves the JS vm state, although it should
13+
// switch to EXTERNEL. https://bugs.chromium.org/p/v8/issues/detail?id=13464
14+
//
15+
// Therefore, this commit can be considered as an workaround of the v8 bug,
16+
// but we also find it not useful to call SetIdle() when terminating.
17+
18+
if (process.argv[2] === 'child') {
19+
const { Worker } = require('worker_threads');
20+
new Worker('', { eval: true });
21+
throw new Error('xxx');
22+
} else {
23+
const assert = require('assert');
24+
const { spawnSync } = require('child_process');
25+
const result = spawnSync(process.execPath, [__filename, 'child']);
26+
27+
const stderr = result.stderr.toString().trim();
28+
// Expect error message to be preserved
29+
assert.match(stderr, /xxx/);
30+
// Expect no crash
31+
assert(!common.nodeProcessAborted(result.status, result.signal), stderr);
32+
}
Collapse file
File renamed without changes.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.