Skip to content

Commit

Permalink
Attempt to cleanly exit beacon node
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Apr 1, 2023
1 parent 3ddd326 commit fa70880
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lodestar
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#
# ./lodestar.sh beacon --network prater

node --trace-deprecation --max-old-space-size=4096 ./packages/cli/bin/lodestar.js "$@"
node --trace-deprecation --max-old-space-size=4096 -r why-is-node-running/include ./packages/cli/bin/lodestar.js "$@"
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"release:tag-rc": "node scripts/release/tag_rc.mjs",
"release:tag-stable": "node scripts/release/tag_stable.mjs",
"release:publish": "lerna publish from-package --yes --no-verify-access",
"check-readme": "lerna run check-readme"
"check-readme": "lerna run check-readme",
"postinstall": "patch-package"
},
"devDependencies": {
"@chainsafe/eslint-plugin-node": "^11.2.3",
Expand All @@ -49,10 +50,10 @@
"crypto-browserify": "^3.12.0",
"electron": "^21.0.1",
"eslint": "^8.16.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-chai-expect": "^3.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-mocha": "^10.1.0",
"eslint-plugin-prettier": "^4.0.0",
"https-browserify": "^1.0.0",
"karma": "^6.4.1",
"karma-chai": "^0.1.0",
Expand All @@ -68,7 +69,9 @@
"node-gyp": "^9.0.0",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"patch-package": "^6.5.1",
"path-browserify": "^1.0.1",
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.0.5",
"process": "^0.11.10",
"resolve-typescript-plugin": "^1.2.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/beacon-node/src/chain/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class BlockProcessor {
async processBlocksJob(job: BlockInput[], opts: ImportBlockOpts = {}): Promise<void> {
await this.jobQueue.push(job, opts);
}

stop(): void {
this.jobQueue.dropAllJobs();
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,12 @@ export class BeaconChain implements IBeaconChain {
}

async close(): Promise<void> {
this.abortController.abort();
this.clock.stop();
this.blockProcessor.stop();
this.stateCache.clear();
this.checkpointStateCache.clear();
await this.bls.close();
this.abortController.abort();
}

validatorSeenAtEpoch(index: ValidatorIndex, epoch: Epoch): boolean {
Expand Down
4 changes: 4 additions & 0 deletions packages/beacon-node/src/chain/clock/LocalClock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export class LocalClock implements BeaconClock {
return toSec - (this.genesisTime + slot * this.config.SECONDS_PER_SLOT);
}

stop(): void {
clearTimeout(this.timeoutId);
}

private onNextSlot = (slot?: Slot): void => {
const clockSlot = slot ?? getCurrentSlot(this.config, this.genesisTime);
// process multiple clock slots in the case the main thread has been saturated for > SECONDS_PER_SLOT
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/chain/clock/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ export type BeaconClock = {
* Return second from a slot to either toSec or now.
*/
secFromSlot(slot: Slot, toSec?: number): number;
stop(): void;
};
2 changes: 1 addition & 1 deletion packages/beacon-node/src/eth1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Eth1ForBlockProduction implements IEth1ForBlockProduction {

this.eth1DepositDataTracker = opts.disableEth1DepositDataTracker
? null
: new Eth1DepositDataTracker(opts, modules, eth1Provider);
: new Eth1DepositDataTracker({...opts, enabled: false}, modules, eth1Provider);

this.eth1MergeBlockTracker = new Eth1MergeBlockTracker(modules, eth1Provider);
}
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/node/nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export class BeaconNode {
await this.db.stop();
if (this.controller) this.controller.abort();
this.status = BeaconNodeStatus.closed;
console.log("Beacon node closed!");
}
}
}
2 changes: 2 additions & 0 deletions packages/cli/bin/lodestar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

await import("../lib/index.js");

console.log("Process ID", process.pid);
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"@types/js-yaml": "^3.12.5",
"@types/lodash": "^4.14.157",
"@types/rimraf": "^3.0.2",
"@types/yargs": "^15.0.9"
"@types/yargs": "^15.0.9",
"why-is-node-running": "^2.2.2"
}
}
1 change: 1 addition & 0 deletions packages/reqresp/src/ReqResp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class ReqResp {
}

async stop(): Promise<void> {
this.rateLimiter.stop();
this.controller.abort();
}

