Skip to content

Commit 61be5e3

Browse files
committed
src: expose environment RequestInterrupt api
Allow add-ons to interrupt JavaScript execution, and wake up loop if it is currently idle.
1 parent e0191ca commit 61be5e3

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

src/api/hooks.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ void RemoveEnvironmentCleanupHookInternal(
166166
handle->info->env->RemoveCleanupHook(RunAsyncCleanupHook, handle->info.get());
167167
}
168168

169+
void RequestInterrupt(Environment* env,
170+
void (*fun)(void* arg),
171+
void* arg) {
172+
env->RequestInterrupt([fun, arg](Environment* env) {
173+
// Disallow JavaScript execution during interrupt.
174+
Isolate::DisallowJavascriptExecutionScope scope(
175+
env->isolate(),
176+
Isolate::DisallowJavascriptExecutionScope::CRASH_ON_FAILURE);
177+
fun(arg);
178+
});
179+
}
180+
169181
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
170182
Environment* env = Environment::GetCurrent(isolate);
171183
if (env == nullptr) return -1;

src/node.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,14 @@ inline void RemoveEnvironmentCleanupHook(AsyncCleanupHookHandle holder) {
10881088
RemoveEnvironmentCleanupHookInternal(holder.get());
10891089
}
10901090

1091+
// This behaves like V8's Isolate::RequestInterrupt(), but also wakes up
1092+
// the event loop if it is currently idle. The passed callback can not call
1093+
// back into JavaScript.
1094+
// This function can be called from any thread.
1095+
NODE_EXTERN void RequestInterrupt(Environment* env,
1096+
void (*fun)(void* arg),
1097+
void* arg);
1098+
10911099
/* Returns the id of the current execution context. If the return value is
10921100
* zero then no execution has been set. This will happen if the user handles
10931101
* I/O from native code. */
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <node.h>
2+
#include <v8.h>
3+
#include <thread> // NOLINT(build/c++11)
4+
5+
using node::Environment;
6+
using v8::Context;
7+
using v8::FunctionCallbackInfo;
8+
using v8::HandleScope;
9+
using v8::Isolate;
10+
using v8::Local;
11+
using v8::Maybe;
12+
using v8::Object;
13+
using v8::String;
14+
using v8::Value;
15+
16+
static std::thread interrupt_thread;
17+
18+
void ScheduleInterrupt(const FunctionCallbackInfo<Value>& args) {
19+
Isolate* isolate = args.GetIsolate();
20+
HandleScope handle_scope(isolate);
21+
Environment* env = node::GetCurrentEnvironment(isolate->GetCurrentContext());
22+
23+
interrupt_thread = std::thread([=]() {
24+
std::this_thread::sleep_for(std::chrono::seconds(1));
25+
node::RequestInterrupt(
26+
env,
27+
[](void* data) {
28+
// Interrupt is called from JS thread.
29+
interrupt_thread.join();
30+
exit(0);
31+
},
32+
nullptr);
33+
});
34+
}
35+
36+
void ScheduleInterruptWithJS(const FunctionCallbackInfo<Value>& args) {
37+
Isolate* isolate = args.GetIsolate();
38+
HandleScope handle_scope(isolate);
39+
Environment* env = node::GetCurrentEnvironment(isolate->GetCurrentContext());
40+
41+
interrupt_thread = std::thread([=]() {
42+
std::this_thread::sleep_for(std::chrono::seconds(1));
43+
node::RequestInterrupt(
44+
env,
45+
[](void* data) {
46+
// Interrupt is called from JS thread.
47+
interrupt_thread.join();
48+
Isolate* isolate = static_cast<Isolate*>(data);
49+
HandleScope handle_scope(isolate);
50+
Local<Context> ctx = isolate->GetCurrentContext();
51+
Local<String> str =
52+
String::NewFromUtf8(isolate, "interrupt").ToLocalChecked();
53+
// Calling into JS should abort immediately.
54+
Maybe<bool> result = ctx->Global()->Set(ctx, str, str);
55+
if (!result.IsNothing() && result.ToChecked()) {
56+
exit(2);
57+
}
58+
exit(1);
59+
},
60+
isolate);
61+
});
62+
}
63+
64+
void init(Local<Object> exports) {
65+
NODE_SET_METHOD(exports, "scheduleInterrupt", ScheduleInterrupt);
66+
NODE_SET_METHOD(exports, "ScheduleInterruptWithJS", ScheduleInterruptWithJS);
67+
}
68+
69+
NODE_MODULE(NODE_GYP_MODULE_NAME, init)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': [ 'binding.cc' ],
6+
'includes': ['../common.gypi'],
7+
}
8+
]
9+
}

test/addons/request-interrupt/test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
const spawnSync = require('child_process').spawnSync;
7+
8+
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
9+
10+
Object.defineProperty(globalThis, 'interrupt', {
11+
set: () => {
12+
throw new Error('should not calling into js');
13+
},
14+
});
15+
16+
if (process.argv[2] === 'child-busyloop') {
17+
(function childMain() {
18+
const addon = require(binding);
19+
addon[process.argv[3]]();
20+
while (true) {
21+
/** wait for interrupt */
22+
}
23+
})();
24+
return;
25+
}
26+
27+
if (process.argv[2] === 'child-idle') {
28+
(function childMain() {
29+
const addon = require(binding);
30+
addon[process.argv[3]]();
31+
// wait for interrupt
32+
setTimeout(() => {}, 10_000_000);
33+
})();
34+
return;
35+
}
36+
37+
for (const type of ['busyloop', 'idle']) {
38+
{
39+
const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'scheduleInterrupt' ]);
40+
assert.strictEqual(child.status, 0, `${type} should exit with code 0`);
41+
}
42+
43+
{
44+
const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'ScheduleInterruptWithJS' ]);
45+
if (process.platform === 'win32') {
46+
assert.notStrictEqual(child.status, 0, `${type} should not exit with code 0`);
47+
} else {
48+
assert.strictEqual(child.signal, 'SIGTRAP', `${type} should be interrupted with SIGTRAP`);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)