Skip to content

Commit fa1656a

Browse files
committed
Improve server 'last error' detection
There's plenty of cases where old errors were being assumed to cause a much later crash, which is not likely. We leave a little margin here for very slow exits.
1 parent 8a287f2 commit fa1656a

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,19 @@ if (!amMainInstance) {
416416
addBreadcrumb({ category: 'server-stdout', message: data.toString('utf8'), level: <any>'info' });
417417
});
418418

419-
let lastError: string | undefined = undefined;
419+
let lastError: { message: string, time: number } | undefined = undefined;
420420
serverStderr.on('data', (data) => {
421421
const errorOutput = data.toString('utf8');
422422
addBreadcrumb({ category: 'server-stderr', message: errorOutput, level: <any>'warning' });
423423

424424
// Remember the last '*Error:' line we saw.
425-
lastError = errorOutput
426-
.split('\n')
427-
.filter((line: string) => line.match(/^\s*Error:/i))
428-
.slice(-1)[0]?.trim() || lastError;
425+
lastError = {
426+
message: errorOutput
427+
.split('\n')
428+
.filter((line: string) => line.match(/^\s*Error:/i))
429+
.slice(-1)[0]?.trim() || lastError,
430+
time: Date.now()
431+
};
429432
});
430433

431434
const serverStartTime = Date.now();
@@ -443,8 +446,8 @@ if (!amMainInstance) {
443446

444447
if (errorOrCode && typeof errorOrCode !== 'number') {
445448
error = errorOrCode;
446-
} else if (lastError) {
447-
error = new Error(`Server crashed with '${lastError}' (${errorOrCode})`);
449+
} else if (lastError?.time && (Date.now() - lastError.time) < 2000) {
450+
error = new Error(`Server crashed with '${lastError.message}' (${errorOrCode})`);
448451
} else {
449452
error = new Error(`Server shutdown unexpectedly with code ${errorOrCode}`);
450453
}

0 commit comments

Comments
 (0)