Skip to content

Commit 22f9c98

Browse files
committed
fix: ignore errors while shutting down
- introduce a new boolean state to the Monitor: shutdown in progress - while shutdown is in progress, the Monitor ignores unhandled errors so they may not affect its exit code
1 parent 9e1b3e8 commit 22f9c98

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import * as SourceMapSupport from 'source-map-support';
2+
3+
import * as state from './state';
24
import logger = require('./common/logger');
35
import { currentClusterName } from './supervisor/cluster';
46
import { beginWatchingWorkloads } from './supervisor/watchers';
57

68
process.on('uncaughtException', (err) => {
9+
if (state.shutdownInProgress) {
10+
return;
11+
}
12+
713
try {
814
logger.error({err}, 'UNCAUGHT EXCEPTION!');
915
} catch (ignore) {
@@ -14,6 +20,10 @@ process.on('uncaughtException', (err) => {
1420
});
1521

1622
process.on('unhandledRejection', (reason) => {
23+
if (state.shutdownInProgress) {
24+
return;
25+
}
26+
1727
try {
1828
logger.error({reason}, 'UNHANDLED REJECTION!');
1929
} catch (ignore) {

src/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const workloadsLruCacheOptions = {
2121
};
2222

2323
const state = {
24+
shutdownInProgress: false,
2425
imagesAlreadyScanned: new lruCache<string, string>(imagesLruCacheOptions),
2526
workloadsAlreadyScanned: new lruCache<string, string>(workloadsLruCacheOptions),
2627
};

test/system/kind.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,10 @@ tap.test('Kubernetes-Monitor with KinD', async (t) => {
9797
// additional asserts?
9898
t.ok(nock.isDone(), 'all outgoing calls were made');
9999

100+
// instruct the Monitor to ignore errors from this point
101+
// eslint-disable-next-line @typescript-eslint/no-var-requires
102+
const kubernetesMonitorState = require('../../src/state');
103+
kubernetesMonitorState.shutdownInProgress = true;
104+
100105
// TODO cleanup the images we saved to /var/tmp?
101106
});

0 commit comments

Comments
 (0)