Skip to content

Commit d6528fc

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 d6528fc

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

src/api/hooks.cc

+10
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ void RemoveEnvironmentCleanupHookInternal(
166166
handle->info->env->RemoveCleanupHook(RunAsyncCleanupHook, handle->info.get());
167167
}
168168

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

src/node.h

+8
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. */
+69
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)
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

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
get: () => {
12+
return null;
13+
},
14+
set: () => {
15+
throw new Error('should not calling into js');
16+
},
17+
});
18+
19+
if (process.argv[2] === 'child-busyloop') {
20+
(function childMain() {
21+
const addon = require(binding);
22+
addon[process.argv[3]]();
23+
while (true) {
24+
/** wait for interrupt */
25+
}
26+
})();
27+
return;
28+
}
29+
30+
if (process.argv[2] === 'child-idle') {
31+
(function childMain() {
32+
const addon = require(binding);
33+
addon[process.argv[3]]();
34+
// wait for interrupt
35+
setTimeout(() => {}, 10_000_000);
36+
})();
37+
return;
38+
}
39+
40+
for (const type of ['busyloop', 'idle']) {
41+
{
42+
const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'scheduleInterrupt' ]);
43+
assert.strictEqual(child.status, 0, `${type} should exit with code 0`);
44+
}
45+
46+
{
47+
const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'ScheduleInterruptWithJS' ]);
48+
if (process.platform === 'win32') {
49+
assert.notStrictEqual(child.status, 0, `${type} should not exit with code 0`);
50+
} else {
51+
assert.strictEqual(child.signal, 'SIGTRAP', `${type} should be interrupted with SIGTRAP`);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)