Skip to content

Commit

Permalink
Merge pull request #96 from ScreamingHawk/terminate
Browse files Browse the repository at this point in the history
Add teardown to cli commands
  • Loading branch information
erhant authored Oct 6, 2024
2 parents acf1bbd + f8fa9eb commit ea48e25
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Circomkit} from './core';
import {existsSync, readFileSync, readdirSync, writeFileSync} from 'fs';
import {prettyStringify} from './utils';
import {exec} from 'child_process';
import {teardown} from './utils/teardown';

const CONFIG_PATH = './circomkit.json';

Expand Down Expand Up @@ -214,9 +215,9 @@ function cli(args: string[]) {
});

///////////////////////////////////////////////////////////////////////////////
const config = new Command('config')
.description('print configuration')
.action(() => circomkit.log.info(circomkit.config));
const config = new Command('config').description('print configuration').action(() => {
circomkit.log.info(circomkit.config);
});

///////////////////////////////////////////////////////////////////////////////
new Command()
Expand All @@ -241,6 +242,8 @@ function cli(args: string[]) {
.addCommand(witness)
.addCommand(verify)
.addCommand(calldata)
// teardown to terminate SnarkJS, otherwise hangs the program
.hook('postAction', async () => await teardown())
.parse(args);

// TODO: test graceful exits
Expand Down
15 changes: 15 additions & 0 deletions src/utils/teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare global {
// eslint-disable-next-line no-var
var curve_bn128: {terminate: () => Promise<void>} | undefined;
}

/**
* Teardown function to be run for graceful exits.
*
* SnarkJS may attach curve_bn128 to global, but does not terminate it.
* We have to do it manually (see https://github.com/iden3/snarkjs/issues/152)
* For this, we use the [`terminate`](https://github.com/iden3/ffjavascript/blob/master/src/bn128.js#L48) function.
*/
export async function teardown() {
if (globalThis.curve_bn128) await globalThis.curve_bn128.terminate();
}

0 comments on commit ea48e25

Please sign in to comment.