Expand Down
37 changes: 37 additions & 0 deletions patches/@chainsafe+threads+1.10.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
diff --git a/node_modules/@chainsafe/threads/dist/master/implementation.node.js b/node_modules/@chainsafe/threads/dist/master/implementation.node.js
index e8a2cdb..64454e2 100644
--- a/node_modules/@chainsafe/threads/dist/master/implementation.node.js
+++ b/node_modules/@chainsafe/threads/dist/master/implementation.node.js
@@ -138,14 +138,7 @@ function initWorkerThreadsWorker() {
this.off(eventName, listener);
}
}
- const terminateWorkersAndMaster = () => {
- // we should terminate all workers and then gracefully shutdown self process
- Promise.all(allWorkers.map(worker => worker.terminate())).then(() => process.exit(0), () => process.exit(1));
- allWorkers = [];
- };
- // Take care to not leave orphaned processes behind. See #147.
- process.on("SIGINT", () => terminateWorkersAndMaster());
- process.on("SIGTERM", () => terminateWorkersAndMaster());
+
class BlobWorker extends Worker {
constructor(blob, options) {
super(Buffer.from(blob).toString("utf-8"), Object.assign(Object.assign({}, options), { fromSource: true }));
@@ -202,15 +195,7 @@ function initTinyWorker() {
return super.terminate();
}
}
- const terminateWorkersAndMaster = () => {
- // we should terminate all workers and then gracefully shutdown self process
- Promise.all(allWorkers.map(worker => worker.terminate())).then(() => process.exit(0), () => process.exit(1));
- allWorkers = [];
- };
- // Take care to not leave orphaned processes behind
- // See <https://github.com/avoidwork/tiny-worker#faq>
- process.on("SIGINT", () => terminateWorkersAndMaster());
- process.on("SIGTERM", () => terminateWorkersAndMaster());
+
class BlobWorker extends Worker {
constructor(blob, options) {
super(Buffer.from(blob).toString("utf-8"), Object.assign(Object.assign({}, options), { fromSource: true }));
78 changes: 74 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7551,6 +7551,13 @@ find-up@^4.0.0, find-up@^4.1.0:
locate-path "^5.0.0"
path-exists "^4.0.0"

find-yarn-workspace-root@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/find-yarn-workspace-root/-/find-yarn-workspace-root-2.0.0.tgz#f47fb8d239c900eb78179aa81b66673eac88f7bd"
integrity sha512-1IMnbjt4KzsQfnhnzNd8wUEgXZ44IzZaZmnLYx7D5FZlaHt2gW20Cri8Q+E/t5tIj4+epTBub+2Zxu/vNILzqQ==
dependencies:
micromatch "^4.0.2"

flat-cache@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.0.4.tgz#61b0338302b2fe9f957dcc32fc2a87f1c3048b11"
Expand Down Expand Up @@ -7715,7 +7722,7 @@ fs-extra@^8.1.0:
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-extra@^9.1.0:
fs-extra@^9.0.0, fs-extra@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d"
integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==
Expand Down Expand Up @@ -8973,7 +8980,7 @@ is-windows@^1.0.2:
resolved "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz"
integrity sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==

is-wsl@^2.2.0:
is-wsl@^2.1.1, is-wsl@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-2.2.0.tgz#74a4c76e77ca9fd3f932f290c17ea326cd157271"
integrity sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==
Expand Down Expand Up @@ -9510,6 +9517,13 @@ kind-of@^6.0.2, kind-of@^6.0.3:
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==

klaw-sync@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/klaw-sync/-/klaw-sync-6.0.0.tgz#1fd2cfd56ebb6250181114f0a581167099c2b28c"
integrity sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==
dependencies:
graceful-fs "^4.1.11"

kuler@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/kuler/-/kuler-2.0.0.tgz"
Expand Down Expand Up @@ -10101,7 +10115,7 @@ methods@^1.1.2, methods@~1.1.2:
resolved "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz"
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=

micromatch@^4.0.0, micromatch@^4.0.4:
micromatch@^4.0.0, micromatch@^4.0.2, micromatch@^4.0.4:
version "4.0.5"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.5.tgz#bc8999a7cbbf77cdc89f132f6e467051b49090c6"
integrity sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==
Expand Down Expand Up @@ -11165,6 +11179,14 @@ onetime@^5.1.0, onetime@^5.1.2:
dependencies:
mimic-fn "^2.1.0"

open@^7.4.2:
version "7.4.2"
resolved "https://registry.yarnpkg.com/open/-/open-7.4.2.tgz#b8147e26dcf3e426316c730089fd71edd29c2321"
integrity sha512-MVHddDVweXZF3awtlAS+6pgKLlm/JgxZ90+/NBurBoQctVOOB/zDdVjcyPzQ+0laDGbsWgrRkflI65sQeOgT9Q==
dependencies:
is-docker "^2.0.0"
is-wsl "^2.1.1"

open@^8.4.0:
version "8.4.0"
resolved "https://registry.yarnpkg.com/open/-/open-8.4.0.tgz#345321ae18f8138f82565a910fdc6b39e8c244f8"
Expand Down Expand Up @@ -11527,6 +11549,26 @@ parseurl@~1.3.3:
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==

patch-package@^6.5.1:
version "6.5.1"
resolved "https://registry.yarnpkg.com/patch-package/-/patch-package-6.5.1.tgz#3e5d00c16997e6160291fee06a521c42ac99b621"
integrity sha512-I/4Zsalfhc6bphmJTlrLoOcAF87jcxko4q0qsv4bGcurbr8IskEOtdnt9iCmsQVGL1B+iUhSQqweyTLJfCF9rA==
dependencies:
"@yarnpkg/lockfile" "^1.1.0"
chalk "^4.1.2"
cross-spawn "^6.0.5"
find-yarn-workspace-root "^2.0.0"
fs-extra "^9.0.0"
is-ci "^2.0.0"
klaw-sync "^6.0.0"
minimist "^1.2.6"
open "^7.4.2"
rimraf "^2.6.3"
semver "^5.6.0"
slash "^2.0.0"
tmp "^0.0.33"
yaml "^1.10.2"

path-browserify@0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/path-browserify/-/path-browserify-0.0.1.tgz"
Expand Down Expand Up @@ -11687,6 +11729,11 @@ platform@^1.3.3:
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.6.tgz#48b4ce983164b209c2d45a107adb31f473a6e7a7"
integrity sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==

postinstall-postinstall@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/postinstall-postinstall/-/postinstall-postinstall-2.1.0.tgz#4f7f77441ef539d1512c40bd04c71b06a4704ca3"
integrity sha512-7hQX6ZlZXIoRiWNrbMQaLzUUfH+sSx39u8EJ9HYuDc1kLo9IXKWjM5RSquZN1ad5GnH8CGFM78fsAAQi3OKEEQ==

prebuild-install@5.3.0:
version "5.3.0"
resolved "https://registry.npmjs.org/prebuild-install/-/prebuild-install-5.3.0.tgz"
Expand Down Expand Up @@ -12443,7 +12490,7 @@ rfdc@^1.1.4, rfdc@^1.2.0, rfdc@^1.3.0:
resolved "https://registry.npmjs.org/rfdc/-/rfdc-1.3.0.tgz"
integrity sha512-V2hovdzFbOi77/WajaSMXk2OLm+xNIeQdMMuB7icj7bk6zi2F8GGAxigcnDFpJHbNyNcgyJDiP+8nOrY5cZGrA==

rimraf@^2.6.1:
rimraf@^2.6.1, rimraf@^2.6.3:
version "2.7.1"
resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz"
integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
Expand Down Expand Up @@ -12766,6 +12813,11 @@ side-channel@^1.0.4:
get-intrinsic "^1.0.2"
object-inspect "^1.9.0"

siginfo@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/siginfo/-/siginfo-2.0.0.tgz#32e76c70b79724e3bb567cb9d543eb858ccfaf30"
integrity sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==

signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3, signal-exit@^3.0.7:
version "3.0.7"
resolved "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz"
Expand Down Expand Up @@ -12809,6 +12861,11 @@ sinon@^14.0.0:
nise "^5.1.1"
supports-color "^7.2.0"

slash@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
Expand Down Expand Up @@ -13053,6 +13110,11 @@ stack-trace@0.0.x:
resolved "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz"
integrity sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=

stackback@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/stackback/-/stackback-0.0.2.tgz#1ac8a0d9483848d1695e418b6d031a3c3ce68e3b"
integrity sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==

statuses@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/statuses/-/statuses-2.0.1.tgz#55cb000ccf1d48728bd23c685a063998cf1a1b63"
Expand Down Expand Up @@ -14546,6 +14608,14 @@ which@^2.0.1, which@^2.0.2:
dependencies:
isexe "^2.0.0"

why-is-node-running@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/why-is-node-running/-/why-is-node-running-2.2.2.tgz#4185b2b4699117819e7154594271e7e344c9973e"
integrity sha512-6tSwToZxTOcotxHeA+qGCq1mVzKR3CwcJGmVcY+QE8SHy6TnpFnh8PAvPNHYr7EcuVeG0QSMxtYCuO1ta/G/oA==
dependencies:
siginfo "^2.0.0"
stackback "0.0.2"

wide-align@^1.1.0, wide-align@^1.1.5:
version "1.1.5"
resolved "https://registry.npmjs.org/wide-align/-/wide-align-1.1.5.tgz"
Expand Down

0 comments on commit fa70880

Please sign in to comment.