Skip to content

Commit 718ad10

Browse files
legendecastargos
authored andcommitted
src: fix fatal errors when a current isolate not exist
napi_fatal_error and node watchdog trigger fatal error but rather running on a thread that hold no current isolate. PR-URL: #38624 Reviewed-By: Michael Dawson <[email protected]>
1 parent a0dc194 commit 718ad10

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

src/node_errors.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,12 @@ void OnFatalError(const char* location, const char* message) {
425425
}
426426

427427
Isolate* isolate = Isolate::GetCurrent();
428+
// TODO(legendecas): investigate failures on triggering node-report with
429+
// nullptr isolates.
430+
if (isolate == nullptr) {
431+
fflush(stderr);
432+
ABORT();
433+
}
428434
Environment* env = Environment::GetCurrent(isolate);
429435
bool report_on_fatalerror;
430436
{

test/node-api/test_fatal/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ const p = child_process.spawnSync(
1616
assert.ifError(p.error);
1717
assert.ok(p.stderr.toString().includes(
1818
'FATAL ERROR: test_fatal::Test fatal message'));
19+
assert.ok(p.status === 134 || p.signal === 'SIGABRT');

test/node-api/test_fatal/test2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ const p = child_process.spawnSync(
1616
assert.ifError(p.error);
1717
assert.ok(p.stderr.toString().includes(
1818
'FATAL ERROR: test_fatal::Test fatal message'));
19+
assert.ok(p.status === 134 || p.signal === 'SIGABRT');

test/node-api/test_fatal/test_fatal.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1+
// For the purpose of this test we use libuv's threading library. When deciding
2+
// on a threading library for a new project it bears remembering that in the
3+
// future libuv may introduce API changes which may render it non-ABI-stable,
4+
// which, in turn, may affect the ABI stability of the project despite its use
5+
// of N-API.
6+
#include <uv.h>
17
#include <node_api.h>
28
#include "../../js-native-api/common.h"
39

10+
static uv_thread_t uv_thread;
11+
12+
static void work_thread(void* data) {
13+
napi_fatal_error("work_thread", NAPI_AUTO_LENGTH,
14+
"foobar", NAPI_AUTO_LENGTH);
15+
}
16+
417
static napi_value Test(napi_env env, napi_callback_info info) {
518
napi_fatal_error("test_fatal::Test", NAPI_AUTO_LENGTH,
619
"fatal message", NAPI_AUTO_LENGTH);
720
return NULL;
821
}
922

23+
static napi_value TestThread(napi_env env, napi_callback_info info) {
24+
NODE_API_ASSERT(env,
25+
(uv_thread_create(&uv_thread, work_thread, NULL) == 0),
26+
"Thread creation");
27+
return NULL;
28+
}
29+
1030
static napi_value TestStringLength(napi_env env, napi_callback_info info) {
1131
napi_fatal_error("test_fatal::TestStringLength", 16, "fatal message", 13);
1232
return NULL;
@@ -16,6 +36,7 @@ static napi_value Init(napi_env env, napi_value exports) {
1636
napi_property_descriptor properties[] = {
1737
DECLARE_NODE_API_PROPERTY("Test", Test),
1838
DECLARE_NODE_API_PROPERTY("TestStringLength", TestStringLength),
39+
DECLARE_NODE_API_PROPERTY("TestThread", TestThread),
1940
};
2041

2142
NODE_API_CALL(env, napi_define_properties(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
const child_process = require('child_process');
5+
const test_fatal = require(`./build/${common.buildType}/test_fatal`);
6+
7+
// Test in a child process because the test code will trigger a fatal error
8+
// that crashes the process.
9+
if (process.argv[2] === 'child') {
10+
test_fatal.TestThread();
11+
// Busy loop to allow the work thread to abort.
12+
while (true) {}
13+
}
14+
15+
const p = child_process.spawnSync(
16+
process.execPath, [ __filename, 'child' ]);
17+
assert.ifError(p.error);
18+
assert.ok(p.stderr.toString().includes(
19+
'FATAL ERROR: work_thread foobar'));
20+
assert.ok(p.status === 134 || p.signal === 'SIGABRT');

0 commit comments

Comments
 (0)