@@ -8,12 +8,16 @@ const child_process = require('child_process');
8
8
process . on ( 'uncaughtException' , err => panic ( 'process uncaughtException' , err ) ) ;
9
9
10
10
function panic ( message , err ) {
11
+ // this printing is duplicated here in case LOOP_ON_PANIC is true (the process will not be exit)
11
12
console . error ( 'PANIC:' , message , err . stack || err ) ;
12
13
while ( process . env . LOOP_ON_PANIC === 'true' ) {
13
14
console . warn ( 'Encountered an error, holding the process on an infinite loop' ) ;
14
15
child_process . execSync ( 'sleep 10' ) ;
15
16
}
16
- process . exit ( 1 ) ;
17
+ // to avoid cases where the process can exit without printing the error
18
+ process . stderr . write ( 'PANIC: ' + message + ( err . stack || err ) + '\n' , ( ) => {
19
+ process . exit ( 1 ) ;
20
+ } ) ;
17
21
}
18
22
19
23
// dump heap with kill -USR2 <pid>
@@ -37,25 +41,32 @@ function enable_heapdump(name, next_mb, step_mb) {
37
41
}
38
42
39
43
function memory_monitor ( ) {
40
- const m = process . memoryUsage ( ) ;
41
- const c = memory_monitor_config ;
42
- const h = c . heapdump ;
43
- const current = m . heapUsed ;
44
- if ( c . logging_threshold &&
45
- c . logging_threshold <= current ) {
46
- const usage = ( m . heapUsed / 1024 / 1024 ) . toFixed ( 0 ) ;
47
- const total = ( m . heapTotal / 1024 / 1024 ) . toFixed ( 0 ) ;
48
- const resident = ( m . rss / 1024 / 1024 ) . toFixed ( 0 ) ;
49
- console . log ( `memory_monitor: heap ${ usage } MB | total ${ total } MB | resident ${ resident } MB` ) ;
50
- }
51
- if ( h && h . next && h . next <= current ) {
52
- const size_mb = ( current / 1024 / 1024 ) . toFixed ( 0 ) ;
53
- const snapshot_name = `heapdump-${ h . name } -${ process . pid } -${ new Date ( ) . toISOString ( ) } -${ size_mb } MB.heapsnapshot` ;
54
- console . log ( `memory_monitor: writing ${ snapshot_name } ` ) ;
55
- heapdump . writeSnapshot ( snapshot_name ) ;
56
- const increase = current - h . next ;
57
- const align = h . step - ( increase % h . step ) ;
58
- h . next += increase + align ;
44
+ try {
45
+ const m = process . memoryUsage ( ) ;
46
+ const c = memory_monitor_config ;
47
+ const h = c . heapdump ;
48
+ const current = m . heapUsed ;
49
+ if ( c . logging_threshold &&
50
+ c . logging_threshold <= current ) {
51
+ const usage = ( m . heapUsed / 1024 / 1024 ) . toFixed ( 0 ) ;
52
+ const total = ( m . heapTotal / 1024 / 1024 ) . toFixed ( 0 ) ;
53
+ const resident = ( m . rss / 1024 / 1024 ) . toFixed ( 0 ) ;
54
+ console . log ( `memory_monitor: heap ${ usage } MB | total ${ total } MB | resident ${ resident } MB` ) ;
55
+ }
56
+ if ( h && h . next && h . next <= current ) {
57
+ const size_mb = ( current / 1024 / 1024 ) . toFixed ( 0 ) ;
58
+ const snapshot_name = `heapdump-${ h . name } -${ process . pid } -${ new Date ( ) . toISOString ( ) } -${ size_mb } MB.heapsnapshot` ;
59
+ console . log ( `memory_monitor: writing ${ snapshot_name } ` ) ;
60
+ heapdump . writeSnapshot ( snapshot_name ) ;
61
+ const increase = current - h . next ;
62
+ const align = h . step - ( increase % h . step ) ;
63
+ h . next += increase + align ;
64
+ }
65
+ } catch ( err ) {
66
+ console . error ( 'memory_monitor got an error' , err ) ;
67
+ // we saw cases where the number of open files (file descriptors) was a reason for uncaught error during the memory_monitor
68
+ // we don't want the process to fail on that
69
+ if ( err . code !== 'EMFILE' ) throw err ;
59
70
}
60
71
}
61
72
0 commit comments