Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/beacon-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export {type NodeJsLibp2pOpts, createNodeJsLibp2p} from "./network/index.js";
export * from "./node/index.js";
// Export type util for CLI - TEMP move to lodestar-types eventually
export {getStateSlotFromBytes, getStateTypeFromBytes} from "./util/multifork.js";

export {hasWorkerTerminationFailed} from "./util/workerEvents.js";
12 changes: 12 additions & 0 deletions packages/beacon-node/src/util/workerEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ export function wireEventsOnMainThread<EventData>(
* @returns `false` if it could not be terminated, the worker thread is then still running and the
* caller has to decide how to proceed without it.
*/
/**
* Process-wide record that some worker thread could not be terminated. It is a property of the
* process, not of any one instance, and `process.exit()` joins every worker regardless of which
* one is stuck, so the caller only needs to know that it happened at all.
*/
let someWorkerTerminationFailed = false;

export function hasWorkerTerminationFailed(): boolean {
return someWorkerTerminationFailed;
}

export async function terminateWorkerThread({
worker,
retryMs,
Expand Down Expand Up @@ -151,5 +162,6 @@ export async function terminateWorkerThread({
}

logger?.debug(`Worker thread failed to terminate in ${retryCount * retryMs}ms`);
someWorkerTerminationFailed = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear the failure state after late worker termination

If the worker misses the final retry deadline but terminates while the remaining BeaconNode.close() steps are running, this flag remains permanently true. The handler then sends itself SIGTERM even though no worker remains to block process.exit(), changing a successful shutdown from exit 0 to exit 143. Track workers that are currently unterminated, or clear the state when the existing termination subscription receives a late event.

Useful? React with 👍 / 👎.

return false;
}
20 changes: 19 additions & 1 deletion packages/cli/src/cmds/beacon/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "node:path";
import {getHeapStatistics} from "node:v8";
import {SignableENR} from "@chainsafe/enr";
import {hasher} from "@chainsafe/persistent-merkle-tree";
import {BeaconDb, BeaconNode} from "@lodestar/beacon-node";
import {BeaconDb, BeaconNode, hasWorkerTerminationFailed} from "@lodestar/beacon-node";
import {ChainForkConfig, createBeaconConfig} from "@lodestar/config";
import {LevelDbController} from "@lodestar/db/controller/level";
import {LoggerNode, getNodeLogger} from "@lodestar/logger/node";
Expand Down Expand Up @@ -146,6 +146,24 @@ export async function beaconHandler(args: BeaconArgs & GlobalArgs): Promise<void
try {
await node.close();
logger.debug("Beacon node closed");
if (hasWorkerTerminationFailed()) {
// The state is archived and the db is closed by now, but `process.exit()` joins every
// worker via `stop_sub_worker_contexts()`, so a worker that could not be terminated
// blocks it until the process manager gives up. Leave on our own terms instead.
logger.warn("Worker thread still running, forcing process exit");
// A signal always reports 128+signum, the code can not be chosen, but SIGTERM (143) is
// what a container reports for a normal stop while SIGKILL (137) reads as a crash
process.removeAllListeners("SIGTERM");
process.kill(process.pid, "SIGTERM");
// Only reached if the signal was not delivered. The kernel discards signals sent to
// pid 1 that have no handler installed, including SIGKILL, so this is expected when
// running as pid 1 in a container without an init. `process.exit()` below then still
// blocks on the join, which is no worse than before this branch existed.
logger.error(
"Could not force exit, run with an init (docker --init, tini) so the process is not pid 1",
{pid: process.pid}
);
}
// Explicitly exit until active handles issue is resolved
// See https://github.com/ChainSafe/lodestar/issues/5642
process.exit(0);
Expand Down
Loading