Skip to content

Commit

Permalink
feat: added handler to detect promise deadlocks to ExitHandlers.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Sep 21, 2022
1 parent 206dceb commit c65e758
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bin/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class ErrorCLIPolykeyAgentProcess<T> extends ErrorCLI<T> {
exitCode = sysexits.OSERR;
}

class ErrorCLIPolykeyAsynchronousDeadlock<T> extends ErrorCLI<T> {
static description =
'PolykeyAgent process exited unexpectedly, likely due to promise deadlock';
exitCode = sysexits.SOFTWARE;
}

class ErrorNodeFindFailed<T> extends ErrorCLI<T> {
static description = 'Failed to find the node in the DHT';
exitCode = 1;
Expand All @@ -70,6 +76,7 @@ export {
ErrorCLIFileRead,
ErrorCLIPolykeyAgentStatus,
ErrorCLIPolykeyAgentProcess,
ErrorCLIPolykeyAsynchronousDeadlock,
ErrorNodeFindFailed,
ErrorNodePingFailed,
};
16 changes: 16 additions & 0 deletions src/bin/utils/ExitHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import process from 'process';
import * as binUtils from './utils';
import ErrorPolykey from '../../ErrorPolykey';
import * as CLIErrors from '../errors';

class ExitHandlers {
/**
Expand Down Expand Up @@ -84,6 +85,19 @@ class ExitHandlers {
}
};

protected deadlockHandler = async () => {
if (process.exitCode == null) {
const e = new CLIErrors.ErrorCLIPolykeyAsynchronousDeadlock();
process.stderr.write(
binUtils.outputFormatter({
type: this._errFormat,
data: e,
}),
);
process.exitCode = e.exitCode;
}
};

/**
* Automatically installs all handlers
*/
Expand All @@ -110,6 +124,7 @@ class ExitHandlers {
// Both synchronous and asynchronous errors are handled
process.once('unhandledRejection', this.errorHandler);
process.once('uncaughtException', this.errorHandler);
process.once('beforeExit', this.deadlockHandler);
}

public uninstall() {
Expand All @@ -119,6 +134,7 @@ class ExitHandlers {
process.removeListener('SIGHUP', this.signalHandler);
process.removeListener('unhandledRejection', this.errorHandler);
process.removeListener('uncaughtException', this.errorHandler);
process.removeListener('beforeExit', this.deadlockHandler);
}

/**
Expand Down

0 comments on commit c65e758

Please sign in to comment.