Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi,worker: handle termination exception #33386

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ void AtExit(Environment* env, void (*cb)(void* arg), void* arg) {
}

void EmitBeforeExit(Environment* env) {
env->RunBeforeExitCallbacks();
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
"BeforeExit", env);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the lifetime of the scope a little but makes it more correct, IMO. In the old location it only covered part of the BeforeExit sequence.

if (!env->destroy_async_id_list()->empty())
AsyncWrap::DestroyAsyncIdsCallback(env);

HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Local<Value> exit_code = env->process_object()
->Get(env->context(), env->exit_code_string())
.ToLocalChecked()
->ToInteger(env->context())
.ToLocalChecked();

Local<Value> exit_code_v;
if (!env->process_object()->Get(env->context(), env->exit_code_string())
.ToLocal(&exit_code_v)) return;

Local<Integer> exit_code;
if (!exit_code_v->ToInteger(env->context()).ToLocal(&exit_code)) return;

ProcessEmit(env, "beforeExit", exit_code).ToLocalChecked();
}

Expand Down
20 changes: 0 additions & 20 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,6 @@ Environment::Environment(IsolateData* isolate_data,
}

destroy_async_id_list_.reserve(512);
BeforeExit(
[](void* arg) {
Environment* env = static_cast<Environment*>(arg);
if (!env->destroy_async_id_list()->empty())
AsyncWrap::DestroyAsyncIdsCallback(env);
},
this);

performance_state_ =
std::make_unique<performance::PerformanceState>(isolate());
Expand Down Expand Up @@ -692,19 +685,6 @@ void Environment::RunCleanup() {
}
}

void Environment::RunBeforeExitCallbacks() {
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
"BeforeExit", this);
for (ExitCallback before_exit : before_exit_functions_) {
before_exit.cb_(before_exit.arg_);
}
before_exit_functions_.clear();
}

void Environment::BeforeExit(void (*cb)(void* arg), void* arg) {
before_exit_functions_.push_back(ExitCallback{cb, arg});
}

void Environment::RunAtExitCallbacks() {
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
"AtExit", this);
Expand Down
3 changes: 0 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1102,8 +1102,6 @@ class Environment : public MemoryRetainer {
const char* name,
v8::FunctionCallback callback);

void BeforeExit(void (*cb)(void* arg), void* arg);
void RunBeforeExitCallbacks();
void AtExit(void (*cb)(void* arg), void* arg);
void RunAtExitCallbacks();

Expand Down Expand Up @@ -1364,7 +1362,6 @@ class Environment : public MemoryRetainer {
void (*cb_)(void* arg);
void* arg_;
};
std::list<ExitCallback> before_exit_functions_;

std::list<ExitCallback> at_exit_functions_;

Expand Down
8 changes: 6 additions & 2 deletions src/node_process_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ MaybeLocal<Value> ProcessEmit(Environment* env,
const char* event,
Local<Value> message) {
// Send message to enable debug in cluster workers
Local<Object> process = env->process_object();
Isolate* isolate = env->isolate();
Local<Value> argv[] = {OneByteString(isolate, event), message};

Local<String> event_string;
if (!String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(event))
.ToLocal(&event_string)) return MaybeLocal<Value>();

Local<Object> process = env->process_object();
Local<Value> argv[] = {event_string, message};
return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0});
}

Expand Down
44 changes: 44 additions & 0 deletions test/wasi/test-wasi-worker-terminate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Flags: --experimental-wasi-unstable-preview1
'use strict';

const common = require('../common');
const assert = require('assert');
const { WASI } = require('wasi');
const { Worker, parentPort } = require('worker_threads');

// void _start(void) { for (;;); }
const bytecode = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01,
0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41,
0x80, 0x88, 0x04, 0x0b, 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f,
0x72, 0x79, 0x02, 0x00, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00,
0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b,
0x00, 0x10, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x01, 0x09, 0x01, 0x00, 0x06,
0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x2f, 0x09, 0x70, 0x72, 0x6f,
0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x01, 0x0c, 0x70, 0x72, 0x6f, 0x63,
0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x01, 0x05, 0x63, 0x6c,
0x61, 0x6e, 0x67, 0x0f, 0x31, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2d, 0x34,
0x75, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x31
]);

// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
const worker = new Worker(__filename);
worker.once('message', (message) => {
assert.strictEqual(message, 'start');
setTimeout(() => worker.terminate(), common.platformTimeout(50));
});
} else {
go();
}

async function go() {
const wasi = new WASI({ returnOnExit: true });
const imports = { wasi_snapshot_preview1: wasi.wasiImport };
const module = await WebAssembly.compile(bytecode);
const instance = await WebAssembly.instantiate(module, imports);
parentPort.postMessage('start');
wasi.start(instance);
}