mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
src: expand --process-timeout output to include workers
When a process is interrupted using `--process-timeout`, the output would print the resources and stack of the main thread. If the thing keeping the process alive was a worker thread, or if the worker thread was interrupted, there was no diagnostic output. This adds an additional `--process-timeout-worker-stacks` flag that includes the worker details. We don't necessarily want to include workers every time because if there's a large number of workers the output could become quite noisy. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Claude PR-URL: https://github.com/nodejs/node/pull/67712 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
This commit is contained in:
1 parent
be0f17cfe4
commit
b4e974cc3a
7 files changed
+379
-30
No files matched your search
+47
-1
@@ -2716,6 +2716,48 @@ changes:
|
||||
|
||||
Identical to `-e` but prints the result.
|
||||
|
||||
### `--process-timeout-worker-stacks`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
> Stability: 1.1 - Active development
|
||||
|
||||
When [`--process-timeout`][] expires, also prints what each [`Worker`][] thread,
|
||||
including Workers created by other Workers, was doing and which resources were
|
||||
keeping its event loop alive to stderr, after the summary for the main thread:
|
||||
|
||||
```console
|
||||
$ node --process-timeout=5s --process-timeout-worker-stacks app.js
|
||||
(node:25418) Process timed out after 5s (--process-timeout). Exiting with code 124.
|
||||
Main thread was not executing JavaScript.
|
||||
Resources keeping the event loop alive:
|
||||
Worker (thread 2, name 'WorkerThread')
|
||||
Worker (thread 1, name 'compute')
|
||||
Worker thread 1 (name 'compute') was executing JavaScript:
|
||||
at spin (/home/user/compute.js:3:3)
|
||||
at /home/user/compute.js:6:1
|
||||
at node:internal/modules/cjs/loader:1989:14
|
||||
at node:internal/modules/cjs/loader:2129:10
|
||||
at node:internal/modules/cjs/loader:1711:32
|
||||
at node:internal/modules/cjs/loader:1501:12
|
||||
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
|
||||
at executeUserEntryPoint (node:internal/modules/run_main:166:5)
|
||||
at node:internal/main/worker_thread:247:26
|
||||
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:876:20)
|
||||
No resources keeping its event loop alive were found.
|
||||
Worker thread 2 (name 'WorkerThread') was not executing JavaScript.
|
||||
Resources keeping its event loop alive:
|
||||
Timeout (next due in 28ms)
|
||||
```
|
||||
|
||||
Worker threads that do not respond within two seconds, for example because
|
||||
they are blocked in a synchronous operation such as
|
||||
[`child_process.execSync()`][], are listed without a stack trace or
|
||||
resources. Requires
|
||||
[`--process-timeout`][].
|
||||
|
||||
### `--process-timeout=duration`
|
||||
|
||||
<!-- YAML
|
||||
@@ -2746,7 +2788,9 @@ Resources keeping the event loop alive:
|
||||
```
|
||||
|
||||
If the main thread was executing JavaScript, its stack trace is printed instead.
|
||||
Use [`--report-on-process-timeout`][] to also generate a [diagnostic report][].
|
||||
Use [`--process-timeout-worker-stacks`][] to also print the stack traces of
|
||||
Worker threads, and [`--report-on-process-timeout`][] to also generate a
|
||||
[diagnostic report][].
|
||||
|
||||
The process exits without emitting the `'beforeExit'` and `'exit'` events, as
|
||||
the JavaScript code may be what keeps the process running. Code coverage and
|
||||
@@ -4975,6 +5019,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
|
||||
[`--openssl-config`]: #--openssl-configfile
|
||||
[`--preserve-symlinks`]: #--preserve-symlinks
|
||||
[`--print`]: #-p---print-script
|
||||
[`--process-timeout-worker-stacks`]: #--process-timeout-worker-stacks
|
||||
[`--process-timeout`]: #--process-timeoutduration
|
||||
[`--redirect-warnings`]: #--redirect-warningsfile
|
||||
[`--report-on-process-timeout`]: #--report-on-process-timeout
|
||||
@@ -4995,6 +5040,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
|
||||
[`NO_COLOR`]: https://no-color.org
|
||||
[`RealFSProvider`]: vfs.md#class-realfsprovider
|
||||
[`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
|
||||
[`Worker`]: worker_threads.md#class-worker
|
||||
[`YoungGenerationSizeFromSemiSpaceSize`]: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.3.129/src/heap/heap.cc#328
|
||||
[`ZipProvider`]: vfs.md#class-zipprovider
|
||||
[`child_process.execSync()`]: child_process.md#child_processexecsynccommand-options
|
||||
|
||||
+36
-1
@@ -1415,6 +1415,39 @@ See \fB--preserve-symlinks\fR for more information.
|
||||
.It Fl p , Fl -print Ar "script"
|
||||
Identical to \fB-e\fR but prints the result.
|
||||
.
|
||||
.It Fl -process-timeout-worker-stacks
|
||||
When \fB--process-timeout\fR expires, also prints what each \fBWorker\fR thread,
|
||||
including Workers created by other Workers, was doing and which resources were
|
||||
keeping its event loop alive to stderr, after the summary for the main thread:
|
||||
.Bd -literal
|
||||
$ node --process-timeout=5s --process-timeout-worker-stacks app.js
|
||||
(node:25418) Process timed out after 5s (--process-timeout). Exiting with code 124.
|
||||
Main thread was not executing JavaScript.
|
||||
Resources keeping the event loop alive:
|
||||
Worker (thread 2, name 'WorkerThread')
|
||||
Worker (thread 1, name 'compute')
|
||||
Worker thread 1 (name 'compute') was executing JavaScript:
|
||||
at spin (/home/user/compute.js:3:3)
|
||||
at /home/user/compute.js:6:1
|
||||
at node:internal/modules/cjs/loader:1989:14
|
||||
at node:internal/modules/cjs/loader:2129:10
|
||||
at node:internal/modules/cjs/loader:1711:32
|
||||
at node:internal/modules/cjs/loader:1501:12
|
||||
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
|
||||
at executeUserEntryPoint (node:internal/modules/run_main:166:5)
|
||||
at node:internal/main/worker_thread:247:26
|
||||
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:876:20)
|
||||
No resources keeping its event loop alive were found.
|
||||
Worker thread 2 (name 'WorkerThread') was not executing JavaScript.
|
||||
Resources keeping its event loop alive:
|
||||
Timeout (next due in 28ms)
|
||||
.Ed
|
||||
Worker threads that do not respond within two seconds, for example because
|
||||
they are blocked in a synchronous operation such as
|
||||
\fBchild_process.execSync()\fR, are listed without a stack trace or
|
||||
resources. Requires
|
||||
\fB--process-timeout\fR.
|
||||
.
|
||||
.It Fl -process-timeout Ns = Ns Ar duration
|
||||
Exits the process with code \fB124\fR if it is still running after \fBduration\fR,
|
||||
measured from the start of the process. \fBduration\fR is a positive integer
|
||||
@@ -1431,7 +1464,9 @@ Resources keeping the event loop alive:
|
||||
Timeout x2 (next due in 2931ms)
|
||||
.Ed
|
||||
If the main thread was executing JavaScript, its stack trace is printed instead.
|
||||
Use \fB--report-on-process-timeout\fR to also generate a diagnostic report.
|
||||
Use \fB--process-timeout-worker-stacks\fR to also print the stack traces of
|
||||
Worker threads, and \fB--report-on-process-timeout\fR to also generate a
|
||||
diagnostic report.
|
||||
The process exits without emitting the \fB'beforeExit'\fR and \fB'exit'\fR events, as
|
||||
the JavaScript code may be what keeps the process running. Code coverage and
|
||||
profiles, such as those enabled with \fBNODE_V8_COVERAGE=dir\fR or
|
||||
|
||||
@@ -196,6 +196,10 @@ void PerProcessOptions::CheckProcessTimeoutOptions(
|
||||
errors->push_back(
|
||||
"--report-on-process-timeout must be used with --process-timeout");
|
||||
}
|
||||
if (process_timeout_worker_stacks) {
|
||||
errors->push_back("--process-timeout-worker-stacks must be used with "
|
||||
"--process-timeout");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1823,6 +1827,10 @@ PerProcessOptionsParser::PerProcessOptionsParser(
|
||||
"print why the process is still running and exit with code 124 "
|
||||
"if it has not exited after the given duration (e.g. 30s)",
|
||||
&PerProcessOptions::process_timeout);
|
||||
AddOption("--process-timeout-worker-stacks",
|
||||
"also print the JavaScript stacks of Worker threads when "
|
||||
"--process-timeout expires",
|
||||
BOOL_FIELD(process_timeout_worker_stacks));
|
||||
|
||||
Insert(iop, &PerProcessOptions::get_per_isolate_options);
|
||||
|
||||
|
||||
@@ -456,6 +456,7 @@ class PerProcessOptions : public Options {
|
||||
DEFINE_BOOL_FIELD(disable_wasm_trap_handler) = false;
|
||||
DEFINE_BOOL_FIELD(report_on_fatalerror) = false;
|
||||
DEFINE_BOOL_FIELD(report_on_process_timeout) = false;
|
||||
DEFINE_BOOL_FIELD(process_timeout_worker_stacks) = false;
|
||||
DEFINE_BOOL_FIELD(report_compact) = false;
|
||||
DEFINE_BOOL_FIELD(trace_sigint) = false;
|
||||
// Tracks whether `--run` was passed, since an empty `run` is ambiguous
|
||||
|
||||
+160
-20
@@ -21,6 +21,8 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
@@ -250,22 +252,12 @@ class ResourceList {
|
||||
std::vector<Entry> entries_;
|
||||
};
|
||||
|
||||
// Describes what the main thread was doing and what keeps the event loop
|
||||
// alive. This must not call into JavaScript, as it may run from a V8 interrupt.
|
||||
std::string FormatProcessTimeoutDiagnostics(Environment* env) {
|
||||
Isolate* isolate = env->isolate();
|
||||
// Describes what keeps the event loop of `env` alive. `whose` is "the" for the
|
||||
// main thread and "its" for a Worker thread. This must not call into
|
||||
// JavaScript, as it may run from a V8 interrupt, and must run on the thread
|
||||
// that owns `env`.
|
||||
std::string FormatResources(Environment* env, const char* whose) {
|
||||
std::string result;
|
||||
|
||||
Local<StackTrace> stack;
|
||||
if (GetCurrentStackTrace(isolate, static_cast<int>(env->stack_trace_limit()))
|
||||
.ToLocal(&stack) &&
|
||||
stack->GetFrameCount() > 0) {
|
||||
result += "Main thread was executing JavaScript:\n";
|
||||
result += FormatJavaScriptStack(isolate, stack);
|
||||
} else {
|
||||
result += "Main thread was not executing JavaScript.\n";
|
||||
}
|
||||
|
||||
ResourceList resources;
|
||||
|
||||
for (ReqWrapBase* req_wrap : *env->req_wrap_queue()) {
|
||||
@@ -340,14 +332,148 @@ std::string FormatProcessTimeoutDiagnostics(Environment* env) {
|
||||
&walk_data);
|
||||
|
||||
if (resources.empty()) {
|
||||
result += "No resources keeping the event loop alive were found.\n";
|
||||
result += SPrintF("No resources keeping %s event loop alive were found.\n",
|
||||
whose);
|
||||
} else {
|
||||
result += "Resources keeping the event loop alive:\n";
|
||||
result += SPrintF("Resources keeping %s event loop alive:\n", whose);
|
||||
result += resources.ToString();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Describes what the main thread was doing and what keeps the event loop
|
||||
// alive. This must not call into JavaScript, as it may run from a V8 interrupt.
|
||||
std::string FormatProcessTimeoutDiagnostics(Environment* env) {
|
||||
Isolate* isolate = env->isolate();
|
||||
std::string result;
|
||||
|
||||
Local<StackTrace> stack;
|
||||
if (GetCurrentStackTrace(isolate, static_cast<int>(env->stack_trace_limit()))
|
||||
.ToLocal(&stack) &&
|
||||
stack->GetFrameCount() > 0) {
|
||||
result += "Main thread was executing JavaScript:\n";
|
||||
result += FormatJavaScriptStack(isolate, stack);
|
||||
} else {
|
||||
result += "Main thread was not executing JavaScript.\n";
|
||||
}
|
||||
return result + FormatResources(env, "the");
|
||||
}
|
||||
|
||||
// What the Worker threads were doing, keyed by thread id so that they are
|
||||
// printed in the order in which the Workers were created. Shared with the
|
||||
// interrupt callbacks, which can outlive FormatWorkerStacks() if a Worker
|
||||
// thread does not respond in time.
|
||||
struct WorkerStacks {
|
||||
struct Entry {
|
||||
std::string name;
|
||||
// 0 for Workers created by the main thread.
|
||||
uint64_t parent_thread_id = 0;
|
||||
bool responded = false;
|
||||
// Empty if the Worker thread was not executing JavaScript.
|
||||
std::string stack;
|
||||
// What keeps the Worker thread's event loop alive.
|
||||
std::string resources;
|
||||
};
|
||||
|
||||
Mutex mutex;
|
||||
ConditionVariable notify;
|
||||
std::map<uint64_t, Entry> entries;
|
||||
size_t pending = 0;
|
||||
};
|
||||
|
||||
// Asks the Workers of `env`, and through them their own Workers, for their
|
||||
// JavaScript stacks and the resources keeping their event loops alive. This
|
||||
// does not wait for the answers.
|
||||
void RequestWorkerStacks(Environment* env,
|
||||
const std::shared_ptr<WorkerStacks>& shared,
|
||||
uint64_t parent_thread_id) {
|
||||
env->ForEachWorker([&](worker::Worker* worker) {
|
||||
if (worker->is_stopped()) return;
|
||||
const uint64_t thread_id = worker->thread_id();
|
||||
{
|
||||
Mutex::ScopedLock lock(shared->mutex);
|
||||
WorkerStacks::Entry& entry = shared->entries[thread_id];
|
||||
entry.name = worker->name();
|
||||
entry.parent_thread_id = parent_thread_id;
|
||||
shared->pending++;
|
||||
}
|
||||
const bool requested =
|
||||
worker->RequestInterrupt([shared, thread_id](Environment* worker_env) {
|
||||
Isolate* isolate = worker_env->isolate();
|
||||
HandleScope handle_scope(isolate);
|
||||
std::string stack;
|
||||
std::string resources;
|
||||
{
|
||||
Isolate::DisallowJavascriptExecutionScope disallow_js(
|
||||
isolate,
|
||||
Isolate::DisallowJavascriptExecutionScope::CRASH_ON_FAILURE);
|
||||
Local<StackTrace> trace;
|
||||
if (GetCurrentStackTrace(
|
||||
isolate, static_cast<int>(worker_env->stack_trace_limit()))
|
||||
.ToLocal(&trace)) {
|
||||
stack = FormatJavaScriptStack(isolate, trace);
|
||||
}
|
||||
resources = FormatResources(worker_env, "its");
|
||||
}
|
||||
// Request the stacks of nested Workers before this one counts as
|
||||
// done, so that the main thread waits for them as well.
|
||||
RequestWorkerStacks(worker_env, shared, thread_id);
|
||||
|
||||
Mutex::ScopedLock lock(shared->mutex);
|
||||
WorkerStacks::Entry& entry = shared->entries[thread_id];
|
||||
entry.responded = true;
|
||||
entry.stack = std::move(stack);
|
||||
entry.resources = std::move(resources);
|
||||
shared->pending--;
|
||||
shared->notify.Signal(lock);
|
||||
});
|
||||
if (!requested) {
|
||||
// The Worker thread has already stopped.
|
||||
Mutex::ScopedLock lock(shared->mutex);
|
||||
shared->entries.erase(thread_id);
|
||||
shared->pending--;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Describes what the Worker threads were doing and what keeps their event loops
|
||||
// alive, for --process-timeout-worker-stacks. Worker threads that are blocked,
|
||||
// e.g. in a synchronous native call, cannot respond, so they are waited for
|
||||
// only briefly.
|
||||
std::string FormatWorkerStacks(Environment* env) {
|
||||
auto shared = std::make_shared<WorkerStacks>();
|
||||
RequestWorkerStacks(env, shared, 0);
|
||||
|
||||
const uint64_t deadline =
|
||||
uv_hrtime() + kProcessTimeoutResponseGraceMs * kNanosecondsPerMillisecond;
|
||||
Mutex::ScopedLock lock(shared->mutex);
|
||||
while (shared->pending > 0) {
|
||||
const uint64_t now = uv_hrtime();
|
||||
if (now >= deadline) break;
|
||||
shared->notify.TimedWait(lock, deadline - now);
|
||||
}
|
||||
|
||||
std::string result;
|
||||
for (const auto& [thread_id, entry] : shared->entries) {
|
||||
result += SPrintF("Worker thread %d", thread_id);
|
||||
if (!entry.name.empty()) result += SPrintF(" (name '%s')", entry.name);
|
||||
if (entry.parent_thread_id != 0) {
|
||||
result +=
|
||||
SPrintF(", created by Worker thread %d,", entry.parent_thread_id);
|
||||
}
|
||||
if (!entry.responded) {
|
||||
result += SPrintF(" did not respond within %dms.\n",
|
||||
kProcessTimeoutResponseGraceMs);
|
||||
} else if (entry.stack.empty()) {
|
||||
result += " was not executing JavaScript.\n";
|
||||
} else {
|
||||
result += " was executing JavaScript:\n" + entry.stack;
|
||||
}
|
||||
result += entry.resources;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
struct ProcessTimeoutWatchdog::State {
|
||||
@@ -364,8 +490,14 @@ struct ProcessTimeoutWatchdog::State {
|
||||
kDisarmed,
|
||||
};
|
||||
|
||||
State(uint64_t deadline, std::string duration, bool report)
|
||||
: deadline(deadline), duration(std::move(duration)), report(report) {
|
||||
State(uint64_t deadline,
|
||||
std::string duration,
|
||||
bool report,
|
||||
bool worker_stacks)
|
||||
: deadline(deadline),
|
||||
duration(std::move(duration)),
|
||||
report(report),
|
||||
worker_stacks(worker_stacks) {
|
||||
CHECK_EQ(uv_mutex_init(&mutex), 0);
|
||||
CHECK_EQ(uv_cond_init(&cond), 0);
|
||||
}
|
||||
@@ -397,6 +529,8 @@ struct ProcessTimeoutWatchdog::State {
|
||||
const std::string duration;
|
||||
// --report-on-process-timeout
|
||||
const bool report;
|
||||
// --process-timeout-worker-stacks
|
||||
const bool worker_stacks;
|
||||
};
|
||||
|
||||
bool ProcessTimeoutWatchdog::IsEnabled() {
|
||||
@@ -410,11 +544,13 @@ std::unique_ptr<ProcessTimeoutWatchdog> ProcessTimeoutWatchdog::MaybeStart(
|
||||
uint64_t timeout_ms;
|
||||
std::string duration;
|
||||
bool report;
|
||||
bool worker_stacks;
|
||||
{
|
||||
Mutex::ScopedLock lock(per_process::cli_options_mutex);
|
||||
timeout_ms = per_process::cli_options->process_timeout_ms;
|
||||
duration = per_process::cli_options->process_timeout;
|
||||
report = per_process::cli_options->report_on_process_timeout;
|
||||
worker_stacks = per_process::cli_options->process_timeout_worker_stacks;
|
||||
}
|
||||
if (timeout_ms == 0) return nullptr;
|
||||
|
||||
@@ -424,7 +560,8 @@ std::unique_ptr<ProcessTimeoutWatchdog> ProcessTimeoutWatchdog::MaybeStart(
|
||||
|
||||
const uint64_t deadline =
|
||||
per_process::node_start_time + timeout_ms * kNanosecondsPerMillisecond;
|
||||
auto state = std::make_shared<State>(deadline, std::move(duration), report);
|
||||
auto state = std::make_shared<State>(
|
||||
deadline, std::move(duration), report, worker_stacks);
|
||||
return std::unique_ptr<ProcessTimeoutWatchdog>(
|
||||
new ProcessTimeoutWatchdog(env, std::move(state)));
|
||||
}
|
||||
@@ -566,6 +703,9 @@ void ProcessTimeoutWatchdog::OnTimeout(Environment* env,
|
||||
"%s%s",
|
||||
FormatProcessTimeoutHeader(state->duration),
|
||||
FormatProcessTimeoutDiagnostics(env));
|
||||
if (state->worker_stacks) {
|
||||
FPrintF(stderr, "%s", FormatWorkerStacks(env));
|
||||
}
|
||||
fflush(stderr);
|
||||
|
||||
if (state->report) {
|
||||
|
||||
@@ -38,16 +38,24 @@ for (const value of [
|
||||
});
|
||||
}
|
||||
|
||||
spawnSyncAndExit(process.execPath, ['--report-on-process-timeout', '-e', ''], {
|
||||
status: 9,
|
||||
signal: null,
|
||||
trim: true,
|
||||
stderr: `${process.execPath}: --report-on-process-timeout must be used with ` +
|
||||
'--process-timeout',
|
||||
});
|
||||
for (const option of [
|
||||
'--report-on-process-timeout',
|
||||
'--process-timeout-worker-stacks',
|
||||
]) {
|
||||
spawnSyncAndExit(process.execPath, [option, '-e', ''], {
|
||||
status: 9,
|
||||
signal: null,
|
||||
trim: true,
|
||||
stderr: `${process.execPath}: ${option} must be used with --process-timeout`,
|
||||
});
|
||||
}
|
||||
|
||||
if (!process.config.variables.node_without_node_options) {
|
||||
for (const option of ['--process-timeout=1s', '--report-on-process-timeout']) {
|
||||
for (const option of [
|
||||
'--process-timeout=1s',
|
||||
'--report-on-process-timeout',
|
||||
'--process-timeout-worker-stacks',
|
||||
]) {
|
||||
spawnSyncAndExit(process.execPath, ['-e', ''], {
|
||||
env: { ...process.env, NODE_OPTIONS: option },
|
||||
}, {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
'use strict';
|
||||
|
||||
// Tests that --process-timeout-worker-stacks prints the JavaScript stacks of
|
||||
// Worker threads, and what keeps their event loops alive, when
|
||||
// --process-timeout expires.
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const { spawnSync } = require('child_process');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
|
||||
tmpdir.refresh();
|
||||
|
||||
// The deadline is measured from the start of the process, so on a slow machine
|
||||
// it can expire before the Workers have been started. The script writes 'ready'
|
||||
// to stdout once they have, and runs again with a longer timeout if it did not
|
||||
// get that far.
|
||||
function runUntilReady(args, isReady) {
|
||||
for (let timeout = common.platformTimeout(1000); ; timeout *= 2) {
|
||||
const child = spawnSync(process.execPath, [
|
||||
`--process-timeout=${timeout}ms`,
|
||||
...args(timeout),
|
||||
], { encoding: 'utf8' });
|
||||
if (!isReady(child, timeout) && timeout < common.platformTimeout(16000)) {
|
||||
continue;
|
||||
}
|
||||
assert.strictEqual(child.signal, null);
|
||||
assert.strictEqual(child.status, 124, child.stderr);
|
||||
return child.stderr;
|
||||
}
|
||||
}
|
||||
|
||||
// One Worker is executing JavaScript, one is idle with a timer and has created
|
||||
// a Worker of its own that is executing JavaScript, and one is unref'd.
|
||||
const script = `
|
||||
const { writeSync } = require('fs');
|
||||
const { Worker } = require('worker_threads');
|
||||
const spin = 'require("worker_threads").parentPort.postMessage(0);' +
|
||||
'function spin() { for (;;); } spin();';
|
||||
let started = 0;
|
||||
function onStarted() {
|
||||
if (++started === 3) writeSync(1, 'ready\\n');
|
||||
}
|
||||
new Worker(spin, { eval: true, name: 'spinning' }).once('message', onStarted);
|
||||
new Worker(\`
|
||||
const { parentPort, Worker } = require('worker_threads');
|
||||
new Worker(\${JSON.stringify(spin)}, { eval: true, name: 'nested' })
|
||||
.once('message', () => parentPort.postMessage(0));
|
||||
setTimeout(() => {}, 60_000);
|
||||
\`, { eval: true, name: 'idle' }).once('message', onStarted);
|
||||
new Worker(spin, { eval: true }).once('message', onStarted).unref();
|
||||
`;
|
||||
const isReady = (child) => child.stdout === 'ready\n';
|
||||
|
||||
{
|
||||
const stderr = runUntilReady(
|
||||
() => ['--process-timeout-worker-stacks', '-e', script], isReady);
|
||||
assert.match(stderr, /^Main thread was not executing JavaScript\.$/m);
|
||||
assert.match(stderr, new RegExp(
|
||||
"^Worker thread 1 \\(name 'spinning'\\) was executing JavaScript:\\r?\\n" +
|
||||
' {4}at spin \\(\\[worker eval\\]:1:\\d+\\)$', 'm'));
|
||||
// The resources keeping a Worker thread's event loop alive are listed after
|
||||
// its state.
|
||||
assert.match(stderr, new RegExp(
|
||||
"^Worker thread 2 \\(name 'idle'\\) was not executing JavaScript\\.\\r?\\n" +
|
||||
'Resources keeping its event loop alive:\\r?\\n' +
|
||||
' {4}Timeout \\(next due in \\d+ms\\)\\r?\\n' +
|
||||
" {4}Worker \\(thread 4, name 'nested'\\)\\r?\\n" +
|
||||
'(?:Worker thread|$)', 'm'));
|
||||
assert.match(stderr,
|
||||
/^No resources keeping its event loop alive were found\.$/m);
|
||||
assert.match(stderr, new RegExp(
|
||||
"^Worker thread 4 \\(name 'nested'\\), created by Worker thread 2, was " +
|
||||
'executing JavaScript:\\r?\\n {4}at spin \\(\\[worker eval\\]:1:\\d+\\)$', 'm'));
|
||||
// Workers that do not keep the event loop alive are included too.
|
||||
assert.match(stderr, new RegExp(
|
||||
"^Worker thread 3 \\(name 'WorkerThread'\\) was executing JavaScript:\\r?\\n" +
|
||||
' {4}at spin \\(\\[worker eval\\]:1:\\d+\\)$', 'm'));
|
||||
// The Workers are listed by thread id, after the main thread's summary.
|
||||
const order = [...stderr.matchAll(/^Worker thread (\d+)/mg)].map((m) => m[1]);
|
||||
assert.deepStrictEqual(order, ['1', '2', '3', '4']);
|
||||
assert.ok(stderr.indexOf('Worker thread 1') >
|
||||
stderr.indexOf('Resources keeping the event loop alive:'), stderr);
|
||||
}
|
||||
|
||||
{
|
||||
// Without the option, the stacks of Worker threads are not printed.
|
||||
const stderr = runUntilReady(() => ['-e', script], isReady);
|
||||
assert.match(stderr, /^ {4}Worker \(thread 1, name 'spinning'\)$/m);
|
||||
assert.doesNotMatch(stderr, /^Worker thread/m);
|
||||
}
|
||||
|
||||
{
|
||||
// A Worker thread that is blocked in a synchronous native call cannot
|
||||
// respond, so it is only waited for briefly. The fixture creates a file once
|
||||
// the Worker thread is blocked.
|
||||
const stderr = runUntilReady((timeout) => [
|
||||
'--process-timeout-worker-stacks',
|
||||
fixtures.path('process-timeout', 'blocked-worker.js'),
|
||||
// Child processes of a previous attempt may still be running, so use a
|
||||
// different file for each attempt.
|
||||
tmpdir.resolve(`blocked-worker.${timeout}.ready`),
|
||||
], (child, timeout) =>
|
||||
fs.existsSync(tmpdir.resolve(`blocked-worker.${timeout}.ready`)));
|
||||
assert.match(stderr, new RegExp(
|
||||
"^Worker thread 1 \\(name 'blocked'\\) did not respond within 2000ms\\.\\r?\\n" +
|
||||
'\\(node:\\d+\\) The process did not finish exiting within 5000ms after ' +
|
||||
'--process-timeout expired\\. Forcing exit\\.$', 'm'));
|
||||
}
|
||||
Reference in new issue
Block a user